Simple C programming - integer confusion

This is a discussion on Simple C programming - integer confusion within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm new to C and was rather proud of myself having written a new and quite complicated program, ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Unhappy Simple C programming - integer confusion

    Hi everyone,

    I'm new to C and was rather proud of myself having written a new and quite complicated program, until I realised the numbers it was giving me were wrong. I wrote a much simpler program to see if I could understand the problem, and I'm now completely confused!!! I'm sure it's a simple problem, but I've tried reading all over the internet and I can't find anything about it, so if someone could help I'd be ever so grateful!!

    This is the simple program...

    #include <stdio.h>
    #include <math.h>
    #include <complex.h>

    int main()
    {

    /* Parameters */
    int count1, count2, i, j, g[5][5], swap;

    swap = 3;

    for (count1 = 1; count1<=5; count1++)
    {
    for (count2 = 1; count2<=5; count2++)
    {
    g[count1][count2] = (count2);
    }
    }

    for (i=1; i<=5;i++)
    {
    printf("\n");
    for (j=1; j<=5; j++)
    {
    printf("%d",g[i][j]);
    }
    }
    printf("\n");
    printf("swap char = %d\n",swap);
    return 0;
    }
    ;


    ... the output I would expect is...


    12345
    12345
    12345
    12345
    12345
    swap char = 3

    ... however, what I get is...

    12345
    12345
    12345
    12345
    1
    5345
    swap char = 5

    If I change the loop, so that g[count1][count2] = count1 then I get a different last line, and the swap character is 4. If I do g[count1][count2] = (count1 + count2) then my swap character is 9!

    Can anybody explain why? I've tried swapping things round to see if I can work it out and I'm utterly confused!!

    p.s. I realise the program itself doesn't make any sense - I'm just using it to figure out where I'm going wrong with my final program, and to try to learn how it works.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    1. use code tags when posting code
    2. arrays are zero based - indexes are from 0 to 4
    using index 5 - brings you out of bounds of your array overwriting over variables...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    Thanks for your reply!

    And sorry, I didn't know what a code tag was.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by JGeorge View Post
    And sorry, I didn't know what a code tag was.
    << !! Posting Code? Read this First !! >>
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Access Violation
    By Graham Aker in forum C Programming
    Replies: 100
    Last Post: 01-26-2009, 07:31 PM
  3. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  4. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  5. ! C question: simple integer function passing
    By aaronc in forum C Programming
    Replies: 10
    Last Post: 04-29-2004, 01:18 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21