I have no problem writing a function that takes an array of int as a parameter and returns the sum in the array; however, using sumOdd to find the odd numbers in the array either does not work or I am not writing it correct. So, my problem is not finding the sum but finding the sum of odd numbers.
Here is my code which works for finding the sum:
// Module 8.2: ques8dot2.cpp
// Compute the sum of the elements of the array
#include <iostream>
using namespace std;
int main()
{
const int arraySize = 16;
int x[ arraySize ] = { 25, 10, 55, 33, 27, 7, 9, 81, 14, 39};
int total = 0;
// sum contents of array x
for ( int i = 0; i < arraySize; i++)
total += x[ i ];
cout << "Total of array elements:" << total << endl;
return 0;
}
Here, I attempted to find the sum of odd but it does not work:
#include <iostream>
using namespace std;
int main()
{
const int arraySize = 16;
int x[ arraySize ] = { 25, 10, 55, 33, 27, 7, 9, 81, 14, 39};
int sumOdd = 0;
// sum contents of array x
for ( int i = 0; i < arraySize; i++)
sumOdd += x[ i ];
cout << "Sum of odd integers of array elements:" << sumOdd << endl;
return sumOdd;
}
Any help would be appreciated......

New Topic/Question
Reply




MultiQuote




|