0

I can't understand why I get the run time error:

the variable ia is being used without being initialized.

However, as far as I can see, I have initialized it.

#include <iostream>
using namespace std;
int main()
{
 //array dimensions
 const int row_size = 2;
 const int col_size = 4;
 //array definition
 int ia[row_size][col_size] = {{0, 1, 2, 3},{5, 6, 7, 8}};
 cout << ia[2][4];
 system("PAUSE");
 return 0;
}
Mateen Ulhaq
27.9k21 gold badges122 silver badges155 bronze badges
asked Oct 8, 2011 at 21:13
1
  • 2
    ia[2][4] is invalid. Counting starts from 0, not 1. You probably meant ia[1][3]. Commented Oct 8, 2011 at 21:15

4 Answers 4

5

C++ array indexes are zero-based. So to access the fourth column of the second row, you need to access ia[1][3].

answered Oct 8, 2011 at 21:15
Sign up to request clarification or add additional context in comments.

1 Comment

Ah such a simple oversight... thanks will give you the green mark.
1
ia[2][4]

doesn't exist.

ia[0..1][0...3]

all exist, however.

Try:

cout << ia[1][3];

Arrays in C++ start with the index 0. 1 is actually the 2nd element. So:

int a[2] = {42, 50};
std::cout << a[0] << a[1]; // prints 4250
std::cout << a[2]; // a[2] doesn't exist!
answered Oct 8, 2011 at 21:16

Comments

0

Array indexes start from 0, so your ia[2][4] will be out of bounds. Should be ia[1][3].

answered Oct 8, 2011 at 21:16

Comments

0

Arrays are 0-based, i.e. the first element in an array a is a[0]. So the last element in an array of 4 elements would be a[3]. In your case ia[1][3] would give you the sought element I believe.

answered Oct 8, 2011 at 21:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.