Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I modify my code to save the pattern that I created onto the file? #inclu

ID: 675519 • Letter: H

Question

How do I modify my code to save the pattern that I created onto the file?

#include <iostream>
#include <fstream>
using namespace std;

void displayStars(int = 0, int = 0);

int main()
{
   displayStars(2,1);
   displayStars(4,1);
   displayStars(8,1);
   displayStars(16,1);
  
   ofstream outputFile;
   outputFile.open("Pattern.txt");
  
   outputFile.close();
   return 0;
}

void displayStars(int cols, int rows)
{
   for(int down = 0; down < rows; down++)
   {
   for (int across = 0; across < cols; across++)
       cout << "+";
       cout << endl;
   }
}

Explanation / Answer

#include <iostream>

#include <fstream>

using namespace std;

ofstream outputFile;

void displayStars(int = 0, int = 0);

int main()

{

  

outputFile.open("Pattern.txt");

displayStars(2,1);

displayStars(4,1);

displayStars(8,1);

displayStars(16,1);

  

outputFile.close();

return 0;

}

void displayStars(int cols, int rows)

{

for(int down = 0; down < rows; down++)

{

for (int across = 0; across < cols; across++)

{

cout << "+";

outputFile << "+";

}

cout << endl;

outputFile << endl;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote