Can some on help me ):

Hello everyone
can anyone help me with this question
i know it looks easy but i don't understand functions


the question :
Write function totalOdd(int x, int
y)
that receives two integer numbers x and y. It
returns total of all Odd numbers from x to y.
Write
another
function total
Even
(int x, int y)
that receives two integer numbers x
and y. It returns total of all
Even
numbers from x to y.
In main function prompt user to enter the value of x and y, then call both functions and receive the results and print out the results.
Example:
If users enters
x = 4 and y = 10 then output
from totalOdd function will
be 5+7+9 = 21, and output from totalEven function will be 4+6+8+10
= 28
It's just asking you to sum up all the odd/even numbers from x to y. Just write a for loop from x to y that sums the numbers if they're odd/even and return the sum.

Here's some pseudocode for the totalOdd function:
1
2
3
4
5
6
7
int totalOdd( int x, int y) { 
	int sum = 0;
	for (//from x to y ) {
		//if odd, add to sum
	}
	return sum;
}
Last edited on
^^
i am sorry but can i get the full code ?
Hello Cmd,

what exactly do you not understand? What functions?
Have you tried finding it by yourself?
Did you try to solve the problem?

i like red pandas showed you pseudocode that is simple and good enough to try to implement it. Why don't you start writing code and see if you can make it? We, people helping here, really like to help, but we don't want to do your homework. We want you to learn something. Try by yourself. You will see it is not that hard. If you are stuck at something - tell us, but you need to specify your problem. It can't be "I got this task and have no idea how to do it".

And honestly right now I can't help you much more. It would require me to provide you with full code, which I will not do.

I can only tell you, that you will need to use cin and cout(so you need to include <iostream>), operators %(modulo), +, any loop(but for loop fits best), and you need to write these functions. It's really simple task. You will surely do it without much effort.

Good luck!
this is my work
and i think its not correct at all
please help me
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std; 
int totalEven(int x, int y)
{
	int sumEven=0;
	for(x=0; x<=100; x++)
{
if(x%2=0){
y+=x;
}
return sumEven;
} 

int totalOdd( int x, int y) { 
	int sumOdd = 0;
	for (x=0; x<= y; sum++) {

	}
	return sumOdd;
}

int main()
{
	
Last edited on
If you combined your both functions, you would get one working function :P

Firstly: why are you using your own numbers in totalEven? You don't use x and y at all; and you should use them!
Also, == is how you check if one variable is equal to other in C++. = is used for defining variable(like x = 10; it's a statement. But x==10 is a question).
Now, in second function you created loop nearly correctly; you don't have to always initialize something in for loop. Also, inside body of for loop, you didn't include anything, and it would be infinite loop, because you incremented some non-existing variable sum(have you tried compiling it?).

totalEven would look like this:
1
2
3
4
5
6
7
8
9
10
int totalEven(int x, int y)
{
    int sum = 0;
    for(x = x+1; x < y; ++x ) 
    {
        if( x % 2 == 0)
            sum += x;
    }
    return sum;
}

My method takes all numbers between x and y, but not x or y.
So if you want to check for sum of even numbers between 3 and 4, my program would output 0 - even though 4 is even, program checks numbers BETWEEN(and between 3 and 4 you don't have any integer :) )

Hope it helped. Try to write totalOdd basing on knowledge you gained. If you do not understand my function, try to explain to yourself step by step what this function does, and if you do not know some particular step - ask.

Cheers!
Last edited on
The for loop should be from x up to and including y. Thus, totalEven would look like:

1
2
3
4
5
6
7
8
9
10
int totalEven(int x, int y)
{
    int sum = 0;
    for(x; x <= y; ++x ) 
    {
        if( x % 2 == 0)
            sum += x;
    }
    return sum;
}


Then totalOdd is pretty much the same thing except with a different conditional.
Topic archived. No new replies allowed.