0

I have a some knowledge of Python, OOP and some programming principles and I am currently trying to teach myself C++. However, I have a very weird, unexpected problem when trying to deal with multidimensional arrays. Basically, I have this code:

#include <iostream>
using namespace std;

int main() {

  int a[2][2] = { {2, 3} , {4, 6} };

  for (int i = 0; i < 2; i++) {
    for (int y = 0; i < 2; y++) {
      cout << a[i][y] << endl;
    }
  }

  return 0;

}

It starts off great (returning 2, 4, 5, 6; but end up with many weird and seemingly random numbers). However, in Python the similar code:

a = [[2, 3], [4, 6]]
for i in range(2):
 for y in range(2):
  print a[i][y]

Only prints the more expected: 2, 3, 4, 6. Why? What am I doing wrong?

(Also, does anyone have any suggestions as to what projects are excellent for learning c++?)

Andreas
  • 4,525
  • 3
  • 14
  • 28
gloriousCatnip
  • 335
  • 4
  • 18
  • 13
    Simple typo: `for (int y = 0; i < 2; y++) {` --> `for (int y = 0; y< 2; y++) {` – πάντα ῥεῖ Jul 01 '16 at 20:51
  • But shouldn't that throw an error/warning? I am using g++, what can I use to raise warnings? – gloriousCatnip Jul 01 '16 at 20:54
  • 1
    @gloriousCatnip What error or warning do you expect? From compiler's point of view that is perfectly valid code. It enters into endless loop, but that's bug in your code explained by πάντα ῥεῖ – mvidelgauz Jul 01 '16 at 20:58
  • 2
    @gloriousCatnip You code is perfectly valid so the compiler can't generate a warning. The answer to this post explains why there's no warning/error when accessing an array outside of its bounds: http://stackoverflow.com/questions/1239938/accessing-an-array-out-of-bounds-gives-no-error-why – Foggzie Jul 01 '16 at 20:58
  • 2
    @gloriousCatnip Why would you expect the compiler throwing a warning about that? How could the compiler know you weren't clever enough to choose the correct loop condition in the inner loop? It's all OK syntactically. May be some high level SCA tools that use trained databases of wrong coding patterns could detect such stuff. – πάντα ῥεῖ Jul 01 '16 at 20:58
  • Oh, okay. I am just really new to this, and trying to figure things out! Thanks for helping me out. :) – gloriousCatnip Jul 01 '16 at 21:01
  • 4
    btw you can you use [static code analiser](https://en.wikipedia.org/wiki/Static_program_analysis). Some of them will find such bugs – mvidelgauz Jul 01 '16 at 21:03
  • You could use [range-based for](http://en.cppreference.com/w/cpp/language/range-for): `for (auto&& row : a) {for (auto&& val : row) { cout << val << endl; } }` A little less opportunity for error that way. – Fred Larson Jul 01 '16 at 21:21
  • @gloriousCatnip There is no syntax, or linker errors, and the compiler considers this valid. However there is a logic error, and it does not function the way *you* want it to. The compiler doesn't know how *you* want the program to function. –  Jul 02 '16 at 02:19

1 Answers1

1

Simple typo: for (int y = 0; i < 2; y++) { --> for (int y = 0; y< 2; y++) {


"How to learn C++" is off-topic in this forum from my understanding. Put your hopes in helpful comments.

πάντα ῥεῖ
  • 85,314
  • 13
  • 111
  • 183
Andreas
  • 4,525
  • 3
  • 14
  • 28