Question

1. Please write the following in C++ also please show all output code and comment on...

1. Please write the following in C++ also please show all output code and comment on code.

2. Also, use CPPUnitLite to write all test and show outputs for each test.

Write CppUnitLite tests to verify correct behavior for all the exercises. The modifications are aimed at making the exercises more conducive to unit tests.

Write a function that swaps (exchanges the values of two integers). Use int* as the argument type. Write a second swap function using a reference (i.e., int&) as the argument type.

Define a table of the names of months of the year and the number of days in each month. Write out that table to a stringstream. Do this twice; once using an array of char for the names and an array for the number of days and a second time using an array of structures, with each structure holding the name of a month and the number of days in it.

Write a function cat() that takes two C-style strings (i.e., char*) arguments and returns a C-style string that is the concatenation of the arguments. Use new to find store for the result. Write a second function cat that takes two const std::string& arguments and returns a std::string that is a concatenation of the arguments. The std::string version does not require new. Which is the better approach? Explain your rationale for which is the better approach?

Homework Answers

Answer #1

let dist be a |t| × |t| array of minimum distances initialized to ∞ (infinity)
for each vertex v
dist[v][v] ← 0
for each edge (u,v)
dist[u][v] ← w(u,v) // the weight of the edge (u,v)
for k from 1 to |T|
for i from 1 to |T|
for j from 1 to |T|
if dist[i][j] > dist[i][k] + dist[k][j]
dist[i][j] ← dist[i][k] + dist[k][j]
end if
       --------------------------------
       ///call by value:
       void swap(int, int);

int main()
{
int Number1, Number2;

printf("Enter the value of Number1 and Number2\n");
scanf("%d%d",&Number1,&Number2);
printf("Before Swapping\nNumber1 = %d\nNumber2 = %d\n", Number1, Number2);
swap(Number1, Number2);
printf("After Swapping\nNumber1 = %d\nNumber2 = %d\n", Number1, Number2);
return 0;
}
void swap(int a, int b)
{
int temp;
temp = b;
b = a;
a = temp;
printf("Values of a and b is %d %d\n",a,b);
}
//call by refernce
#include<stdio.h>
#include<conio.h>
void swap(int *Number1, int *Number2);
void main() {
int Number1, Number2;
printf("\nEnter First number : ");
scanf("%d", &Number1);
printf("\nEnter Second number : ");
scanf("%d", &Number2);
printf("\nBefore Swaping Number1 = %d and Number2 = %d",Number1 , Number2);
swap(&Number1, &Number2); // Function Call - Pass Bb Reference
printf("\nAfter Swaping Number1 = %d and Number2 = %d", Number1, Number2);
getch();
}
void swap(int *Number1, int *Number2) {
int temp;
temp = *Number1;
*Number1 = *Number2;
*Number2 = temp;
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------

#include<stdio.h>
#include<conio.h>
TEST(tableArraysTest, stringstream){
   std::stringstream sTable;
   const int NUM_MONTHS = 12;
   char MonthNames[][NUM_MONTHS] = {
       "January",
       "Frebruary",
       "March",
       "April",
       "May",
       "June",
       "July",
       "August",
       "September",
       "October",
       "November",
       "December"
   };
   int DaysInmonth[NUM_MONTHS] = {
       31,
       28,
       31,
       30,
       31,
       30,
       31,
       31,
       30,
       31,
       30,
       31
   };
   for(int i = 0; i < NUM_MONTHS; i++){
       sTable << MonthNames[i] << "\t" << DaysInmonth[i] << std::endl;
   }  
   //std::cout << sTable.str() << std::endl; //actually output the table
}

#include<stdio.h>
#include<conio.h>
void TEST()
TEST(tableArStructTest, stringstream){
   std::stringstream sTable;
  
   const int NUM_MONTHS = 12;
  
   struct MONTH{
       std::string name;
       int days;
   }months[NUM_MONTHS] ={
       {"January", 31},
       {"Frebruary", 28},
       {"March", 31},
       {"April", 30},
       {"May", 31},
       {"June", 30},
       {"July", 31},
       {"August", 31},
       {"September", 30},
       {"October", 31},
       {"November", 30},
       {"December", 31}
   };
  
   for(int i = 0; i < NUM_MONTHS; i++){
       sTable << months[i].name << "\t" << months[i].days << std::endl;
   }
  
   std::cout << sTable.str() << std::endl; //actually output the table
}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
C++ only: Please implement a function that accepts a string parameter as well as two character...
C++ only: Please implement a function that accepts a string parameter as well as two character arguments and a boolean parameter. Your function should return the number of times it finds the character arguments inside the string parameter. When both of the passed character arguments are found in the string parameter, set the boolean argument to true. Otherwise, set that boolean argument to false. Return -1 if the string argument is the empty string. The declaration for this function will...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
Please write code in C++ and include headers files not std/bitsc: (Pattern matching) Write a program...
Please write code in C++ and include headers files not std/bitsc: (Pattern matching) Write a program that prompts the user to enter two strings and tests whether the second string is a substring in the first string. Suppose the neighboring characters in the string are distinct. Analyze the time complexity of your algorithm. Your algorithm needs to be at least O(n) time. Sample Run Enter a string s1: Welcome to C++ Enter a string s2: come matched at index 3
Please show fully functioning java code and outputs. Design a Java Animal class (assuming in Animal.java...
Please show fully functioning java code and outputs. Design a Java Animal class (assuming in Animal.java file) and a sub class of Animal named Cat (assuming in Cat.java file).   The Animal class has the following protected instance variables: boolean vegetarian, String eatings, int numOfLegs and the following public instance methods: constructor without parameters: initialize all of the instance variables to some default values constructor with parameters: initialize all of the instance variables to the arguments SetAnimal: assign arguments to all...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment 2 (karatsuba.cpp) in Canvas (please do not rename file or use cout/cin statements in your solution). As a reminder, the algorithm uses recursion to produce the results, so make sure you implement it as a recursive function. Please develop your code in small The test program (karatsuba_test.cpp) is also given. PLEASE DO NOT MODIFY THE TEST FILE. KARATSUBA.CPP /* Karatsuba multiplication */ #include <iostream>...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Suppose that people's heights (in centimeters) are normally distributed, with a mean of 170 and a...
    asked 5 minutes ago
  • Use the information from the following Income Statement to create and Projected Income Statement and solve...
    asked 18 minutes ago
  • An unequal tangent vertical curve has the following elements: g1=-3.25%, g2=75%, total length = 500.00’, length...
    asked 20 minutes ago
  • Please write clear definitions of the following legal terms. Commerce Clause Supremacy Clause Indictment Tort
    asked 24 minutes ago
  • Do you think Moralistic Therapeutic Deism is an accurate reflection of society today? What are relevant...
    asked 29 minutes ago
  • The mean operating cost of a 737 airplane is $2,071 per day. Suppose you take a...
    asked 38 minutes ago
  • Arguments can be made on both sides of this debate about the ethical implications of using...
    asked 44 minutes ago
  • In the Chapter, they mention the idea of strategizing around your cash flows. Why are cash...
    asked 49 minutes ago
  • Company A signed a fixed-price $6,500,000 contract to construct a building. At the end of Year...
    asked 50 minutes ago
  • An unequal tangent vertical curve has the following elements: g1=-3.25%, g2=1.75%, total length = 500.00’, length...
    asked 56 minutes ago
  • In a previous​ year, 61​% of females aged 15 and older lived alone. A sociologist tests...
    asked 1 hour ago
  • Topic: Construction - Subsurface Investigation (Note: Briefly discuss in your own words, 1 paragraph minimum.) Typically...
    asked 1 hour ago