[ create a new paste ] login | about

Link: http://codepad.org/mw5v8sHV    [ raw code | output | fork | 1 comment ]

C, pasted on Oct 19:
#include <stdio.h>

int main(void)
{
    //int  i0,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15;
    
    // printf("Please enter 16 digits between 1 and 16: ");
    // scanf("%d , %d , %d , %d , %d , %d , %d , %d , %d , %d , %d , %d , %d , %d , %d , %d", &i0,&i1,&i2,&i3,&i4,&i5,&i6,&i7,&i8,&i9,&i10,&i11,&i12,&i13,&i14,&i15);
    
    int array1[16] = {1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2};
    
    
    
    //Call the method to sum all the rows.  Create the new array to hold
    //all of the sums.  For-loop to print it to the screen
    sumRows(array1);
    
    
    //Call the method to sum all the columns.  Create the new array to hold
    //all of the column sums.  For-loop to print it to the screen
    int columnSum[4];
    sumColumns(array1);
    
    
    //Call the method to sum the two diagonals.  Create a new array to hold
    //the sums.  For-loop to print it to the screen
    int diagonalSum[2];
    sumDiagonals(array1);
}

int sumRows(int array1[]) {

{
    //Create the holder array
    int rowSum[4];
    
    //Traveses each "row" of the array
    for(int i = 0; i < 15; i+=4){
        rowSum[i/4] = array1[i] + array1[i+1] + array1[i+2] + array1[i+3];
    }
    for(int i = 0; i < 4; i++){
        printf(rowSum[i]);
    }
    printf("\n");
    
}

int sumColumns(int array1[]);{
    //Create the holder array
    int columnSum[4];
    //Traverse each "column" of the array
    for(int i = 0; i < 4; i++){
        columnSum[i] = array1[i] + array1[i+4] + array1[i+8] + array1[i+12];
    }
    //return columnSum;
    for(int i = 0; i < 4; i++)
        printf(columnSum[i]+" ");
    printf("\n");
}

int sumDiagonals(int array1[]);{
    //Create the holder array
    int diagonalSum[2];
    //Traverse both "diagonals" of the array
    for(int i = 0; i < 1; i++){
        diagonalSum[i] = array1[i] + array1[i+5] + array1[i+10] + array1[i+15];
        diagonalSum[i+1] = array1[i+15] + array1[i+10] + array1[i+5] + array1[i];
    }
    //return diagonalSum;
    
    {
    for(int i = 0; i < 2; i++)
        printf(diagonalSum[i]+" ");
        
    }
}

}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
In function 'sumRows':
Line 38: error: 'for' loop initial declaration used outside C99 mode
Line 41: error: redefinition of 'i'
Line 38: error: previous definition of 'i' was here
Line 41: error: 'for' loop initial declaration used outside C99 mode
Line 42: warning: passing argument 1 of 'printf' makes pointer from integer without a cast
Line 52: error: 'for' loop initial declaration used outside C99 mode
Line 56: error: redefinition of 'i'
Line 52: error: previous definition of 'i' was here
Line 56: error: 'for' loop initial declaration used outside C99 mode
Line 65: error: 'for' loop initial declaration used outside C99 mode
Line 72: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


Comments:
posted by matth0x1 on Oct 19
int i = 0;

//then
for(...

// be sure to reuse 'i' for anyother loops where appropiate
reply