Array Help Please

Pages: 123
Trying to stop and exit array when "Enter key is Pressed" but stil add the values I have entered.

CODE:

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
int rowSize = 1;
    int colSize = 10;
    int array[rowSize][colSize];
    int accumulator =0;

//FOR INPUT

    for( int row = 0; row < rowSize; row++)
    {
        cout << "ENTER NUM: ";

        for( int col = 0; col < colSize; col++)
        {
            cin >> array[row][col];
            if('\n') break;

        }
    }

//TEST OUTPUT
    for(int row =0; row < rowSize; row++)
    {

        for( int col =0; col < colSize; col++)
        {
            accumulator = array[row][col] + accumulator;
           cerr << array[row][col];
        }
    }

    cerr << endl << accumulator;





OUTPUT:



ENTER NUM: 1 2 3 4

1867469828
Process finished with exit code 0


Will break the loop but not store my values 1 2 3 4.

Its reserving values in unreserved memory can someone please help
me exit the loop when enter is pressed but still store my values before enter is pressed.
Last edited on
Just changed the input loop to this but only registers the first num1. but no longer uses unresrved memory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 for( int row = 0; row < rowSize; row++)
    {
        cout << "ENTER NUM: ";

        for( int col = 0; col < colSize; col++)
        {
            cin >> array[row][col];
            cout << endl;
            if('\n')
            {
                col++;
               while(col < colSize)
               {
                   array[row][col] = 0;
                   col++;
               }
                break;
            }

        }
    }
Last edited on
So please state your problem.
When enter is pressed I need the array to stop at that width or column size

Enter Values: 1 2 3 "Enter"

//once enter is pressed i need the values 1 2 3 to be stored
is index's [0][0] [0][1] [0][2] and move to the next row [1][0]
int the array right after enter is pressed.


1.) need a 2D array that will take in input untill "Enter" is pressed on the same first line only.

EX.
Enter Values: 1 2 3 "Enter"

2.)After "enter" is pressed i need the array to store the values 123 and change
indexs from [0][3] to start new row at [1][0] to prompt user for next input.

3.)The array has to have a max input of 10 integers so array[10][10] for each row and column

4.) The issue is stopping the column short of 10 and by moving on to the next row by pressing "enter"
Last edited on
I need a two dimensional array to input matrix integers.
However, after the Enter is pressed for row 0 the width needs to be set.
Im not sure how to do this since arrays have a fixed or constant value
such as array[10][10]

Each example is a new run of the program
("Enter" just example of user pressing Enter)

OUTPUT SHOULD LOOK LIKE
EX. 1

enter row 0 for the matrix: 1 2 3 "Enter"
enter row 1 for the matrix: 0 1 0 "Enter"
enter row 2 for the matrix: 1 0 9 "Enter"

EX.2

enter row 0 for the matrix: 1 2 3 4 5 6 "Enter"
enter row 1 for the matrix: 0 1 0 0 1 0 "Enter"
enter row 2 for the matrix: 1 0 9 0 1 0 "Enter"

EX.3
enter row 0 for the matrix: 1 2 3 4 5 6 7 8 9 1 "Enter"
enter row 1 for the matrix: 0 1 0 0 1 0 1 0 1 1 "Enter"
enter row 2 for the matrix: 1 0 9 0 1 0 1 0 1 0 "Enter"


*The first line prompting for row 0 is setting the width for the matrix. But i cant figure it out

*The first line after enter is pressed is setting up the entire matrix for the amount of integers inputed. Is the more clear?

-Let me know if i am unlcear
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/194221/
> enter row 0 for the matrix: 1 2 3 "Enter"
enter row 1 for the matrix: 0 1 0 "Enter"
enter row 2 for the matrix: 1 0 9 "Enter"

1
2
3
4
5
6
for (int i = 0; i < numRows; i++)
{
    cout << "enter row " << i << " for the matrix: ";
    for (int j = 0; j < numCols; j++)
    cin >> matrix[i][j];
}
That doesnt help me thats a basic 2D array.

read my last comment

> That doesn't help me that's a basic 2D array.
Then have you been going with 1D array or 3D array all along? Oh. I don't know.
closed account (48T7M4Gy)
Use code tags alleyzoneme and avoid multiple posts please.

Showing us all your code might be a good move too.
Last edited on
So can you clarify what is a basic array and what is an advanced array?
Note that I never intended to make numCols & numRows const in the first place. If I did, there is another way of writing : NUMCOLS, NUMROWS

This should work with all sorts of arrays, from basic to advanced.
I need a two dimensional array to input matrix integers.
However, after the Enter is pressed for row 0 the width needs to be set.

if 3 integers are entered

enter row 0: 1 2 3

the array needs to look like this:

0 1 2
1 2 1
1 2 2

if 7 integers are entered

enter row 0: 1 2 3 4 5 6 7

0 1 2 3 4 5 6 7
0 1 0 1 0 0 0 1
1 1 1 1 1 1 1 1

After the user is prompted to enter row 0: the width needs to be set by the number of integers entered on row 0.

does that really not make sense? I dont know how to better explain that.


enter row 0: 1 2 3 4 5 6 7 <-- LINE needs to set the max amount of integers allowed for the next line
Last edited on
> However, after the Enter is pressed for row 0 the width needs to be set.
So according to this assignment question, do one need to input the width of the matrix in advance or the width of the matrix will be determined after the first row input?
after the first row inputed

determined by the # of integers inputted on the first line exactly
closed account (48T7M4Gy)
You need 2 additional integer variables say, column_limit and row_limit which are each determined when the first set of numbers for the row or column are input. Count the number of numbers input in each set.

These two limits then are used to override any future reference to array access in place of rowSize and colSize. Keep in mind both have to be not greater than than those two

Breaking on '\n' and inside an if sounds a bit suspect?
Last edited on
> enter row 0 for the matrix: 1 2 3 "Enter"
The program has determined the matrix will have 3 columns. The size : 3 x 3

enter row 1 for the matrix: 0 1 0 "Enter"
enter row 2 for the matrix: 1 0 9 "Enter"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int numRows = 0, i = 0, numCols = 0;

int matrix[1000][1000];

std::stringstream ss;
std::string str = "2 8 4";

cout << "enter row " << i << " for the matrix: ";

std::getline(std::cin, str);
ss.str(str);
while((ss >> matrix[0][numCols]) && ++numRows)++numCols;
cout << "The program has determined the matrix will have " << numCols;
cout << " columns. The size : " << numCols << " x " << numRows << endl << endl;

for (i = 1; i < numRows; i++)
{
    cout << "enter row " << i << " for the matrix: ";
    for (int j = 0; j < numCols; j++)
    cin >> matrix[i][j];
}
The '\n' was just for testing purposes

can you write me a sample code for better understanding?

"when the first set of numbers for the row or column are input. Count the number of numbers input in each set"

I get what your saying but im not sure i know how to code it. I keep getting runtime errors.
Have you seen my example?
going to test and understand it right now.
Pages: 123