0

Good Afternoon,

I 'm new to C++ programming, and I have to do a program that deals with nested loops. Can anyone please check the code that I have for PART C and PART D of the program. Here is the code of the program.

```
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <limits.h> //changed by sirisha
using namespace std;
int main( )
{
//input file objects
ofstream outFile; //output file
ifstream inFile; //input file
//variable declarations
int number; //var to store an int value
int sum, min, max, avg; //vars to store simple statistical info
int counter; //a counter for controling the loop
int size; //another counter
int row, col; //loop variable for rows and columns
/************************************************************
Part A. Create a data file data.txt in range of 0 to 9999
As assume 10 lines, each line with 8 random numbers
in the range
************************************************************/
//use two nested for-loops to create the data file
//open data.txt
outFile.open("data.txt");
if (outFile.fail())
{
cout<<"failed to open data.txt so break ...... "<<endl;
return 1;
}
for (row = 0; row < 10; row++) //outer loop for rows/lines
{
srand(row); //set random seed
for (col = 0; col < 8; col++) //inner loop for columns/number per row
{
outFile <<setw(10)<<right<< rand()%10000; //generate a random number between 0 and 9999
}
if (row != 9) //move to next line
outFile <<endl; //but do not move at the last line
}
//close outFile
outFile.close();
/************************************************************
Part B. Use two nested for-loops to process data.txt
************************************************************/
cout<<" Use two nested while-loops to process data.txt "<<endl;
//open data.txt for input
inFile.open("data.txt");
for (row = 0; row < 10; row++) //outer loop to process rows/lines
{
//initialization
sum=counter=max=avg =0;
min=INT_MAX; //min has the maximum integer valued assigned//changed by sirisha
size = 8;
for (col = 0; col < 8; col++) //inner loop to process column/numbers in a row
{
inFile>>number; //get next number
sum += number;
if (number > max)
max = number;
if (number < min)
min = number;
}
//show stats for each row
cout<<" \nStatistical info about row " << row +1 <<" is "<<endl
<<" \t The sum is " << setw(10)<<right<<sum << endl
<<" \t The min is " << setw(10)<<right<<min << endl
<<" \t The max is " << setw(10)<<right<<max << endl
<<" \t The Avg is " << setw(10)<<right
<<setprecision(4)<<fixed<<showpoint
<<static_cast<double> (sum) / static_cast<double> (size) << endl;
}
/*******************************************************************
Part C. This is for you: Re-do Part B with two nested while-loops
*******************************************************************/
sum=counter=max=avg =0;
min=INT_MAX;
size = 8;
cout<<" Use two nested while-loops to process data.txt "<<endl;
inFile.open("data.txt");
while(row < 0)
{
while(col < 0)
{
inFile>>number;
sum += number;
if (number > max)
max = number;
if (number < min)
min = number;
}
cout<<" \nStatistical info about row " << row +1 <<" is "<<endl
<<" \t The sum is " << setw(10)<<right<<sum << endl
<<" \t The min is " << setw(10)<<right<<min << endl
<<" \t The max is " << setw(10)<<right<<max << endl
<<" \t The Avg is " << setw(10)<<right
<<setprecision(4)<<fixed<<showpoint
<<static_cast<double> (sum) / static_cast<double> (size) << endl;
}
/*********************************************************************
Part D. This is for you: Re-do Part B with two nested do-while-loops
*********************************************************************/
cout<<" Use two nested while-loops to process data.txt "<<endl;
inFile.open("data.txt");
sum=counter=max=avg =0;
min=INT_MAX;
size = 8;
do
{
inFile>>row;
row += number;
do
{
inFile>>number;
sum += number;
if (number > max)
max = number;
if (number < min)
min = number;
}while(col < 0);
cout<<" \nStatistical info about row " << row +1 <<" is "<<endl
<<" \t The sum is " << setw(10)<<right<<sum << endl
<<" \t The min is " << setw(10)<<right<<min << endl
<<" \t The max is " << setw(10)<<right<<max << endl
<<" \t The Avg is " << setw(10)<<right
<<setprecision(4)<<fixed<<showpoint
<<static_cast<double> (sum) / static_cast<double> (size) << endl;
}while(row < 0);
//well done and exit
return 0;
}
```