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
15345
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.