Click here to Skip to main content
15,886,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

// Jones, Bradley L.; Peter Aitken; Dean Miller. C Programming in One Hour a Day, Sams Teach Yourself (p. 357). Pearson Education. Kindle Edition.
// Write a function named sumarrays() that accepts two arrays as arguments, totals all values in both arrays, and returns the total to the calling program.

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.
Posted
Updated 19-Aug-21 4:00am
v3
Comments
jeron1 19-Aug-21 10:01am    
for(loop = 9; arrayc[loop] >= 0; loop--) { <== the testing condition is incorrect, in both places.

1 solution

Compare your 2 piece of code doing the additions :
C++
   for(loop = 9; loop >= 0; loop--) {
      sum = sum + array[loop];
   }
...
    for(loop = 9; arrayc[loop] >= 0; loop--) {
      sum = sum + arrayc[loop];
   }

Do you see something ?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Mieczyslaw1683 19-Aug-21 10:17am    
Okey. Now I have seen the wrong code and fixed it (should be loop/loop2 instead of arrayc[loop]/arrayc2[loop2]). I will learn some more about debbuging also. Thank you for the help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900