Separate scores into rows

Hello, what i'm supposed to do is open a file with some scores in it, and then:
1. Find the average
2. Find the minimum and maximum score
3. Display scores in rows of three.

What I have so far only lists the scores. Like so:

The scores are: 1
The scores are: 2
The scores are: 3

etc, until it lists all the scores. I want it to list them like so:

The scores are:
1 2 3
4 5 6
7 8 9

I want to know how to create 3 columns with an unknown number of rows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <fstream>
using namespace std;

void main()
{
	string dataFile("Scores.txt");
	ifstream fin;

	fin.open(dataFile);

	double n(0), ns(0);

	fin >> n;

	while (! fin.eof())
	{
		++ns;
		cout<< "The scores are: " << n << endl;

		fin >> n;
	}

	fin.close();
}
closed account (48T7M4Gy)
Count the scores as they are read in. Print each one. For every 3rd score add a << endl
One way you can think about printing the numbers is to continuously print them horizontally, without any line breaks. Then, keep track of how many numbers you have printed and for every third number, print a line break.

So at first, you would print the numbers as:

1 2 3 4 5 6 7 8 9 10 11 12 ...

Then add a counter and if the counter % 3 == 0, then output an endl:

1 2 3
4 5 6
7 8 9
10 11 12
...
squished18:

I can't input any numbers, they must be read from the file.
It doesn't matter if the numbers are inputted or read from a file.

First, print out all the numbers horizontally as they are being read.

Then add the counter that puts in an endl after every three numbers.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <fstream>
using namespace std;

void main()
{
	string dataFile("Scores.txt");
	ifstream fin;

	fin.open(dataFile);

	double n(0), ns(0);
        int counter = 0; ////

	fin >> n;

        cout<< "The scores are: " << endl; ////

	while (! fin.eof())
	{
                ++ns;
		cout<< setw(6)<< n ; ////

                if (counter % 3 == 0) ////
                   cout << endl; ////

                counter ++; ////

		fin >> n;
	}

	fin.close();
}
Last edited on
squished18

Can you please, please, please show how to add the counter? Please, my teacher doesn't teach, and the book is not helping, and my teacher is not at school on Mondays, and it's due Tuesday, and I just want to cry because I have no idea of what I'm doing. Please help me.
squished18

Ok, thanks!! I have a problem though, the first number doesn't align with the rest of the numbers.

So it looks like

1
1 2 5
5 7 8
1 4 0
6 2

How do i fix that? Also do you know how to find the maximum and minimum? I really appreciate your help.
closed account (48T7M4Gy)
the first number doesn't align with the rest of the numbers

Make the necessary adjustments.

how to find the maximum and minimum

Get the computer to look at the numbers. The maximum will be the biggest and the minimum will be the smallest.
:-)
kemort

Well I know that, but the adjustments that I've tried don't work.

I was never taught how to tell the computer to find the smallest or the biggest number.

I'm not the one to ask questions until I've tried a lot. I've been working on this homework since 4:00, it's 10:40 now!!! I need help.
Initialize counter to 1 instead of 0.
squished18

Thank you, thank you, thank you!!!!
closed account (48T7M4Gy)
I'm not the one to ask questions until I've tried a lot.

I'd believe that if you showed us your attempts because so far, I am sorry to say, with the suggestions you have been given by a couple of us, including the code, you just want someone to write your assignment for you.


kemort

This is what I had earlier.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <fstream>
using namespace std;

void main()
{
	const int ARRAY_SIZE = 3;
	int numbers[ARRAY_SIZE];
	string dataFile("Scores.txt");
	ifstream fin;

	fin.open(Scores);

	int n(0), ns(0);

	fin >> n;

	while (! fin.eof() )
	{
		++ns;

		cout <<  The scores are: << n << ARRAY_SIZE << endl;

		fin >> n;
	}

	fin.close();

}//End 


That didn't work. But whatever, think whatever you want. As long as I know I really need the help, and that I've been trying since 4, I'm ok.
closed account (48T7M4Gy)
But whatever, think whatever you want.


I will and have. You've just confirmed it.
For finding min and max, start by declaring a variable to store the value for each. You'll want to initialize each variable with an appropriate value.

As you read in each number from the file, compare that value with the value stored in the variable. If the new value is less than min, then update min to the new value. If the value is greater than max, then update max to the new value. That's it.
Squished18

Thank you.

Kemort

Thanks for your help earlier.
Topic archived. No new replies allowed.