I would like to program that reads a file with 6 lines that have 7 numbers on each line. The output should save a file with sum, average, max and min of each line. For now I just have to figure out how to get the max and min numbers the robust way. For starters this is my code. (please note that my goal for now is just to sort my 7 entered numbers)

My code is not working and my total is wrong. Please advise.

//Program utilizing one file for input and one file for output

#include <iostream>

using namespace std;

int main()
{
	int num1, num2, min = 0, max = 0, sum = 0, count = 0;
	double avg;
	
	cin >> num1 >> num2;
	for (count = 0; count <= 7; count++)
	{
		
		if (num1 > num2) max = num1, min = num2; 
		if (num1 < num2) max = num2, min = num1;
		else max = num1, min = num2;
		sum = sum + min + max;
	}
	avg = sum/7.0;

	cout << min << " & " << max << endl;
	cout << "Sum = " << sum << endl;
	cout << "Average = " << avg << endl;
	system ("pause");
	return 0;
}

First of all, you read two numbers and never read any more.

Try this:
Read the first number. Set min and max to this number. This seeds the min/max with at least one number actually in the list.
Set up a loop to read the rest of the numbers one at a time and test for min and max.

How do I assign the first number to min and max? Is this correct?

for (count = 0; count <= 7; count++)
{
  cin >> max;
  max = min;
  if (max > min) max = max;
  if (max < min) max = min;
  sum = sum + min + max;
}

Thanks!

I thought you had to read from a file...

Either way, that's not correct. You need to input to some input variable then store the value to max and min, not input directly to max or min:

inFile >> someVar;
min = someVar;
max = someVar;
while (inFile >> someVar) {
  if (/*fill me in*/) {
    /*fill me in*/
  }
  if (/*fill me in*/) {
    /*fill me in*/
  }
  /*fill me in*/
}

If you don't use a third variable, you can't compare the new values to decide if they actually are your new max/min. As a result, they will get corrupted on every iteration of your loop and inaccurate results are pretty much guaranteed.

The example I gave you will work for 1 set of data. It is your responsibility to figure out how to adapt it to multiple sets of data using techniques you have already learned and what you already know about the data.

Edited 6 Years Ago by Fbody: n/a

Ok so here's my code so far, for some reason the output is not working properly. My goal out put is:

Numbers on Line 1:
346 130 982 90 656 117 595
Highest number is: 982
Lowest number is: 90
Sum = 2916
Average = 416.57

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
	ifstream infile;
	ofstream outfile;
	int num, min, max, sum = 0;
	double avg;
	
	infile.open ("C:\\Users\\garu525\\Desktop\\5A_Data.rbh");
	if (!infile)
	{
		cout << "File not found." << endl;
		exit(1);
	}

	outfile.open ("C:\\Users\\garu525\\Desktop\\5A_Log.rbh");

	infile >> num;

	while (!infile.eof())
	{	
		for (int count = 0; count <=7; count++)
		{	
			infile >> num;
			min = num; 
			max = num;
		
			while (infile >> num)
			{
				if (num > max) max = num;
				if (num < min) min = num;
			}
		}
		sum += num;
		outfile << num << " ";
	}
	
	

	avg = sum / 7.0;

	outfile << "Highest number is: " << max << endl;
	outfile << "Lowest number is: " << min << endl;
	outfile << "Sum = " << sum << endl;
	outfile << "Average = " << avg << endl;

	infile.close();
	outfile.close();
	return 0;
}

BTW, this is what's inside the text file...

346 130 982 90 656 117 595
415 948 126 4 558 571 87
42 360 412 721 463 47 119
441 190 985 214 509 2 571
77 81 681 651 995 93 74
310 9 995 561 995 93 74
466 664 892 8 766 34 639
151 64 98 813 67 834 369

Thanks in advance!

There are some errors. The outer while loop ...

while (!infile.eof()
{
.
.
}

will iterate through all the lines of your file. So the values of max, min and sum after the loop will be from the numbers of last line. You lost all the max, min and sum for the previous lines.

The inner for loop is not correct too. Before the outer while loop, you took a number from infile in num and then without using it, you are taking another number in num from infile at the beginning of for loop. So you are loosing the first number from your calculations.

for (int count = 0; count <=7; count++)
		{	
			infile >> num; //You already have a number from line 1 in num
			min = num; 
			max = num;
		
			while (infile >> num)
			{
				if (num > max) max = num;
				if (num < min) min = num;
			}
		}

Also, your logic for finding max and min is incorrect. The for loop is running 8 times to take 7 numbers from the line. And what is the inner while loop doing?

Are you familiar with string streams? If you are, I suggest you use an outer loop with a line-based input function (such as getline()) to read the file one (1) line at a time into an input string stream. Then, use an inner loop to extract your values from the string stream for analysis.

Thank you FBody and vdit X, I will try both your suggestions.

Here's my code so far. I got what I wanted it works so far, except the minimum number on my line 1 output.

Here's my output file:

Numbers on line 1:
346 130 982 90 656 117 595
Highest number is: 982
Lowest number is: -858993460 Should be 90
Sum = 2916
Average = 416.571


Numbers on line 2:
415 948 126 4 558 571 87
Highest number is: 948
Lowest number is: 4
Sum = 2709
Average = 387


Numbers on line 3:
42 360 412 721 463 47 119
Highest number is: 721
Lowest number is: 42
Sum = 2164
Average = 309.143


Numbers on line 4:
441 190 985 214 509 2 571
Highest number is: 985
Lowest number is: 2
Sum = 2912
Average = 416


Numbers on line 5:
77 81 681 651 995 93 74
Highest number is: 995
Lowest number is: 74
Sum = 2652
Average = 378.857


Numbers on line 6:
310 9 995 561 995 93 74
Highest number is: 995
Lowest number is: 9
Sum = 3037
Average = 433.857


Numbers on line 7:
466 664 892 8 766 34 639
Highest number is: 892
Lowest number is: 8
Sum = 3469
Average = 495.571


Numbers on line 8:
151 64 98 813 67 834 369
Highest number is: 834
Lowest number is: 64
Sum = 2396
Average = 342.286

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
	ifstream infile;
	ofstream outfile;
	int num[7], min, max, sum = 0, counter;
	double avg;
	
	infile.open ("C:\\Users\\garu525\\Desktop\\5A_Data.rbh");
	if (!infile)
	{
		cout << "File not found." << endl;
		exit(1);
	}

	outfile.open ("C:\\Users\\garu525\\Desktop\\5A_Log.rbh");

	for (int row = 1; row <= 8; row++) 
	{
		counter = 0;
		max = num[counter];
		min = num[counter];
		
		for (; counter < 7; counter++)
		{
			infile >> num[counter];
			if (min > num[counter]) min = num[counter];
			if (max < num[counter]) max = num[counter];
		}
		sum = num[0] + num[1] + num[2] + num[3] + num[4] + num[5] + num[6];
		avg = sum / 7.0;
			
		outfile << "Numbers on line " << row << ":"<< endl;
		outfile << num[0] << " " << num[1] << " " << num[2] << " " << num[3] << " " << num[4] << " " << num[5] << " " << num[6] << endl;
		outfile << "Highest number is: " << "\t" << "\t" << max << endl;
		outfile << "Lowest number is: " << "\t" << "\t" << min << endl;
		outfile << "Sum = " << "\t" << "\t" << "\t" << "\t" << sum << endl;
		outfile << "Average = " << "\t" << "\t" << "\t" << avg << endl << endl << endl;
	}
		
	infile.close();
	outfile.close();
	return 0;
}

What am I doing wrong this time. Please help my head hurts!

THanks!

max = num[counter];
min = num[counter];

You are assigning values to max and min from an un-initialized variable and that is the problem. The reason its working with other lines is after the first iteration, the num array gets intialized with the values.

What you can try is .. get a number from the file in the outer for loop, store it in max and min, leave inner for loop as it is and it should work.

Edited 6 Years Ago by vidit_X: n/a

This question has already been answered. Start a new discussion instead.