C Download the program starscpp Read the program and try to

(C++) Download the program stars.cpp Read the program and try to understand what it does. Compile and run it as well to see what it does: in the program, the function, boxOfStars, returns a string that, when printed, yields a box of stars of width w and height h. The function, lineOfStars, is a function that returns a string that when printed, yields a string containing a sequence of stars of length len, without a new line character. Following the example from the textbook write a new definition of the function boxOfStars that does NOT contain an explicitly nested loop. Instead, it should have a call to the function lineOfStars to accomplish the same goal as the original program. You can test what you’ve done by compiling and running your changed code. Write the body of your program in the space provided below.

Stars.cpp program below.

Uncomment the code between lines 40 and 45 in the original stars.cpp code provided to you. Then compile and run the program. What do you think happens to the variables ‘width’ and ‘height’ when the function swap() is called? Draw pointer diagrams to show how the function swap achieves that outcome.

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
string lineOfStars(int width);
string boxOfStars(int width, int height);
void swap(int * p1, int *p2);
int main(int argc, char *argv[]){
if(argc !=3){
cerr<<\"Usage : \"<<argv[0]<< \" width height\"<<endl;
exit(1);
}
int width = atoi(argv[1]);
int height = atoi(argv[2]);
string myLineOfStars(lineOfStars(width));
cout<<\"Printing a line of stars \"<<endl;
cout<<myLineOfStars;
cout<<endl;
string myBoxOfStars(boxOfStars(width,height));
cout<<\"Printing a box of stars with width \"<<width<<\" and \"
<<\"height \"<<height<<endl;
cout<<myBoxOfStars;
cout<<endl;
//Uncomment the following code to answer question 7 on hw7
/*
swap(width, height);
myBoxOfStars = boxOfStars(width,height);
cout<<\"Printing box of stars with width \"<<width<<\" and \"
<<\"height \"<<height<<endl;
cout<<myBoxOfStars;
*/
return 0;
}
string boxOfStars(int width, int height){
string result=\"\";
for (int i=0; i<height; i++){
for (int j=0; j<width; j++){
result+=\"*\";
}
result+=\"\ \";
}
return result;
}
string lineOfStars(int width){
string result=\"\";
for (int j=0; j<width; j++){
result+=\"*\";
}
return result;
}
//What does this function do?
void swap(int *p1, int *p2){
int tmp;
tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}

Solution

#include <iostream>
   #include <string>
   #include <cstdlib>
  
   using namespace std;
  
   string lineOfStars(int width);
  
   string boxOfStars(int width, int height);
  
   void swap(int * p1, int *p2);
  
   int main(int argc, char *argv[]){
  
   if(argc !=3){
   cerr<<\"Usage : \"<<argv[0]<< \" width height\"<<endl;
   exit(1);
   }
   int width = atoi(argv[1]);
   int height = atoi(argv[2]);
   string myLineOfStars(lineOfStars(width));
   cout<<\"Printing a line of stars \"<<endl;
   cout<<myLineOfStars;
   cout<<endl;
  
   string myBoxOfStars(boxOfStars(width,height));
   cout<<\"Printing a box of stars with width \"<<width<<\" and \"
   <<\"height \"<<height<<endl;
   cout<<myBoxOfStars;
  
   cout<<endl;
   //Uncomment the following code to answer question 7 on hw7
   /*
   swap(width, height);
   myBoxOfStars = boxOfStars(width,height);
  
   cout<<\"Printing box of stars with width \"<<width<<\" and \"
   <<\"height \"<<height<<endl;
   cout<<myBoxOfStars;
   */
   return 0;
   }
  
  
   string boxOfStars(int width, int height){
   string result=\"\";
   for (int i=0; i<height; i++){
   result+= lineOfStars(width); //call lineOfStars() function with parameter width
   result+=\"\ \";   //after printing each line print in new line
   }

   result+=\"\ \"; //new line at the end
  
   return result;
   }

  
   string lineOfStars(int width){
   string result=\"\";
   for (int j=0; j<width; j++){
   result+=\"*\";
   }
   return result;
   }
  
   //What does this function do?
   void swap(int *p1, int *p2){
   int tmp;
   tmp = *p1;
   *p1 = *p2;
   *p2 = tmp;
  
   }

output:

sh-4.2$ stars 3 4

Printing a line of stars

***

Printing a box of stars with width 3 and height 4

***

***

***

***

By uncommenting the code between lines 40 and 45 in the original stars.cpp and compiling and running ,the output is:

Printing a box of stars with width 4 and height 3

****

****

****

the variables width and height are swapped or interchanged by calling the swap function.

      tmp = *p1; // temp = width
   *p1 = *p2; //width = height
   *p2 = tmp; //height = temp

address p1of width address p2 of height
width height temp
(C++) Download the program stars.cpp Read the program and try to understand what it does. Compile and run it as well to see what it does: in the program, the fu
(C++) Download the program stars.cpp Read the program and try to understand what it does. Compile and run it as well to see what it does: in the program, the fu
(C++) Download the program stars.cpp Read the program and try to understand what it does. Compile and run it as well to see what it does: in the program, the fu
(C++) Download the program stars.cpp Read the program and try to understand what it does. Compile and run it as well to see what it does: in the program, the fu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site