Help plz

I am a beginner of c++.I have a homework about "sumodd". It's like that:
Someone input two values,valueA and valueB. It should be add the odd numbers from valueA to valueB by using for loop.How can I get it?

For example,valueA=7 valueB=11. Therefore, the sum = 7+9+11, it's equal to 27.
Last edited on
1) Read in the two input numbers
2) Work out all the odd numbers within the range of those inputs
3) Keep a running total of the odd numbers
Yes,I know the general process.However, can you write the code to me?
Write this if you want to fail your test ) , otherwise think for yourself.

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
26
27
28
29
30
int main()
{
 int a, b;
 int max;
 int min;
 int sum = 0;

 cin >> a;
 cin >> b;

 if ( a > b)
 {
 max = a;
 min = b;
 }
 else
 {
 min = a ;
 max = b;
 }

                             
 for(int i = min; i <= max ; i++)
  if( i % 2 == 1) sum += i;
 

 cout << "sum is :" << sum;

return 0;
}


you might want it to check for bad input and modify the code a little bit
Last edited on
I may be wrong, but this website isn't about giving people answers. it's about offering advice and help.
If we wrote your homework for you, how would you transition our of being a beginner in C++? MikeyBoy gave you a good starting point.

How about you try writing the program, and come back here with specific questions. There are a lot of people on this forum who will answer questions and give suggestions, but you have to put in some effort to get thing rolling.

I will add to MikeyBoy's pseudo-code:

1.5) Determine which number is is greater than the other.
First consider if one or both values are even, not odd, and positive.
some quasi code fragments:
int x
read x
if x/2*2==x the x is even, else it is odd.
if x is even, then x+1 and x-1 are odd (for x>0)
int sum = 0
then for int i =LoOddValue to HighOddValue sum = sum + i

print sum

This is not c or c++ code but you get the idea.
I am not going to write the code for you, since your question is not really one of coding.
It is one of algorithms:
1. How to add integers regularly spaced
2. How to check if an int x is even or odd.
I assume you know how to code input and output and syntax etc.
If not, do some more reading of looping and control structures on this site.
Learning takes effort......
go to it! and Good luck
Ex. When it is finished, test for these inputs
7, 13
7, 14
6 13
6 14
-3, 5

Then consider what it would take to make the program add the EVEN numbers between two inputs like those given

When that is sorted, can you conceive of a SINGLE algorithm (ie. not one with the above two algorithms in it) that accepts two integers and a 1 (odd) or a zero (even)
if 0 add the evens
if 1 add the odds
Last edited on
Sorry everybody.Since I had not the c++ programme in my computer, so I can't write the code here, but I wrote the code before this post.And here you are :)

#include <iostream>
using namespace std;

int main()
{
int valueA, valueB, smaller, large, sum=0;

cout << "Please input value A: ";
cin >> valueA;
cout << "Please input value B: ";
cin >> valueB;

if(valueA > valueB) //Here I can distinguish which num is large or smaller, am I right???
{large = valueA; smaller = valueB;}
else
{smaller = valueA; large = valueB;}

for (

cout << "Sum of odd integers from " << smaller << " to " << large << " is " << sum << endl;

return 0;
}

About the "for loop" part, how can i write the code???
Thx~
I saw a reply above, the for loop part is

for(int i = min; i <= max ; i++)
if( i % 2 == 1) sum += i;

I want to ask what's the "i"??? Can I don't add the "i" into "int valueA, valueB, smaller, large, sum=0;" this sentence and use the "i"???

And what is the meaning of "if( i % 2 == 1) sum += i;" ??? Here is one sentence or two sentences???
Last edited on
it's better/clearer formatted like this:
1
2
3
4
if( i % 2 == 1) 
{
    sum += i;
}


i.e. if i is odd, add this to your sum variable;
But can I add that before the for sentence???
Why would you want to do it before the for loop? Don't you want to do it for every number in the range?
why does no one use Code tags!??
closed account (48T7M4Gy)
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
26
27
#include <iostream>
using namespace std;

int main()
{
	int valueA, valueB, smaller, large, sum = 0;

	cout << "Please input value A: ";
	cin >> valueA;
	cout << "Please input value B: ";
	cin >> valueB;

	if (valueA > valueB) //Here I can distinguish which num is large or smaller, am I right???
	{
		large = valueA; smaller = valueB;
	}
	else
	{
		smaller = valueA; large = valueB;
	}

	for (

		cout << "Sum of odd integers from " << smaller << " to " << large << " is " << sum << endl;

	return 0;
}


thank you! Yes that tells you if it is larger or smaller. on line 22 that for loop is incomplete.
Last edited on
Topic archived. No new replies allowed.