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} };
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.
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?
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/