954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Homworkk.. Please help guyss !!

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.

harde
Newbie Poster
13 posts since Nov 2011
Reputation Points: 10
Solved Threads: 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.
pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
 

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
harde
Newbie Poster
13 posts since Nov 2011
Reputation Points: 10
Solved Threads: 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
harde
Newbie Poster
13 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

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.

Schol-R-LEA
Posting Pro
556 posts since Oct 2010
Reputation Points: 254
Solved Threads: 85
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: