Thread: 2 dimensional arrays

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    2

    2 dimensional arrays

    Good day everyone. I am a newbie in C, just started out like 4 days ago. I am currently doing 2 dimensional arrays and came across this example. But when i ran it in the compiler, it says the for loop is only allowed in C99 mode. and im notsure if there are other errors. Help would be appreciated .Tq.

    Code:
    #include <stdio.h>
    int main()
    {
        int temperature[CITY][WEEK];
        for(int i =0; i < CITY;++i){
            for(int j =0; j < WEEK;++j){
                printf("City %d, Day %d: ", i+1, j+1);
                scanf("%d",&temperature[i][j]);
            }
        }
    
        printf("\nDisplaying values: \n\n");
        for(int i =0; i < CITY;++i){
            for(int j =0; j < WEEK;++j)
            {
                printf("City %d, Day %d = %d\n", i+1, j+1, temperature[i][j]);
            }
        }
        return0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well if your compiler is GCC for example, then try
    gcc -std=c99 prog.c

    If your compiler is Microsoft visual studio, then know that it only supports C89.
    In which case, you have to turn
    for(int i =0; i < CITY;++i)
    into
    int i;
    for(i =0; i < CITY;++i)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    2
    I am doing the C programming momentarily in tutorialspoint online compiler

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    project -> compile options

    Add the -std=c99 flag as shown by Salem.

  5. #5
    Registered User
    Join Date
    Jan 2017
    Posts
    1
    On line 4 it looks like you tried to declare a variable length 2D array. (PS, matrice is another term used for 2D arrays). You put in variables city and week but they have not been defined properly. That's the first thing I've noticed.


    ALso at the top write a discription about what your code is designed to do.
    I do like how you put some commentary in your printf() functions.

    -Jovani Sanchez, New to C as well.
    Last edited by Jovani Sanchez; 01-24-2017 at 07:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two Dimensional Arrays (What does this do?)
    By ChickenChowMinh in forum C Programming
    Replies: 1
    Last Post: 02-10-2013, 08:27 PM
  2. Two Dimensional Arrays
    By Lock in forum C Programming
    Replies: 7
    Last Post: 10-20-2011, 11:04 PM
  3. 2 dimensional arrays
    By naserian in forum C Programming
    Replies: 5
    Last Post: 03-22-2010, 04:40 PM
  4. Help with 2 dimensional arrays
    By riotact in forum C++ Programming
    Replies: 1
    Last Post: 11-02-2002, 10:19 AM
  5. Two dimensional arrays
    By Jax in forum C Programming
    Replies: 1
    Last Post: 11-07-2001, 12:53 PM

Tags for this Thread