Re: Looping Statement

Hi! I am working with the codes below:
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;
int main()
{
int x, y;
int i; 
int sum46 = 0;
int ave37 = 0;
int prodd = 1;
	cout << "Enter a number: ";
	cin >> x;
	cout << "\nEnter a larger number: ";
	cin >> y;
	
	for (i=x; i<=y;i++)
	{
		if (i % 4 == 0 && i % 6 == 0)
		{
			sum46=sum46+i;
		}
			if (i % 3 == 0 && i % 7 == 0)
			{
				ave37+=i;
				int ctr = 0;
				ctr++;
				ave37/=ctr;
			}
			else
			{
				break;
			}
	}
	for (i=x<i<=y;i+=2)
	{
		if (i%2!=0)
		{
			prodd*=i;
		}
	}
	cout << "\nA. Total of values divisible by 6 & 4: " << sum46 << endl;
	cout << "\nB. Average of values divisibe by 7 & 3:  " << ave37 << endl;
	cout << "\nC. Product of all odd values: " << prodd << endl;
return 0;
}


My a and b are good to go in this program. However, my c--which prints out the product of all odd numbers between x and y--seem to have problems if I input values bigger than 5. I suspect that this is due to the conditions of odd numbers in which a value increases by 2, unlike my increment in the for loop. Thoughts?
Last edited on
Maybe you should fix line 33 first:
for (i=x<i<=y;i+=2)
That won't even compile.

When you fix it, i needs to go up in steps of 1, not 2. For example, if x is even - say x =6 - then steps of 2 would just give you 6, 8, 10, 12, ... none of which are odd.
Hi
first your condition for for loop is not right here
for (i=x;i<=y;i+=2)

second: with this i+=2 if the first number is even the if will not have the correct condition. what you want to do exactlly?

third: I did not understand the usage of ctr in ave37?
it will be always 1!!!
Last edited on
Hello aftselakhis,

Your program is on the right track and has many usable parts, but dors have some problems.

Line 2. It is best not to use this.

Line 8. Since this is for an average I made it a "double" so the final answer is more correct. Integer division tends to round the number not will because it will drop the decimal portion of the calculation. And when you get to the part ave37/=ctr; you should check that "ctr" is not zero before you do your division and the program crashes.

Something like:
1
2
if (ctr)
    ave37/=ctr;

Comes to mind right off.

Line 9. Because "prodd" can become a very large number you need to learn what different data types can hold and what their limits are. Have a look at this https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx Right now you are a long long way from using the right data type.

Here is an example:

Enter a number: 2

Enter a larger number: 45

A. Total of values divisible by 6 & 4: 72

B. Average of values divisibe by 7 & 3:  31.5

C. Product of all odd values: 9481495526376579231


 Press Enter to continue


Notice the value of line C.

Line 19. You can use "+=" here as you used "/=" and "*=".

Line 24. saeidsj is correct. Each iteration through the for loop and then each time you enter the if statement you are defining and setting "ctr" to zero then adding one, so the most it can be is one. It works better to move this definition at the top with the rest of your variables.

Lines 28 - 31. If neither if statement is true the else part will break out of the for loop to early. You do not need this else part.

Line 33. Has been covered.

Lines 35 - 38. To deal with only the odd numbers after line 37 add the line i==;. Now every time the if statement is entered you will only be using the odd numbers.

Lastly watch your indenting. Line 21 should line up with line 17. And line 43 should be indented.

Hope that helps,

Andy

Edit:
Last edited on
Topic archived. No new replies allowed.