Finding Odd in Array

Finding Sum of Odd in Array

Page 1 of 1

3 Replies - 6173 Views - Last Post: 18 April 2007 - 07:17 PM Rate Topic: -----

#1 mccannfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-April 07

Finding Odd in Array

Posted 15 April 2007 - 06:52 PM

I need to write a function that takes an array of int as a parameter and returns the sum of odd numbers in the array.
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......

Is This A Good Question/Topic? 0
  • +

Replies To: Finding Odd in Array

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Finding Odd in Array

Posted 15 April 2007 - 07:04 PM

First things first...you have declared your array to be of size 16, you are assigning it far less than that many elements.

Second, you only wish to add the number to the total if it is an odd number - you are adding all the items. To identify an odd number, you can use the modulus operator:

http://www.cprogramm...al/modulus.html

If the result of the number in question being modded with 2 is zero, you know the number is even...therefore is the result is not 0, you know the number is odd, and should be added to the total.

if(x[i]%2!=0)
   total += x[i];


Was This Post Helpful? 0
  • +
  • -

#3 mccannfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-April 07

Re: Finding Odd in Array

Posted 18 April 2007 - 06:25 PM

View PostAmadeus, on 15 Apr, 2007 - 07:04 PM, said:

First things first...you have declared your array to be of size 16, you are assigning it far less than that many elements.

Second, you only wish to add the number to the total if it is an odd number - you are adding all the items. To identify an odd number, you can use the modulus operator:

http://www.cprogramm...al/modulus.html

If the result of the number in question being modded with 2 is zero, you know the number is even...therefore is the result is not 0, you know the number is odd, and should be added to the total.

if(x[i]%2!=0)
   total += x[i];



I made some adjustments; however I now receive the error 14 `i' undeclared (first use this function) of course this repeats for every 'i'

My changed code is:

#include <iostream>
using namespace std;
int main()
{
// int sumOdd;
int sumOdd=0;

const int arraySize = 10;
int x[ arraySize ] = { 25, 10, 55, 33, 27, 7, 9, 81, 14, 39};

// sum contents of array x
x[i]%2!=0

for (int i=0;i<arraySize;i++) {
if (x[i] % 2 != 0)
sumOdd += x[i];
}
cout << sumOdd << " sum of odd number ";

return 0;

}

Thanks for your help and if you can offer any help I would appreciate it. :crazy:
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Finding Odd in Array

Posted 18 April 2007 - 07:17 PM

See, the problem is you've simply copied and pasted the example given...it was meant to demonstrate the technique to be used, and was not meant as something to be stuck right into your program.
x[i]%2!=0


that line in and of itself means nothing...why do you have it stuck in the middle of the code?

As for the error itself, if it says i is undeclared, what does that tell you to do?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1