Hi,
I am in a C++ class and am having trouble piecing together a program that does the following:
(C++)
Read 6 Integers from File, find sum, find average, and find Min/Max average.
I borrowed some code from other forums that had similar programs, but obviously it doesn't match my needs specifically.
Here is the assignment:
I am in a C++ class and am having trouble piecing together a program that does the following:
(C++)
Read 6 Integers from File, find sum, find average, and find Min/Max average.
I borrowed some code from other forums that had similar programs, but obviously it doesn't match my needs specifically.
Here is the assignment:
"Part 1.
1. (Use a while-loop) Write a function called AVE with (input) parameter FileName and return a return value that is the number of integers in the file
Open an existing file (file name: a3.txt). You need to use standard ways to check if the file exists. There are a number of integers in this file. Each integer takes one line.
2) Read from the file, one integer at a time
3) Add all into a single integer TOTAL
4) Calculate the average by dividing TOTAL by the number of integers in the file; that results the Average
5) Print out the Average
6) Close the file.
7) Return Count; that is the number of the integers in the input file
Part 2.
Write a function called MAXMIN with (input) parameter FileName and an integer Count, and return a return average of MAX and MIN:
1) Open an existing file FileName. You need to use standard ways to check if the file exists. There are a number of integers in this file. Each integer takes one line.
2) Read from the file, one integer at a time
3) Find the MAX of all integers
4) Find the MIN of all integers
5) Print out MAX
6) Print out MIN
7) Calculate the average of MAX and MIN
8) Print the average
9) Close the file.
10) Return the average in step 7.
In main program, define the file name (FileName) as a3.txt. Call functions AVE and MAXMIN.
The input file a3.txt is read-only. Your program should not change its contents."
Code:
//******************************************************************************************
// This program calculates the sum, average.
// By Cory Woolf
//******************************************************************************************
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cctype>
#include <fstream>
#include <stdio.h>
using namespace std;
main ()
{
while (inFile >> num)
{
if (count3 == 0)
{
max = num;
min = num;
}
if (num > max)
{
max = num;
}
if (num < min)
{
min = num;
}
count3++;
sum += num;
totSum += num;
count1++;
count2++;
}
int main()
{
float Max, Min, num;
int i;
FILE *fi;
FILE *fo;
if ((fi = fopen("a3.txt")) == 0)
{
printf("error opening file\n");
return 1;
}
fscanf(fi, %f, & Max);
Min = Max;
for (i = 0; i < 8; i ++)
{
if (num > Max)
{
Max = num;
}
if (Temp < Min)
{
Min = num;
}
}
fclose(fi);
if ( (fo=fopen("a3.txt")) == NULL )
{
printf("ERROR opening file\n");
return 1;
}
cout << "The minimum value found is %f" << Min;
cout << "The maximum = " << Max;
fclose(fo);
return 0;
}
system ("Pause");
return 0;
}