I'm working on a lab assignment for a C programming class I'm taking. I wrote the code in my local Cygwin directory, compiled it with gcc
, and the executable that is produced works exactly the way I want it to without any errors.
When I copy my code over to my school's UNIX server and compile it with gcc
, I don't get any errors, but when I try to run it, nothing happens.
I tried doing gcc 2darray.c -Wall -pedantic
and this is what was returned:
2darray.c: In function 'main':
2darray.c:5:3: warning: missing braces around initializer [-Wmissing-braces]
2darray.c:5:3: warning: (near initialization for 'M[0]') [-Wmissing-braces]
2darray.c:5:24: warning: C++ style comments are not allowed in ISO C90 [enabled by default]
2darray.c:5:24: warning: (this will be reported only once per input file) [enabled by default]
The errors mention something about initializing the array M
, but I don't see any problems with the way I initialized it. Here's the code I'm trying to compile:
#include <stdio.h>
int main(void)
{
int M[10][10] = {0}; // creating a 10x10 array and initializing it to 0
int i, j; // loop variables
int sum[10] = {0}; // creating an array to hold the sums of each column of 2d array M
for (i = 1; i < 10; i++) // assigning values to array M as specified in directions
{
for (j = i - 1; j < i; j++)
{
M[i][j] = -i;
M[i][j+1] = i;
M[i][j+2] = -i;
}
}
for (i = 0; i < 10; i++) // printing array M
{
for(j = 0; j < 10; j++)
{
printf("%3d", M[i][j]);
}
printf("\n");
}
printf("\n");
for (i = 0; i < 10; i++) // calculating sum of each column
{
for (j = 0; j < 10; j++)
{
sum[i] = M[j][i] + sum[i];
}
printf("%3d", sum[i]); // printing array sum
}
return 0;
}
I tried inserting a printf statement between the variable declarations and the first for loop and the statement printed, so maybe something goes wrong in my loops?
If relevant, here's what the output looks like from my Cygwin directory and what it should like in my school's UNIX directory:
0 0 0 0 0 0 0 0 0 0
-1 1 -1 0 0 0 0 0 0 0
0 -2 2 -2 0 0 0 0 0 0
0 0 -3 3 -3 0 0 0 0 0
0 0 0 -4 4 -4 0 0 0 0
0 0 0 0 -5 5 -5 0 0 0
0 0 0 0 0 -6 6 -6 0 0
0 0 0 0 0 0 -7 7 -7 0
0 0 0 0 0 0 0 -8 8 -8
0 0 0 0 0 0 0 0 -9 9
-1 -1 -2 -3 -4 -5 -6 -7 -8 1
int M[10][10] = {0};
toint M[10][10] = {{0}};
– user1969104-std=c99
– BLUEPIXYM[i][j+2] = -i;
: wheni : 9
, out of bounds. – BLUEPIXY