Array Initialization

I am very new to the C++ language and have started learning by teaching myself using a book(C++ a beginner's guide second edition Herbert Schildt-is that a good book? But my real question is about array initialization.

I got to page 130 and it talked about arrays and how you indexed them and I don't really understand that bit about indexing arrays and when you initialize the array; also when I was writing a program(the code below) I understand the most of the program but not the indexing bit with the for loop and how the if statement works; also the array bit.

So the first array is set to 10 and I get that there is 10 numbers and I understand that because is goes 1 through 10 but then it has a second array of 2 and I don't understand why there is 10 numbers but the second array is set to 2 mean that should not fit or does that second array mean how many columns there are?

Thanks for helping me out if you write back. As I said I'm to C++ and really want to become good in the language.


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
  #include <iostream>
using namespace std;

int main() {

	int i, j;
int sqrs[10][2] = {
	{1, 1},
	{2, 4},
	{3, 9},
	{4, 16},
	{5, 25},
	{6, 36},
	{7, 49},
	{8, 64},
	{9, 81},
	{10, 100}
};

cout << "Enter a number between 1 and 10: ";
cin >> i;


for(j=0; j<10; j++)
	if(sqrs[j][0]==i) break;
cout << "The square of " << i << " is ";
cout << sqrs[j][1];
cout << "\n\n";

return 0;
}
is that a good book?

no, not at all. See http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list for a list of good books

does that second array mean how many columns there are?

Yes, that's what it means.

To be more precise, int sqrs[10][2] is an array of ten arrays of two ints each. The first array of two ints is initialized with {1, 1), the second array of two ints is initialized with {2, 4}, and so on.

When describing an array of X arrays of Y something, people often view it as a matrix with X as the number of rows and Y as the number of columns.
When describing an array of X arrays of Y something, people often view it as a matrix with X as the number of rows and Y as the number of columns.


That a good way to remember that, thanks.

no, not at all.


Sorry to ask but is there a reason to why the book I have is not very good.
I'm not being funny or anything just want to know-because this book I'm using is sometimes not very compatible with my complier and seems like a very old version of C++.

Also, sorry to ask but could you explain array indexing to me; mean I understand it a little be the book didn't really explain very well.

Thanks for your time and effort.

Thank you for your reply.
Topic archived. No new replies allowed.