Problem with Arrays

Hi everyone, im a noob and need some help with arrays. I am trying to create a 5x5 column and get it to print correctly. I have done this with single digit numbers but i am having trouble with decimals.

**EDIT:okay so i changed the data type to double instead of int and changed the index to [4][4] but now it is just printing the last decimal in the array "4.4" over and over again, how can i make it print all the decimals in a 5x5 columns/rows?
HERE is the code:

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

int main()
{
int qArray2D [5][5]=
{ {0.0,0.1,0.2,0.3,0.4 },
{1.0,1.1,1.2,1.3,1.4},
{2.0,2.1,2.2,2.3,2.4},
{3.0,3.1,3.2,3.3,3.4},
{4.0,4.1,4.2,4.3,4.4} };


for (int row = 0; row<5; row++)
{
for (int column = 0; column < 5; column++)
cout << qArray2D[5][5]<<" ";
}
cout << endl;
Last edited on
with decimals use the data type float...
float and double work with decimals..

in your cout statement you are accessing index[5][5] which is wrong. Arrays start at 0 therefore 0-4 would be your 5. Furthermore, the cout statement should be using the for loop variables.

cout<<qArray2D[row][column];
what are you finding
okay so i changed the data type to double instead of int and changed the index to [4][4] but now it is just printing the last decimal in the array "4.4" over and over again, how can i make the loop print from the first row all the way to the last?
That's cause you have qArray[5][5] in your cout - there should be row and colummn inside.
how can i make the loop print from the first row all the way to the last?


See my last post, you are not using the for..loop row and column variables to access the index of the 2D array.
 
cout << qArray2D[5][5]<<" ";

You're printing a fixed position from the array every time (which also happens to be out of bounds).

Softrix gave you the answer above. You need to use the row and column indexes to refer to a specific location.
1
2
3
4
5
    for (int row = 0; row<5; row++)
    {   for (int col = 0; col< 5; col++)
            cout << qArray2D[row][col]<<" ";
        cout << endl;  // end each row 
    }


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
Thanks guys! really appreciate the help.
Topic archived. No new replies allowed.