2D array user choice selection, getting odd answers

Hi, I'm really new to C++, and whenever I see an exercise in the book I'm learning from I like to expand on it, or use it to build something else I'd like to see. This is a part of a larger program I'm writing, and I like to build smaller modules to see first if my idea works. In this program I'm using a 2D array, and it prints fine, but when I try to process the user's choice I get weird answers.

Here is the program:

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// name: 2D-array-while-loop-sentinel-if.cpp
// date: Mon Mar 28 2016
// purpose: create a 2D array with 6 rows and 2 columns.
// column 1 is the line count, column 2 is the item.
// create a function that takes user's input on which item
// they want to see, and display the line count and the item.

#include <iostream>
#include <iomanip>
using namespace std;

const int ARRAY_ROW_SIZE = 6;
const int ARRAY_COL_SIZE = 2;

void showArray(int const array[][ARRAY_COL_SIZE], int numRows);

int main()
{
    int arrayX[ARRAY_ROW_SIZE][ARRAY_COL_SIZE] = {{1,38}, {2,42}, {3,66}, {4,33}, 
                                                      {5,66}, {6,55}};
    int count = 0;
    int choice = 0;     // user input choice
    char repeat = 'y';  // while loop sentinel

    showArray(arrayX, ARRAY_ROW_SIZE); // call showArray function

    cout << endl;
    cout << "Pick one of the " << ARRAY_ROW_SIZE << " elements: ";
    cin >> choice;
    cout << endl;

    // take in user input
    while (repeat == 'y')   // only continue while loop if answer is 'y'
    {
        // set boundary for array, no less than 1 to no more then array size
        if (choice > 0 && choice <= ARRAY_ROW_SIZE)
        {
            // use choice-1 to match the screen printed list with elements starting at 1, not 0
            cout << "Element " << choice << " contains the value " 
                 << arrayX[choice-1][choice-1] << "." << endl;
        }
        else
        {
            // issue error if number is 0 or less, and greater than array size
            cout << "ERROR! You can only pick a number from the " << ARRAY_ROW_SIZE 
                 << " elements." << endl;
        } // end if choice check

        // while loop control
        cout << endl << "Repeat? (y or n): ";
        cin >> repeat;

        if (repeat == 'y')
        {
            // repeat user input within the while loop
            cout << endl;
            cout << "Pick which number you want to see: ";
            cin >> choice;
            cout << endl;
        } // end if-sentinel check
    } // end while loop
    return 0;
}

void showArray(int const array[][ARRAY_COL_SIZE], int numRows)
{
	for (int row = 0; row < numRows; row++)
	{
		for (int col = 0; col < ARRAY_COL_SIZE; col++)
		{
			cout << setw(1) << array[row][col] << " ";
		}
		cout << endl;
	}
}


Here is the output:


1 38
2 42
3 66
4 33
5 66
6 55

Pick one of the 6 elements: 1

Element 1 contains the value 42.

Repeat? (y or n): y

Pick which number you want to see: 2

Element 2 contains the value 4.

Repeat? (y or n): y

Pick which number you want to see: 3

Element 3 contains the value 66.

Repeat? (y or n): y

Pick which number you want to see: 4

Element 4 contains the value 2043553669.

Repeat? (y or n):

Pick which number you want to see: 4

Element 4 contains the value 2043553669.

Repeat? (y or n): y

Pick which number you want to see: 5

Element 5 contains the value 1973443524.

Repeat? (y or n): y

Pick which number you want to see: 6

Element 6 contains the value 2130567168.
Last edited on
Hello, Please always use code tags when posting code. Here
1
2
cout << "Element " << choice << " contains the value " << arrayX[choice-1][choice-1] << "." << endl;
}
The value will always be in the second column right? So instead of subtracting one from choice on the column just set it to 1:
1
2
cout << "Element " << choice << " contains the value " << arrayX[choice-1][1] << "." << endl;
}
Last edited on
Thank you, it worked. I would like to understand why the column array works with just a '1' in it?

I thought it would have to match what is in the rows array, this is why I had it as arrayX[choice-1][choice-1].
When you access a 2D array like this array[1][4] you are only accessing one piece of memory or one variable. A 2D array uses two indexes to access the values, One for the column and one for the row. The way you have your array set up it looks something like this (although in memory it is not laid out like this but that doesn't matter right now)

1
2
3
4
5
6
7
         Column 0       Column 1
Row 0    1[0][0]        38[0][1]
Row 1    2[1][0]        42[1][1]
Row 2    3[2][0]        66[2][1]
Row 3    4[3][0]        33[3][1]
Row 4    5[4][0]        66[4][1]
Row 5    6[5][0]        55[5][1]


Next to the values I have put the indexes to access each of the values. After the user choses the the number that they want to see (which is stored in column 0) we have to print the value that corresponds with it. The way you have your array laid out the value will always be in the second column not, choice-1. For example if the user enters a 6 the program will get and print the value stored at Array[5][5] which is not even a value in this array.
Last edited on
Thanks for laying it out like this, it helps me to understand what is going on.

Here's a further refinement of this program: I wanted to see if I understand how to use typedef, and how I could package the while-if stmts from main() into a separate function. Once I saw the parameters into showArray were similar I figured I could just overload that function. I also removed the int count = 0, it was a carry over from the program I used and is not required inside the program.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// MOD: now using typedef, packaged while/if from main() into a now overloaded
//      showArray function.

#include <iostream>
#include <iomanip>
using namespace std;

const int ARRAY_ROW_SIZE = 6;    // 6 rows
const int ARRAY_COL_SIZE = 2;    // 2 columns each

typedef const int Array[ARRAY_ROW_SIZE][ARRAY_COL_SIZE];

// function prototype now overloaded, it prints AND processes user choice
void showArray(Array, int);          // prints array to screen
void showArray(Array, int, int);     // processes user choice

int main()
{
    int arrayX[ARRAY_ROW_SIZE][ARRAY_COL_SIZE] = {{1,38}, {2,42}, {3,66}, 
                                                      {4,33}, {5,66}, {6,55}};
    int choice = 0;     // user input choice

    showArray(arrayX, ARRAY_ROW_SIZE);

    cout << endl;
    cout << "Pick one of the " << ARRAY_ROW_SIZE << " elements: ";
    cin >> choice;
    cout << endl;

    showArray(arrayX, ARRAY_ROW_SIZE, choice);

	return 0;
}

void showArray(Array arrayX, int numRows)
{
	for (int row = 0; row < numRows; row++)
	{
		for (int col = 0; col < ARRAY_COL_SIZE; col++)
		{
			cout << setw(1) << arrayX[row][col] << " ";
		}
		cout << endl;
	}
}

void showArray(Array arrayX, int numRows, int choice)
{
    char repeat = 'y';  // while loop sentinel

    while (repeat == 'y')   // only continue while loop if answer is 'y'
    {
        // set boundry for array, no less than 1 to no more then array size
        if (choice > 0 && choice <= ARRAY_ROW_SIZE)
        {
            // use choice-1 to match the screen printed list with elements starting at 1, not 0
            cout << "Element " << choice << " contains the value " << arrayX[choice-1][1] 
                 << "." << endl;
        }
        else
        {
            // issue error if number is 0 or less, and greater than array size
            cout << "ERROR! You can only pick a number from the " << ARRAY_ROW_SIZE 
                 << " elements." << endl;
        } // end if choice check

        // while loop control
        cout << endl << "Repeat? (y or n): ";
        cin >> repeat;

        if (repeat == 'y')
        {
            // repeat user input within the while loop
            cout << endl;
            cout << "Pick which number you want to see: ";
            cin >> choice;
            cout << endl;
        } // end if sentinel check
    } // end while loop
}
Looks good! If you have anymore questions I am happy to help.
Hi, yes, you could help me out with this project I've given myself. I'm stuck between where I think it could go, and what I should really be doing with it. I created a new post for it:

First class program, how to implement design ideas?

http://www.cplusplus.com/forum/general/187925/
Last edited on
Topic archived. No new replies allowed.