Functions

Below is my code, I am having problems with 4,5, and 6. I am not sure how to nest a function call to determine largest and smallest for four numbers as well as I am not sure how to do a function with a running total of the four numbers Ex. my numbers are 17.243, 4.532, 12.713, 8.726 and I need #6 function to do the following ex. 17.243 21.775 34.488 43.214. I am also having trouble getting my code to read from an inFile i created.....any suggestions and help would be appreciated.....thanks!!

#include <iostream>
#include <fstream>
#include <math.h>

using namespace std;

ifstream inFile;
ofstream outFile;


// 0. read in four float numbers (pass in the four float variables by reference)
void readInput(float &num1, float &num2, float &num3, float &num4)
{
cin >> num1 >> num2 >> num3 >> num4;
}

//1. print out the four numbers and the sum of the four numbers
float sum(float num1, float num2, float num3, float num4)
{
return num1+num2+num3+num4;
}

//2. print out the difference between the first and second number and the difference between the third and fourth number
float difference(float first, float second)
{
return first-second;
}

//3. print out the first number raised to the second number power (pow function) and the fourth number raised to the third number power
float power(float first, float second)
{
return pow(first,second);
}

//4. print out the largest of the four (this function, you can only pass in two arguments at one time)
float largest(float first, float second)
{
if(first>second)
return first;
else
return second;
}

//5. print out the smallest of the four (this function, you can only pass in two arguments at one time)
float smallest(float first, float second)
{
if(first<second)
return first;
else
return second;
}

//6. print out a running total of the four number. (This function, you can only pass in two arguments at one time.)
float total(float num1, float num2, float num3, float num4)
{
return num1+num2+num3+num4;
}




int main()
{
// open inFile
inFile.open("floatnum.txt");
// return error if input file cannot be opened
if(!inFile)
{
cout<<"Error opening floatnum.txt--file not found!"<<endl;
return 1;
}
// open outFile
outFile.open("functout.txt");
// return error if outFile cannot be opened
if(!outFile)
{
cout<<"Error opening functout.txt!"<<endl;
return 1;
}


float num1, num2, num3, num4;
cout << "Enter four float numbers:" << endl;

readInput(num1,num2,num3,num4);

cout << "The four numbers are: " << " "<< num1 << " " << num2 <<" "<< num3 <<" " << num4 << endl;
outFile << "The four numbers are: " << " "<< num1 << " " << num2 <<" "<< num3 <<" " << num4 << endl;
cout << "Sum of four numbers is: " << sum(num1,num2,num3,num4) << endl;
outFile << "Sum of four numbers is: " << sum(num1,num2,num3,num4) << endl;
cout << "Difference between the first and second number is: " << difference(num1,num2) <<endl;
outFile << "Difference between the first and second number is: " << difference(num1,num2) <<endl;
cout << "Difference between the third and fourth number is: " << difference(num3,num4) <<endl;
outFile << "Difference between the third and fourth number is: " << difference(num3,num4) <<endl;
cout << "First number raised to the second number power is: " << power(num1,num2) << endl;
outFile << "First number raised to the second number power is: " << power(num1,num2) << endl;
cout << "Fourth number raised to the third number power is: " << power(num4,num3) << endl;
outFile << "Fourth number raised to the third number power is: " << power(num4,num3) << endl;
cout << "Largest of the four numbers is: " << largest(largest(num1,num2),num3) << endl;
outFile << "Largest of the four numbers is: " << largest(largest(num1,num2),num3) << endl;
cout << "Smallest of the four numbers is: " << smallest(smallest(num1,num2),num3) << endl;
outFile << "Smallest of the four numbers is: " << smallest(smallest(num1,num2),num3) << endl;
cout << "Running total of the four numbers: " << sum(num1,num2,num3,num4) << endl;
outFile << "Running total of the four numbers: " << sum(num1,num2,num3,num4) << endl;



inFile.close();
outFile.close();

}
> I am having problems with 4,5, and 6.

1
2
3
4
5
6
7
8
9
10
11
//4. print out the largest of the four 
// (this function, you can only pass in two arguments at one time)
float larger( float a, float b ) { return a>b ? a : b ; }

#include <iostream>

int main()
{
    float a = 1.2f, b = 6.7f, c = -4.2f, d = 1.7f ;
    std::cout << "largest: " << larger( larger(a,b), larger(c,d) ) << '\n' ;
}


5 and 6 are similar.
Last edited on
Thanks JLBorges I got 4 and 5. Still not sure about 6. and calculating a running total.
> Still not sure about 6. and calculating a running total.

See: http://en.wikipedia.org/wiki/Running_total

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
float sum( float a, float b ) { return a+b ; }

#include <iostream>

int main()
{
    float a = 1.2f, b = 6.7f, c = -4.2f, d = 1.7f ;

    float running_total = a ;
    std::cout << "running total a: " << running_total << '\n' ;

    running_total = sum( running_total, b ) ;
    std::cout << "running total a,b: " << running_total << '\n' ;

    running_total = sum( running_total, c ) ;
    std::cout << "running total a,b,c: " << running_total << '\n' ;

    running_total = sum( running_total, d ) ;
    std::cout << "running total a,b,c,d: " << running_total << '\n' ;
}
I understand what a running total is I am just not sure how to write the correct function with two arguments to calculate it in my program above. I have to use functions for each one above and pass in two or three floats into each function and pass back results. The main program is the only place I can do couts, no couts in any function. So I am really confused!! Below is my updated program....I just need help and clarification on 6.

#include <iostream>
#include <fstream>
#include <math.h>

using namespace std;

ofstream outFile;


// 0. read in four float numbers (pass in the four float variables by reference)
float num1 = 17.243, num2 = 4.532, num3 = 12.713, num4 = 8.726 ;


//1. print out the four numbers and the sum of the four numbers
float sum(float num1, float num2, float num3, float num4)
{
return num1+num2+num3+num4;
}

//2. print out the difference between the first and second number and the difference between the third and fourth number
float difference(float first, float second)
{
return first-second;
}

//3. print out the first number raised to the second number power (pow function) and the fourth number raised to the third number power
float power(float first, float second)
{
return pow(first,second);
}

//4. print out the largest of the four (this function, you can only pass in two arguments at one time)
float largest( float a, float b ) { return a>b ? a : b ; }


//5. print out the smallest of the four (this function, you can only pass in two arguments at one time)
float smallest ( float a, float b ) { return a<b ? a : b ; }

//6. print out a running total of the four number. (This function, you can only pass in two arguments at one time.)






int main()
{
// open outFile
outFile.open("functout.txt");
// return error if outFile cannot be opened
if(!outFile)
{
cout<<"Error opening functout.txt!"<<endl;
return 1;
}

{
cout << "Function Program" << endl;
outFile << "Function Program" << endl;
cout << "The four numbers are: " << " "<< num1 << " " << num2 <<" "<< num3 <<" " << num4 << endl;
outFile << "The four numbers are: " << " "<< num1 << " " << num2 <<" "<< num3 <<" " << num4 << endl;
cout << "Sum of four numbers is: " << sum(num1,num2,num3,num4) << endl;
outFile << "Sum of four numbers is: " << sum(num1,num2,num3,num4) << endl;
cout << "Difference between the first and second number is: " << difference(num1,num2) <<endl;
outFile << "Difference between the first and second number is: " << difference(num1,num2) <<endl;
cout << "Difference between the third and fourth number is: " << difference(num3,num4) <<endl;
outFile << "Difference between the third and fourth number is: " << difference(num3,num4) <<endl;
cout << "First number raised to the second number power is: " << power(num1,num2) << endl;
outFile << "First number raised to the second number power is: " << power(num1,num2) << endl;
cout << "Fourth number raised to the third number power is: " << power(num4,num3) << endl;
outFile << "Fourth number raised to the third number power is: " << power(num4,num3) << endl;
cout << "Largest of the four numbers is: " << largest(largest(num1,num2),largest(num3,num4)) << endl;
outFile << "Largest of the four numbers is: " << largest(largest(num1,num2),largest(num3,num4)) << endl;
cout << "Smallest of the four numbers is: " << smallest(smallest(num1,num2),smallest(num3,num4)) << endl;
outFile << "Smallest of the four numbers is: " << smallest(smallest(num1,num2),smallest(num3,num4)) << endl;

}

outFile.close();

}

Last edited on
> I am just not sure how to write the correct function with two arguments to calculate it in my program above.

http://www.cplusplus.com/forum/beginner/104070/#msg560978

Try running that program.
Topic archived. No new replies allowed.