7 Replies - 337 Views - Last Post: 11 November 2017 - 05:17 PM Rate Topic: -----

#1 geos59  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 46
  • Joined: 30-August 15

User input for a 2D array.

Posted 08 November 2017 - 09:17 PM

How do I make a 2D array that asks the user where to put an object for a row position, then a column position and puts that object into that row and column position.

[So if the user types 3 and then 10, it will put the object in row 3, column 12.]

Here's my failed example.

    int ROW, COL;
    
    int Rposition, Cposition;
    
    //I want it to be a 20 x 20 2D array
    int myArray[ROW][COL];
    
    cout << "Type Row value here. " << endl;
    cin >> Rposition;
    ROW = Rposition;
    
    cout << "Type Column value here. " << endl;
    cin >> Cposition;
    COL = Cposition;
    
    cout << myArray[ROW][COL];



*I know the above doesn't work, I just get garbage data.*

Every time I try I either get garbage data, an error, or something that I don't really want.

Is This A Good Question/Topic? 0
  • +

Replies To: User input for a 2D array.

#2 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,466
  • Joined: 13-June 14

Re: User input for a 2D array.

Posted 08 November 2017 - 09:43 PM

You need to assign to the right position in the array. Since your know how to do assignment (e.g. line 10), this shouldn't be a problem - assigning to a position in an array is really no different to assigning to any other variable.

If you're getting errors when you're doing that, you need to show the relevant code, the errors and tell us what you think they mean.
Was This Post Helpful? 0
  • +
  • -

#3 Thomas1965  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 275
  • Joined: 09-September 16

Re: User input for a 2D array.

Posted 09 November 2017 - 01:16 AM

int ROW, COL;
int Rposition, Cposition;
//I want it to be a 20 x 20 2D array
int myArray[ROW][COL];


First problem is that ROW and COL are uninitialized so they might have a value like -858993460
Second problem is that array indexes need to be constant - unless you use C99 where VLAs are supported

There are better ways to do it - like dynamic memory or std::vector if you use C++
Was This Post Helpful? 0
  • +
  • -

#4 geos59  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 46
  • Joined: 30-August 15

Re: User input for a 2D array.

Posted 11 November 2017 - 07:31 AM

@ndc85430 Well I tried doing different things, but to no avail.

What am I doing wrong?

    //Initial Values
    int ROW = 0;
    int COL = 0;

    //2D Array
    int board[ROW][COL];
    
    //To move to the array's position.
    ROW += 1;
    COL += 1;
    
    cout <<"Enter the row value here. " << endl;
    cin  >> ROW;
    
    cout <<"Enter the column value here. " << endl;
    cin >> COL;
    
    //This line gives me garbage data.
    cout << board[ROW][COL] << endl;

    //This line gives me the actual address, instead of location.
    cout << board[COL];



Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1375
  • View blog
  • Posts: 4,307
  • Joined: 20-March 10

Re: User input for a 2D array.

Posted 11 November 2017 - 10:09 AM

Well you are creating a board array of size board[0][0] then you try to print out some higher location like 12 20...

You are accessing the array out of bounds by putting in anything other than 0.

And to top it all off you don't assign any value to the array then try to print out its contents..

try board[0][0] = 9;

then cout << board[0][0];

just for fun !
Was This Post Helpful? 0
  • +
  • -

#6 geos59  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 46
  • Joined: 30-August 15

Re: User input for a 2D array.

Posted 11 November 2017 - 04:36 PM

I appear to be getting closer, now I have an output with no errors, but no matter what value I type - the answer is still 0 for the row and column.

**I also got rid of the 'for' loop just in case, but I still get 0 for the row and columns.

    //A 20x20 2D Array. [20 rows and 20 columns]
    int board[20][20];

    //Initializing the array with all zeroes.
    for(int i=0; i < 19; i++)
    {
        board[i][i] = 0;
    }

    int ROW;
    int COL;
    
    cout << "Enter a row value. " << endl;
    cin >> board[ROW][20];
    
    cout <<"Enter a column value. " << endl;
    cin >> board[20][COL];
    
    cout <<"Person A moved to " << ROW <<" row and " << COL << " column." << endl;

    return 0;


Was This Post Helpful? 0
  • +
  • -

#7 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1375
  • View blog
  • Posts: 4,307
  • Joined: 20-March 10

Re: User input for a 2D array.

Posted 11 November 2017 - 05:14 PM

You have a basic lack of understanding of arrays... please read the link below...

https://www.tutorial.../cpp_arrays.htm

everything is 0 because that is what you set it to...
Was This Post Helpful? 0
  • +
  • -

#8 GazinAtCode  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 69
  • Joined: 26-September 16

Re: User input for a 2D array.

Posted 11 November 2017 - 05:17 PM

Your for loop is incorrect. You're effectively setting the elements on the main diagonal to zeros while leaving the last one out. I guess the most elegant way to initialize a C-style array with zeros would be to use memset.

#include <cstring>

int main()
{
    int board[20][20];
    memset(&board[0][0], 0, sizeof(board));
    /* . . . */
}


This post has been edited by GazinAtCode: 11 November 2017 - 05:18 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1