I have a problem, I am learning C and I am "translating / converting" my Java code to C to make it more efficient, just that I have a problem in the output: It is assumed that the output must be 11 15 18, but only the 15 leaves correctly and the others do not. The program makes the sum of data in the same position. In advance thanks for the help. JAVA code
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
int n;
n = leer.nextInt();
int a[] = new int[n];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
a[j] += leer.nextInt();
}
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
Code C
int main(){
int n,aux=0;
scanf("%i",&n);
int a[n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf("%i",&aux);
//printf("%i ",aux);
a[j]+=aux;
aux=0;
}
}
for(int i=0;i<n;i++){
printf("%i ",a[i]);
}
return 0;
}
Exit in C
10165323 15 10163090
Using:
Entry
3
1 2 3
3 4 5
7 8 9
Expected output
11 15 18