How can I make this brutally forced array output into for loop?


1
2
3
4
  cout<<"CRAMER'S EQUATION IS: \n";
  cout<<"  "<<equation[0][0]<<"x + "<<equation[0][1]<<"y = "<<equation[0][2];
  cout<<endl;
  cout<<"  "<<equation[1][0]<<"x + "<<equation[1][1]<<"y = "<<equation[1][2];
Last edited on
Sane version:
1
2
3
for(int i = 0; i < 2; ++i) {
    std::cout << "  "<< equation[i][0] << "x + " << equation[i][1] << "y = " << equation[i][2] << std::endl;
}

Less sane version:
1
2
3
4
5
6
std::array<std::string, 3> prefix {"  ", "x + ", "y = "};
for(int i = 0; i < 2; ++i) {
    for(int j = 0; j < 3; ++j)
        std::cout << prefix[j] << equation[i][j];
    std::cout << std::endl;
}
you can use loop for to output each element of array. But remember, to start the loop from 0, because the first element of array is 0.
for example:

1
2
3
4
5
int array[5];

for(int i=0; i<5 ; i++){
array[i]= i*2;
}


now to output each element of array:

1
2
3
for(int i=0; i <5 ; i++){
cout << array[i];
}


or you can call an additional element of array using this

1
2
3
cout << array[2]; //show the value from the array
cout << *(array+2); //show the value from the array
cout << array+2; //show the address of this element 
Topic archived. No new replies allowed.