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?
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
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
Get Answers For Free
Most questions answered within 1 hours.