0

I have written a piece of c code which I intend to just print out the value of a 2d array

I'm very new to c so this answer might be really basic sorry.

my code compiles fine (gcc) but then doesn't return what I expect. im using ubuntu 18.04 in WSL with gcc as my compiler.

heres my code


#include <stdio.h>

int main(void) {
        int array1[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
        int i2 = 0;
        printf("hello");
        for(int i = 0; i > 4; i++){
                i2 = i;
                printf("hi %d", array1[i][i2]);

}

}

returned value hello

i would expect it to print all my array elements but i dont understand why it doesnt since i dont get an error in gcc so my syntax seems fine.

Thanks.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
Newbie
  • 31
  • 6
  • 2
    You condition in `for` loop is wrong. Try `for(int i = 0; i< 3 ; ++i)` – Mathieu Jul 28 '21 at 08:40
  • 1
    `for` loops iterate until the condition is *false*. In your code, `i > 4` will be false as soon as the code enters the `for` loop, change it to `i < 3` or even `i <= 2` .Additionally, there is no need for the `i2` variable, it only serves to add clutter in your code, you can simply do `array[i][i]`. – Ruks Jul 28 '21 at 08:41
  • just tried changing the i++ to ++i which didn't fix the problem. thanks for trying though – Newbie Jul 28 '21 at 08:42
  • thanks for the tip with i2 not being necessary i have no clue why i added that. also thanks so much for the fix. i dont know how i didnt see that. – Newbie Jul 28 '21 at 08:43
  • "just tried changing the i++ to ++i which didn't fix the problem". Indeed, the important part was the test modification (`>` to `<`). The `++` before `i` is just a coding tic... – Mathieu Jul 28 '21 at 09:53
  • Thanks for helping and fixing my problem although i have already got another one. Lol – Newbie Jul 28 '21 at 10:10
  • Also check your wording / terms: your code does not `return` "hello", it prints it. Your `main` method should `return` an `int`, as you wrote `int main(void)`. – Robert Jul 28 '21 at 15:33

1 Answers1

3

There are a couple of things wrong in this piece of code. Firstly, the usage of the column index like that (i2 = i) will make you print elements at the same row and column index, like array1[0][0], array1[1][1], and you'll skip things like array1[0][1]. You'd have to fix this by using a second for loop inside the first one, incrementing a second index (check this for example). The other wrong thing about this piece of code is the condition in the for loop: you want to keep iterating while i<3, not i>4. In that case, you never enter the for loop, since the condition is not met even at the first iteration.

Nastor
  • 638
  • 4
  • 15
  • thanks! I will implement this in my next code and maybe try and make a game or something! was my i++ incorrect or why would i do ++i instead? – Newbie Jul 28 '21 at 08:49
  • 1
    Good luck with that, but I'd suggest you to study some more basic concepts about C if you're new with this programming language before undertaking such a thing. For the `++i` vs `i++`, check [this](https://stackoverflow.com/questions/2315705/what-is-the-difference-between-i-i-in-a-for-loop) out. – Nastor Jul 28 '21 at 08:51