I'm trying to write a C++ program that adds elements in an array which are not divisible by 2. i initialized my array in the main function and created another function in which using a for loop and an if statement, I try to extract the numbers in the array which are not divisible by 2. Everything seems to fine until i run the program and it prints several numbers which i can't seem to make any sense out of
#include <iostream>
#include <string.h>
using namespace std;
//sum of all numbers that are not divisible by 2
int findDivisibleNumbers(int a[], int n, int sumofnumbers)
{
for (int i = 0; i < n; i++) {
if (i % 2 != 0)
sumofnumbers = sumofnumbers + a[i];
cout << "Sum of values not divisible by 2 = " << sumofnumbers << endl;
}
return 0;
}
int main()
{
` int arrayOfCrazyNumbers[] = { 11,2446,2343,144,65,26,17,8,29,10,238126,1912338 };
findDivisibleNumbers(arrayOfCrazyNumbers, sizeof(arrayOfCrazyNumbers), 0);
}
when i run the program its prints this output;
Sum of values not divisible by 2 = 0
Sum of values not divisible by 2 = 2446
Sum of values not divisible by 2 = 2446
Sum of values not divisible by 2 = 2590
Sum of values not divisible by 2 = 2590
Sum of values not divisible by 2 = 2616
Sum of values not divisible by 2 = 2616
Sum of values not divisible by 2 = 2624
Sum of values not divisible by 2 = 2624
Sum of values not divisible by 2 = 2634
Sum of values not divisible by 2 = 2634
Sum of values not divisible by 2 = 1914972
Sum of values not divisible by 2 = 1914972
Sum of values not divisible by 2 = -857078488
Sum of values not divisible by 2 = -857078488
Sum of values not divisible by 2 = -1716071948
Sum of values not divisible by 2 = -1716071948
Sum of values not divisible by 2 = -1716071937
Sum of values not divisible by 2 = -1716071937
Sum of values not divisible by 2 = 1738090781
Sum of values not divisible by 2 = 1738090781
Sum of values not divisible by 2 = 1739319245
Sum of values not divisible by 2 = 1739319245
Sum of values not divisible by 2 = 1743381485
Sum of values not divisible by 2 = 1743381485
Sum of values not divisible by 2 = 1743381486
Sum of values not divisible by 2 = 1743381486
Sum of values not divisible by 2 = 1754875318
Sum of values not divisible by 2 = 1754875318
Sum of values not divisible by 2 = 1766408486
Sum of values not divisible by 2 = 1766408486
Sum of values not divisible by 2 = 1770470818
Sum of values not divisible by 2 = 1770470818
Sum of values not divisible by 2 = 929666308
Sum of values not divisible by 2 = 929666308
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 932097113
Sum of values not divisible by 2 = 932097113
Sum of values not divisible by 2 = 932097113
C:\COM205_ProgrammingTwo2021Q1\CPlusPlus\BasicDataTypeMemoryAndPointersPt1\Debug\BasicDataTypeMemoryAndPointersPt1.exe (process 21016) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
what could be the problem?