Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can anyone help me with my c++ code. I am trying to make the output go to an out

ID: 3785321 • Letter: C

Question

Can anyone help me with my c++ code. I am trying to make the output go to an output file. I am using xcode and cannot get it to work. Can someone edit my code so it will output to a file in xcode?

#include <iostream>

#include <fstream>

#include <cmath>

using namespace std;

float readFloat()

{

float num;

cout<<"Enter the float value: ";

cin>>num;

return num;

}

void printFloat(float num)

{

cout<<num;

}

float sum(float a, float b)

{

return a + b;

}

float avg(float a[], int size)

{

float s = 0;

for(int i = 0; i < size; i++)

s += a[i];

return s / size;

}

float difference(float a, float b)

{

return abs(a - b);

}

float power(float a, int b)

{

float result = 1;

for(int i = 1; i <= b; i++)

result *= a;

return result;

}

float larger(float a, float b)

{

if(a > b)

return a;

return b;

}

float smallest(float a, float b)

{

if(a < b)

return a;

return b;

}

int main()

{

ofstream outData;

float numbers[5];

for(int i = 0; i < 5; i++)

numbers[i] = readFloat();

  

for(int i = 0; i < 5; i++)

{

printFloat(numbers[i]);

cout<<" ";

}

cout<<endl;

  

float total = 0;

for(int i = 0; i < 5; i++)

total = sum(total, numbers[i]);

cout<<"The sum of given 5 numbers is: ";

printFloat(total);

cout<<endl;

  

cout<<"The average of given 5 numbers is: "<<avg(numbers, 5)<<endl;

  

cout<<"The difference between "<<numbers[0]<<" and "<<numbers[2]<<" is "<<difference(numbers[0], numbers[2])<<endl;

  

cout<<"The difference between "<<numbers[1]<<" and "<<numbers[3]<<" is "<<difference(numbers[1], numbers[3])<<endl;

  

cout<<numbers[0]<<" raised to "<<numbers[2]<<" is: "<<power(numbers[0], numbers[2])<<endl;

  

cout<<numbers[1]<<" raised to "<<numbers[3]<<" is: "<<power(numbers[1], numbers[3])<<endl;

  

float large = numbers[0];

for(int i = 1; i < 5; i++)

large = larger(large, numbers[i]);

cout<<"The largest of 5 numbers is: "<<large<<endl;

  

float small = numbers[0];

for(int i = 1; i < 5; i++)

small = smallest(small, numbers[i]);

cout<<"The smallest of 5 numbers is: "<<small<<endl;

  

cout<<"The average of the 4 largest values is: "<<(total - small) / 4<<endl;

  

cout<<"The average of the 4 smallest values is: "<<(total - large) / 4<<endl;

  

cout<<"The running total of five numbers is: ";

total = 0;

for(int i = 0; i < 5; i++)

{

total = sum(total, numbers[i]);

cout<<total<<" ";

}

{

outData.open("Output.txt");

}

cout<<endl;

}

Explanation / Answer

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;



float readFloat()
{
float num;
cout<<"Enter the float value: ";
cin>>num;
return num;
}

void printFloat(float num)
{
cout<<num;
}

float sum(float a, float b)
{
return a + b;
}

float avg(float a[], int size)
{
float s = 0;
for(int i = 0; i < size; i++)
s += a[i];
return s / size;
}

float difference(float a, float b)
{
return abs(a - b);
}

float power(float a, int b)
{
float result = 1;
for(int i = 1; i <= b; i++)
result *= a;
return result;
}

float larger(float a, float b)
{
if(a > b)
return a;
return b;
}

float smallest(float a, float b)
{
if(a < b)
return a;
return b;
}
int main()


{
ofstream outData;
float numbers[5];
for(int i = 0; i < 5; i++)
numbers[i] = readFloat();
  
outData.open("Output.txt");
  
for(int i = 0; i < 5; i++)
{
printFloat(numbers[i]);
cout<<" ";
outData << numbers[i] << " " ;
}
cout<<endl;
  
float total = 0;
for(int i = 0; i < 5; i++)
total = sum(total, numbers[i]);
cout<< endl << "The sum of given 5 numbers is: ";
printFloat(total);
cout<<endl;
outData << "The sum of given 5 numbers is: " << total << endl;
  
cout<<"The average of given 5 numbers is: "<<avg(numbers, 5)<<endl;
outData <<"The average of given 5 numbers is: "<<avg(numbers, 5)<<endl;
  
cout<<"The difference between "<<numbers[0]<<" and "<<numbers[2]<<" is "<<difference(numbers[0], numbers[2])<<endl;
outData <<"The difference between "<<numbers[0]<<" and "<<numbers[2]<<" is "<<difference(numbers[0], numbers[2])<<endl;
  
cout<<"The difference between "<<numbers[1]<<" and "<<numbers[3]<<" is "<<difference(numbers[1], numbers[3])<<endl;
outData <<"The difference between "<<numbers[1]<<" and "<<numbers[3]<<" is "<<difference(numbers[1], numbers[3])<<endl;
  
cout<<numbers[0]<<" raised to "<<numbers[2]<<" is: "<<power(numbers[0], numbers[2])<<endl;
outData <<numbers[0]<<" raised to "<<numbers[2]<<" is: "<<power(numbers[0], numbers[2])<<endl;
  
cout<<numbers[1]<<" raised to "<<numbers[3]<<" is: "<<power(numbers[1], numbers[3])<<endl;
outData <<numbers[1]<<" raised to "<<numbers[3]<<" is: "<<power(numbers[1], numbers[3])<<endl;
  
float large = numbers[0];
for(int i = 1; i < 5; i++)
large = larger(large, numbers[i]);
cout<<"The largest of 5 numbers is: "<<large<<endl;
outData <<"The largest of 5 numbers is: "<<large<<endl;
  
float small = numbers[0];
for(int i = 1; i < 5; i++)
small = smallest(small, numbers[i]);
cout<<"The smallest of 5 numbers is: "<<small<<endl;
outData <<"The smallest of 5 numbers is: "<<small<<endl;
  
cout<<"The average of the 4 largest values is: "<<(total - small) / 4<<endl;
outData <<"The average of the 4 largest values is: "<<(total - small) / 4<<endl;
  
cout<<"The average of the 4 smallest values is: "<<(total - large) / 4<<endl;
outData <<"The average of the 4 smallest values is: "<<(total - large) / 4<<endl;
  
cout<<"The running total of five numbers is: ";
outData <<"The running total of five numbers is: ";
  
total = 0;
for(int i = 0; i < 5; i++)
{
total = sum(total, numbers[i]);
cout<<total<<" ";
outData <<total<<" ";
}
  
outData.close();
cout<<endl;
}

/*Output.txt

1 2 3 4 5 The sum of given 5 numbers is: 15
The average of given 5 numbers is: 3
The difference between 1 and 3 is 2
The difference between 2 and 4 is 2
1 raised to 3 is: 1
2 raised to 4 is: 16
The largest of 5 numbers is: 5
The smallest of 5 numbers is: 1
The average of the 4 largest values is: 3.5
The average of the 4 smallest values is: 2.5
The running total of five numbers is: 1 3 6 10 15

Explanation

cout writes to console, in the same way we need to write to file like outData << "Something here"

Happy Coding :)

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote