0

I'm quite new to programming and I have used c# before switching to VSB C for school. I am currently trying to print my array through a loop, but i get a mess of irrelevent numbers. Any help or tips on how i can avoid this in the future would be great. My code follows as:

int main()
{

    int flightTimesAndLayoverTimes[2][6] =
    {
        (255, 238, 235, 138, 207),
        (80, 46, 689, 53, 0)
    };

    for (int i = 0; i <= 2; i++)
    {


        for (int j = 0; j < 6; j++)
        {
            printf("%c\n", flightTimesAndLayoverTimes[i][j]);
        }
    }
}
6
  • First time even posting on this extraordinaire website, please ignore all the terrible formatting.
    – Tyler F
    Nov 13, 2014 at 0:00
  • 1
    Where you initialize your array, try using { and } instead of ( and )
    – Dmitri
    Nov 13, 2014 at 0:06
  • 1
    and change from i <= 2 to i < 2 in the outer for loop
    – Dmitri
    Nov 13, 2014 at 0:08
  • Wow, thank you so much. It is the little problems like these that convince me I'm in the wrong program..
    – Tyler F
    Nov 13, 2014 at 0:15
  • 1
    the printf() is expecting characters, but is being fed integers, often integers that are of a greater value than a char (8 bit=char has a max value of 255. So you can expect to print garbage Nov 13, 2014 at 4:01

1 Answer 1

1

Try this:

int flightTimesAndLayoverTimes[2][6] = {{255, 238, 235, 138, 207},
                                        {80, 46, 689, 53, 0}};

for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 5; j++)
    {
        printf("%i \n", flightTimesAndLayoverTimes[i][j]);
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.