How to Create a C++ Program to Separate and Add Even and Odd Integers?

  • Context: C/C++ 
  • Thread starter Thread starter SqrachMasda
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around creating a C++ program to separate any number of integers into odd and even categories and calculate their respective sums. Participants explore various programming techniques, loop structures, and optimization strategies related to this task.

Discussion Character

  • Exploratory
  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant suggests using the condition `if i%2==0` to determine if a number is even and `if i%2==1` for odd numbers, while also mentioning the need for a loop to process multiple integers.
  • Another participant proposes a shorthand using the ternary operator to add integers to even or odd sums based on their parity.
  • A different approach is presented using bitwise operations, specifically `i&1`, as a more efficient method for checking odd/even status compared to `i%2`.
  • Some participants discuss the importance of algorithmic complexity and optimization, with one suggesting that learning assembly could provide deeper insights into processor operations.
  • There is a mention of a pseudo-code approach to eliminate branching in the logic for summing even and odd integers.
  • One participant expresses gratitude for the suggestions and notes a new technique involving a Y/N prompt for continuing input.

Areas of Agreement / Disagreement

Participants present multiple competing views on the best methods for implementing the program, including differing opinions on the use of bitwise operations versus modulus. The discussion remains unresolved regarding which approach is definitively superior.

Contextual Notes

Some participants highlight the need for understanding algorithmic complexity and optimization techniques, indicating that the discussion may not cover all necessary foundational concepts for beginners.

Who May Find This Useful

Individuals interested in programming, particularly in C++ and algorithm optimization, as well as those looking to understand different methods for handling integer categorization and summation.

SqrachMasda
Messages
42
Reaction score
0
i have to create a program to separate ANY number of integers into odds and evens and give back the sums of them separately

i'm thinking to use if i%2=0 then it is even
or if i%2 is .5 then it is odd or sumthing
i haven't learned much about how to set up these loop things so i don't know how to organize it and what headerfiles to use
any body got any hints, help, or answers :biggrin: ?
 
Technology news on Phys.org
You can do something like this:

(input%2) ? (even+=input) : (odd+=input) ;
 
if i%2 == 0 i is even
if i%2 == 1 i is odd

I guess you should know how to make a loop going throug all i. Whithin that loop you check whether i is even or odd, and if the numuber is even you add it to total_even and if it is odd you add it to total_odd.

For that you do not need any headerfiles. If you want to print the results you need the iostream headerfile.
 
SqrachMasda said:
i have to create a program to separate ANY number of integers into odds and evens and give back the sums of them separately

i'm thinking to use if i%2=0 then it is even
or if i%2 is .5 then it is odd or sumthing
i haven't learned much about how to set up these loop things so i don't know how to organize it and what headerfiles to use
any body got any hints, help, or answers :biggrin: ?

i believe you will need iostream.h only if working on Console
can be done in following way

#include<iostream>

using namespace std;
int main()
{
int i,sumEVEN=0,sumODD=0;
char ch;
do{
count<<"\n"<<"Enter the Number::";
cin>>i;
if(i%2==0)
sumEVEN += i;
else
sumODD += i;
count<<"Got more integers(Y/N)";
cin>>ch;
}while(ch == 'y' || ch == 'Y');

count<<"\n"<<"Even Sum::"<<sumEVEN;
count<<"\n"<<"Odd Sum ::"<<sumODD;

cin>>ch;

return 0;

}
 
Well, since the solution has already been posted I might as well give you the most optimized C version.

Code:
#include <stdio.h>

int main(void) {

int i,e=0,o=0;

do {
  printf("Enter a postive integer (-1 to quit): ") ;
  scanf("%i", &i) ;
  if(i>=0) (i&1) ? (o+=i) : (e+=i) ;
} while(i!=-1) ;

printf("Even Sum: %i\n", e) ;
printf("Odd Sum: %i\n", o) ;

return 0 ;

}
 
Last edited:
dduardo said:
Well, since the solution has already been posted I might as well give you the most optimized C version.

Shouldn't you be using
Code:
(i&1)
rather than
Code:
(i%2)
Just in case the compiler doesn't do that?
 
NateTG, your right, i&1 is more efficient. I changed my source to reflect the change. Good ol' bitwise operators.

i&1 = 0 when i is even
i&1 = 1 when i is odd

------
Also, if people are wondering what this is:

(i&1) ? (o+=i) : (e+=i) ;

it is called a ternary operator and can be generalized like this:

(condition) ? (expression if condition is true) : (expression if condition is false) ;

It's just a shorthand way to write:

if( i&1 ) o+=i ;
else e+=i ;
 
yes gr8 i have just completed basic java and cany say basic C++,
but still to learn the optimization and complexity.
Exactly i knew binary addition wud be much faster, but never applied coz maybe i m not professional.

Any links/tutorial for Complexity and how to increase Efficiecy of programs
Thanks
 
The best way in my opinion is to learn assembly. You really get a sense of what the processor is really doing.

http://maven.smith.edu/~thiebaut/ArtOfAssembly/artofasm.html

For pure algorithms start here:

Introduction to Algorithms by
Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein
 
Last edited by a moderator:
  • #10
himanshu121 said:
yes gr8 i have just completed basic java and cany say basic C++,
but still to learn the optimization and complexity.
Exactly i knew binary addition wud be much faster, but never applied coz maybe i m not professional.

Any links/tutorial for Complexity and how to increase Efficiecy of programs
Thanks

You should worry about computational i.e. algoritmic complexity.
The "i&1" vs "i%2" thing is really a joke.
 
  • #11
NateTG said:
You should worry about computational i.e. algoritmic complexity.
The "i&1" vs "i%2" thing is really a joke.

Ya, I though that was funny.
Here is a way to eliminate that pesky branch.
in a sort of c-psuedo code hybrid

int sum[2]={0,0}
while(i<= read another integer) {
sum[i&1]+=i;
}
 
  • #12
egsmith said:
int sum[2]={0,0}, i=0;
while(i<= read another integer) {
sum[i&1] += i++;
}
:smile:

I wonder if the original poster is interested in nice optimal solutions, assembly, etc. (noticing that the op stated: "i haven't learned much about how to set up these loop things")
 
  • #13
Thanks for the help, you all are great...I like that Y/N thing, that's new to me
and it works great
himanshu121 , thanks
 

Similar threads

  • · Replies 25 ·
Replies
25
Views
3K
Replies
81
Views
7K
Replies
69
Views
10K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
9
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K