GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Nov-2005, 14:09
jack223 jack223 is offline
New Member
 
Join Date: Nov 2005
Posts: 15
jack223 is on a distinguished road

how do i find(list them) and sum them up?


here is what i have so far...i need to list all the odd number and sum them up at the end....any help please.

CPP / C++ / C Code:
#include <iostream>

using std::cout;

using std::cin;

using std::endl;

#include <iomanip>

using std::setw;


const int MAX = 10;


int main()

{

int odd = 0;

	int MyArray[MAX] = {2, 3, 4, 5, 7, 10, 23, 45, 50, 77}; 


	int odd = 0;

	for (int i = 0; i < MAX; i++)
	{	MyArray[i];

		if ((MyArray[i] % 2) == 0)
		{
			odd++;
		}
	}


	cout << "Element" << setw(13) << "Value" << endl;
	
	for ( int j = 0; j < MAX; j++)
		cout << setw(7) << j << set(13) << MyArray[j] << endl;

	cout << "Count odd numbers: " << odd << "\n" << endl;
	
	return 0 ;
}
Last edited by LuciWiz : 03-Nov-2005 at 15:27. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 03-Nov-2005, 15:42
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: how do i find(list them) and sum them up?


Hi jack,

Welcome to the GID Forums.
Did you read the guidelines?

Quote:
here is what i have so far...i need to list all the odd number and sum them up at the end....any help please.
So, to sum all the odd numbers, we can have an extra variable called sum.
the sum is initialized to zero at the start.
CPP / C++ / C Code:
    int sum = 0;

Then, to check whether a number is odd or not, we have to use like this:
CPP / C++ / C Code:
if ( ( MyArray[ i ] % 2 ) == 1 )
because % will give us the remainder when dividing a number.
For example, consider that we have 5.
5 % 2 will be 1. whereas
4 % 2 will be 0.

So, the remainder should be 1 for odd number.

Now, how to calculate the sum?
We can calculate the sum when we count the odd numbers itself.
like this:
CPP / C++ / C Code:

for (int i = 0; i < MAX; i++)
{

if ((MyArray[i] % 2) == 1)
{
odd++;
sum += MyArray[i];
}

}

Then you can display the count and sum in the end.

Here is what you should remove:
1. The extra int odd = 0; You can declare a variable only once.
So, you can use int sum = 0; instead.
2. In the for loop:
CPP / C++ / C Code:
for (int i = 0; i < MAX; i++)
{ MyArray[i];
there is no need for that MyArray[i] here. You can remove it.
3. In this line:
CPP / C++ / C Code:
cout << setw(7) << j << set(13) << MyArray[j] << endl;
the set should be setw.

In the next line,
CPP / C++ / C Code:
cout << "Count odd numbers: " << odd << "\n" << endl;
We can use endl instead of "\n".

Summarising, here is the code
CPP / C++ / C Code:
int sum = 0;

for (int i = 0; i < MAX; i++ )
{ 
    if ( ( MyArray[ i ] % 2 ) == 1 ) //check whether the number is odd or not.
    {
       odd++;                       //increase the odd number count
       sum += MyArray[i];       //add the odd numbers
    }
}

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 03-Nov-2005, 16:25
jack223 jack223 is offline
New Member
 
Join Date: Nov 2005
Posts: 15
jack223 is on a distinguished road

Re: how do i find(list them) and sum them up?


thanks Paramesh.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:45.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.