I am looking to finish up this program for class. I'm lost when it comes to arrays and I've read all the course work, the book, etc. The question is how do you increase an array element at the position?

``````using namespace std;
int main()
{
int quantity[4][5], warehouse, product;
int inventory[4][5] = {
{900,400,250,95,153},
{52, 95, 625, 44, 250},
{100,720,301,50,878},
{325,650,57,445,584},
};
cout << "Enter the warehouse number between 1 and 4: " << endl;
cin >> warehouse;

cout << "Enter the product location between 1 and 5: " << endl;
cin >> product;
cout << "Enter the quanity delivered: " << endl;
cin >> quantity[4][5];

for(warehouse = 0; warehouse < 4; warehouse++)
for(product = 0; product < 5; product++)
quantity[warehouse][product] = inventory[warehouse][product] +
quantity[4][5];

cout << "The amount of units in warehouse " << warehouse << " is \n\n";

/* Then print the results */

for(warehouse = 0; warehouse < 4; warehouse++ ) {
for( product = 0; product < 5; product++ )
cout << "\t" <<  quantity[warehouse][product];
cout << endl;   /* at end of each warehouse */
}
return 0;
}
``````
2
Contributors
3
Replies
34
Views
5 Years
Discussion Span
Last Post by vmanes

To increase (or decrease) the value stored in an array element, just do the math.

``````int data[5] = { 1, 2, 3, 9, 10 };

data[0]++;  //increases the value by 1

data[1] = data[1] + 5;  //adds some number to the element

data[2] += 10; //shortcut operator

//array contents now:  2, 7, 13, 9, 10
``````

That part I understand but, how does that relate to a 2D array?

Works the same, but you must specify the cell you want with both row and column indexes.

``````int data2D[5][3] = { 0 );

for (int i = 0; i < 5; i++ ) //walk through the rows
for( int j = 0; j < 3; j++ ) //walk the columns in each row
cin >> data2D[i][j];

//OK, array filled with something now. Change some

data2D[0][0]++;  //increment the upper left corner cell
data2D[4][3] += 10; //modify the lower right corner cell

int row, col;
cout << "Enter a row and column cell to set to zero: ";
cin >> row >> col;
if( row < 0 || row > 4 || col < 0 || col > 2 )
cout << "Bad input, so sorry, don't pass Go, don't collect \$200." << endl;
else
data2D[row][col] = 0;
``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.