cin pairs into an array

Would like to read in pairs of follows and add them per value:
The program runs, but doesn't add and throws out an exception.

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
34
35
36
37
38
39
40
41
42
43
#include "stdafx.h"
#include <iostream>

using namespace std;
const int AMAX = 51;

	
 
int main()
{

	int myarray[51] = { 0 };
	int score = 0;
	int times = 0;
	int count = 0;




	while (true)
	{
		if (score != -1)
		{
			cin >> score >> times;
			myarray[score] = times++;
		}
		else {
			break;
		}


	}



	for (int i = 1; i < AMAX; i++)
	{
		cout << "Value:  " << i << ": " <<  myarray[i] << "\n";
	}

}

Could you give an example of a typical input and the expected output please.

This description, "Would like to read in pairs of follows and add them per value:" made me think you wanted to do something like this:
1
2
3
4
5
6
7
8
9
int a = 0;
int b = 0;

while (count < AMAX)
{ 
    cin >> a >> b;
    myarray[count] = a + b;
    count++;
}


... but I'm not sure if that is or is not what you want.
Chervil,

Thanks for the reply! That's close, but what about a loop that takes input until the user enters -2. Example, user input 40 4 40 9 41 5 41 5
The output would display subscript 40 13 and subscript 41 10

I tried myarray[score] = times + times, but it's not added, but printing the last value.
You need to add the value to the existing array contents.

 
    myarray[score] = myarray[score] + times;
or simply
 
    myarray[score] += times;

Thanks! That was too easy. Figured it was simple.
However, that still leaves the main loop, and how it is terminated.
This line: if (score != -1) is dangerous because it is testing the previous value of score. When that condition is true, the program has already executed the line which modified array element myarray[-1].

Out-of-bounds array access like this gives results which are undefined - the program could crash or some other data be corrupted and so on.

suggestion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    int myarray[AMAX] = { 0 };
    int score = 0;
    int times = 0;

    while (cin >> score && score >= 0)
    {
        if (score >= AMAX)
        {
            cout << "out of range\n";
        }
        else
        {
            cin >> times;
            myarray[score] += times;
        }
    }
Last edited on
Great! Things are coming along.
Here's what I am using for a vertical chart or bar chart:

for (int i = 0; i < AMAX; i++)
{
cout << i << ": " ;
for (int k = 0; k < myarray[i]; k++)
{
cout << "*";
}

cout << "\n";


}
That example shows a count using * for each value, but need a little help with a horizontal chart.
Because of the way that we read and write (line-by-line) that's the way the console output is generated, making it easy to print lines of asterisks.

Below, gives the same result as your code, but using a std::string to create the row of asterisks.
1
2
3
4
    for (int i = 0; i < AMAX; i++)
    {
        cout << setw(2) << i << ": " << string(myarray[i], '*') << '\n';
    }


To do something similar, but displaying vertical lines of asterisks instead of horizontal strings, takes a little bit more effort.

The first thing to do is to determine how many lines of output will be needed. That is (unless applying a scaling factor) find the maximum value stored in the array myarray.

Then you could use a 2D character array with height = the max just found, and the width = AMAX. Fill it with spaces. Place asterisks in vertical columns of the array according to the values found in myarray. Finally, print the entire array, line by line.

But you don't really need a 2D array to do this. Instead, just loop for the number of rows required (max value from the array). Then inside that loop, have another loop, which will print either a space or an asterisk depending on the value found in myarray for that column.

I don't think I'm explaining things very well. See if that makes any sense.
Chervil,

Thank you for the feedback. If I need to find the highest grade, I know to do the for loop and assign the value each time and reassign.

for (i = 0; array[i]<AMAX;i++)
{
if (smallest < array[i])
{
smallest = array[i]
}}

I am worried that my array does not show scores, but the times the scores were entered.

Does that make sense?
The code here looks incorrect:
1
2
3
4
5
6
7
for (i = 0; array[i]<AMAX; i++)
{
    if (smallest < array[i])
    {
        smallest = array[i]    
    }
}

The loop condition should be i<AMAX.
1
2
3
4
5
6
7
8
9
int largest = array[0];

for (int i = 0; i<AMAX; i++)
{
    if (largest < array[i]);
    {
        largest = array[i];    
    }
}

I am worried that my array does not show scores, but the times the scores were entered.
In this case, the array index (the position of the item in the array) is representing the score.
Last edited on
Topic archived. No new replies allowed.