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.

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.
commented: 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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.