Hi
I have tried to make a program with a function, sumarrays(), that should sum 2 arrays and then add the 2 sums together and then print the value.
I dont know why I get a wrong value (155) instead of the correct one (90) and therefore I want one explanation atleast.
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int array2[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int sumarrays(int arrayc[], int arrayc2[]);
#include <stdio.h>
int main() {
int sum = 0, sum2 = 0, loop;
for(loop = 9; loop >= 0; loop--) {
sum = sum + array[loop];
}
for(loop = 9; loop >= 0; loop--) {
sum2 = sum2 + array2[loop];
}
printf("Sum of array is %d.\n", sum);
printf("Sum of array2 is %d.\n", sum2);
printf("Sum of array + sum of array2 is %d.\n", sum + sum2);
printf("Sum of array + sum of array2 is %d (through function sumarrays()).", sumarrays(array, array2));
return 0;
}
int sumarrays(int arrayc[], int arrayc2[])
{
int sum = 0, sum2 = 0, loop, loop2;
for(loop = 9; arrayc[loop] >= 0; loop--) {
sum = sum + arrayc[loop];
}
for(loop2 = 9; arrayc2[loop2] >= 0; loop2--) {
sum2 = sum2 + arrayc2[loop2];
}
return sum + sum2;
}
What I have tried:
I have tried to look at a working function in my C programming book that takes 1 int array and finds the biggest value in it. To see if there is some wrong programming that I have done with the program with the sumarrays() function.