Change array values
How can i change a specific value in an array, if there is already a value at that location?
After printing the array again, no values are changed.
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
|
int corchoice, k, l, i ,j;
for (i = 6; i >= 0; i--)
{
cout << endl;
for (j = 0; j < maxc; j++)
{
if (np[i][j] == -1)
cout << " ";
else
cout << setw(6) << np[i][j];
}
}
cout << endl << "Is this pyramid correct?(yes = 1/no = 2)" << endl;
cin >> corchoice;
while (corchoice != 1)
{
cout << "please enter the row of the incorrect number." << endl;
cin >> k;
cout << "please enter the column of the incorrect number." << endl;
cin >> l;
cout << "Please enter a integer for the array number: " << "[" << k << "]" << "[" << l << "]" << endl;
cin >> np[k][l];
cout << endl;
for (i = 6; i >= 0; i--)
{
cout << endl;
for (j = 0; j < maxc; j++)
{
if (np[i][j] == -1)
cout << " ";
else
cout << setw(6) << np[i][j];
}
}
cout << endl;
cout << "Is this pyramid correct?(yes = 1/no = 2)" << endl;
cin >> corchoice;
}
|
If I understood correctly you can change the specific value by using its index like I did in this 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
|
#include<iostream>
using namespace std;
int main()
{
int array[]={2,3,5};
for (int i=0 ; i<3 ; i++)
{
cout<<array[i]<<endl;
}
array[1]=8;
for (int j=0;j<3;j++)
{
cout<<array[j]<<endl;
}
cin.get();
cin.ignore();
return 0;
}
|
Last edited on
Topic archived. No new replies allowed.