0

The program should calculate the average of the values stored in the rates array. It then should display the average rate on the screen. Display the average with two decimal places. Complete the program using the for statement. Save and then run the program. Complete the program and also have it write the average output to an output file opened in append mode.

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

int main()
{
	double rates[5][3] = {{3.4, 56.7, 8.99}, 
						  {11.23, 4.67, 85.4},
					 	  {34.6, 2.4, 9.0},
						  {6.3, 8.0, 4.1},
						  {4.0, 2.0, 3.5}};


	system("pause");
	return 0;
}	//end of main function

Please guyss helpp.. last program of the class. Thank you.

3
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by Schol-R-LEA
0

Last program of the class and you can't construct a for loop?

//Syntax:
//for( ; ; )
//for( <initilization, normally a loop-control variable>; <test expression>; <increment/decement> )
//As such it follows that:
for(int i = 0; i < 100; i++)
   cout << i << endl;
//is a valid count-controlled (with the loop-control variable) for-loop.
Votes + Comments
Thanks man
0

So im done with my program.. Next is "Complete the program and also have it write the average output to an output file opened in append mode."

Not sure how to do that.. need help again please !!

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

int main()
{
	
	// declare array and variable
	double rates[5][3] = {{3.4, 56.7, 8.99}, 
						  {11.23, 4.67, 85.4},
					 	  {34.6, 2.4, 9.0},
						  {6.3, 8.0, 4.1},
						  {4.0, 2.0, 3.5}};
	double total = 0.0;
	double average = 0.0;
	
for(int row = 0; row < 5; row +=1)  
	for (int col = 0; col < 3; col +=1)
		total += rates[row][col];
average = total/15;


 
//dislay average 
cout << fixed << setprecision(2);
cout << "Average: $" << average << endl;



	system("pause");
	return 0;
}	//end of main function
0

This is how i did it and its working too.. but i still want someone to review it for me ? ? ?

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

int main()
{
	
	// declare array and variable
	double rates[5][3] = {{3.4, 56.7, 8.99}, 
						  {11.23, 4.67, 85.4},
					 	  {34.6, 2.4, 9.0},
						  {6.3, 8.0, 4.1},
						  {4.0, 2.0, 3.5}};
	double total = 0.0;
	double average = 0.0;
	
	fstream file;
	file.open("output.txt", ios::out);

for(int row = 0; row < 5; row +=1)  
	for (int col = 0; col < 3; col +=1)
		total += rates[row][col];
average = total/15;

 
//dislay average 
cout << fixed << setprecision(2);
cout << "Average: $" << average << endl;

	

//writing to output file
file << "Average: $" << average << endl;



 file.close();

 cout << "File was succesfully written! \n";

	system("pause");
	return 0;
}	//end of main function
1

One thing I noticed (aside from your indentation, which needs improving) is that the project said to set the file to append mode. Thus, you should change the open() call to:

file.open("output.txt", ios::out | ios:app);

This ensures that the data is added to the end of the file, rather than overwriting the data already there.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.