Hello,
I seem to be having a newbie problem with writng a simple program consisting of 3 functions. Here is what's being asked of me:
Your main function should call the function printMyName which will print out your name and a description of the program (see details below)
Next, in a while loop you should do the following:
read in three integers
print out all three numbers to your output file (hw3.out).
call the function findLargest, which will return the largest of the 3 values.
call the function findAverage which will return the average of the three numbers.
print out the largest and average of the three numbers.
Do this loop again i.e. print a nice message asking "Continue(y or n)?"
Here is the description of the three functions:
printMyName - This function will not return anything and will have no parameters. It will print your name and a description of the program to your Output File.
findLargest - will take in three integer parameters, and return an integer, which is the largest of the three numbers.
findAverage - will take in three integer parameters, and return a double, which is the average of the three numbers.
So far I managed to make it work in primitive way without any functions, please help me modify this into functions the way Im being asked to do:
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
int main()
{
int a, b, c, max, average;
char resume;
while (resume != 'n'){ //After 3 integers are read; user enters 'n' to end loop
cin >> a;
cin >> b;
cin >> c;
double average = (a + b + c) / 3.0;
if ( a > b ){
max = a;
}
else{
max = b;
}
if ( c > max ){
max = c;
}
cout << "The Largest Number is: " << max << endl;
cout << "The Average is: " << average << endl;
cout << "Do you want to continue (y/n)?\n";
cin >> resume; //User enters 'n' to end loop. or 'y' to continue.
ofstream outfile("hw3.ou t"); //sends outfile info to file "hw3.out"
outfile << "name" << endl;
outfile << "This program reads 3 integers, finds largest and average, then sends information to file" << endl;
outfile << a << " " << b << " " << c << endl;
outfile << "The Largest Number is: " << max << endl;
outfile << "The Average is: " << average << endl;
outfile.close() ;
}
return 0;
}
Any help is appreciated.
I seem to be having a newbie problem with writng a simple program consisting of 3 functions. Here is what's being asked of me:
Your main function should call the function printMyName which will print out your name and a description of the program (see details below)
Next, in a while loop you should do the following:
read in three integers
print out all three numbers to your output file (hw3.out).
call the function findLargest, which will return the largest of the 3 values.
call the function findAverage which will return the average of the three numbers.
print out the largest and average of the three numbers.
Do this loop again i.e. print a nice message asking "Continue(y or n)?"
Here is the description of the three functions:
printMyName - This function will not return anything and will have no parameters. It will print your name and a description of the program to your Output File.
findLargest - will take in three integer parameters, and return an integer, which is the largest of the three numbers.
findAverage - will take in three integer parameters, and return a double, which is the average of the three numbers.
So far I managed to make it work in primitive way without any functions, please help me modify this into functions the way Im being asked to do:
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
int main()
{
int a, b, c, max, average;
char resume;
while (resume != 'n'){ //After 3 integers are read; user enters 'n' to end loop
cin >> a;
cin >> b;
cin >> c;
double average = (a + b + c) / 3.0;
if ( a > b ){
max = a;
}
else{
max = b;
}
if ( c > max ){
max = c;
}
cout << "The Largest Number is: " << max << endl;
cout << "The Average is: " << average << endl;
cout << "Do you want to continue (y/n)?\n";
cin >> resume; //User enters 'n' to end loop. or 'y' to continue.
ofstream outfile("hw3.ou t"); //sends outfile info to file "hw3.out"
outfile << "name" << endl;
outfile << "This program reads 3 integers, finds largest and average, then sends information to file" << endl;
outfile << a << " " << b << " " << c << endl;
outfile << "The Largest Number is: " << max << endl;
outfile << "The Average is: " << average << endl;
outfile.close() ;
}
return 0;
}
Any help is appreciated.
Comment