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
Please code C# Write a method with an int return type that has two int parameters....
Please code C# Write a method with an int return type that has two int parameters. The method returns the larger parameter as an int. If neither is larger, the program returns -1. Call this method three times, once with the first argument larger, once with the second argument larger, and once with both arguments equal
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...
Please follow ALL the instructions and solve it by C++. Please and thank you! There are...
Please follow ALL the instructions and solve it by C++. Please and thank you! There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print...
IN C++ Write a program named printStringReverse.cpp to read in a C-Style sting. Print the string...
IN C++ Write a program named printStringReverse.cpp to read in a C-Style sting. Print the string forward and then backward using pointer increment and decrement. Declare the C-Style string as following: char s[50]; Read the string as follows; cin >> s; Write the following code to get the actual number of characters from your string. int count=0; while (s[count] != NULL) { count++; } count contains the actual length of your string since we declared the array to hold a...
Write a template function maxn() that takes as its arguments an array of items of type...
Write a template function maxn() that takes as its arguments an array of items of type T and an integer representing the number of elements in the array and that returns the largest item in the array. The number of elements should take the default value of 10. The program should include a specialization that takes an array of strings as an argument and returns the longest string. (If there is a tie, the function should return the first one...
Please complete the following code for challenge.c in C according to the instructions in the comments....
Please complete the following code for challenge.c in C according to the instructions in the comments. Further instructions are below in INSTRUCTIONS.txt challenge.c #include "challenge.h" // goal: fork the process and have the child execute a process // param argv: the argument vector for the process to be executed // assumptions: // the first argument of argv is the file name of the executable // argv is null terminated // // TODO: complete the function // fork // exec (child),...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output for proof. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Implement the following functions: Implement a function called readData int readData( int *arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr....
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...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using,...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and...