Hi. I understand the problem, but you have to put up some confidence. I was at your position a year ago, and I still have problem writing codes. Everyone does.
To really clear up the problem here, let me walk through the whole program with you again.
1. Ask the user to input x
2. Ask the user to input y
3. Now, evaluate x,y ( x must be less than or equal to y, and both x and y must be greater than or equal to 0)
4. If x,y meets the requirement, go on to #5. Otherwise, print out an error message asking it again (we can just let the program die in this case)
5. Now, let's ask the user to choose [1] for even, [2] for odd
6. If the user choose [1], we print out only the even numbers that is between x and y (including x and y)
7. If the user choose [2], we print out only the odd numbers that is between x and y (including x and y)
8. end of the program
Yes. Use what I provide, and let's start from there.
I don't want to destroy your code so I said keep the structure.
Quote
for(i = x;i < y;i++)
{
if(z=1)
printf("Even numbers from %d to %d:\n",x,y);
You are one the right track. You are asking the right question. How do we print out the even / odd numbers? Well. I said keep the structure, and in programming, we can often use comparison to evaluate two or more things.
But first let me correct you one little thing.
if (z = 1) is wrong, because the equal sign = is not valid when we are trying to compare something. The statement if (z = 1) is trying to see whether z is equivalent to the integer 1.
In general, when we have a variable, let say
int x = 2, we are assigning the variable which is an integer, the value 2. This = sign means "assignment". In other words, x now holds an integer value "2".
Remember, whenever you try to evaluate something (usually when we say evaluate, we are suggesting comparison, or checking something... true/false, for example), you don't use = for comparison.
There is this == double equal signs operator. We use this equal operators for comparison.
Does x has the value of 2? x == 2 true since int x = 2, right?
So we have to fix that.
if(z==1)
printf("Even numbers from %d to %d:\n",x,y);
Now let;s answer your question: how do I print out only the even? Well, let's give a little thought. You are iterating (looping) from x to y. (let say x = 2, and y = 8)
So the first value we take is i = x, which is now i = x = 2, thus i = 2, right?
This is how a for loop works:
let's take a simple for loop
for ( int aNumb = x; aNumb < y; aNumb++)
{
printf("%d", aNumb) // we want to print every value that aNumb is assigned to
}[1] the very first time the loop begins with the first value assigned to aNumb (our variable is aNumb in this case)
[2] then the compiler moves to the second piece of the for loop --> aNumb < y
[3] If #2 is true, we jump down the printf statement. Since aNumb is 2, the compiler will print 2
[4] Now, there is no furthur instructions follows by the printf statement, so we go back to the aNumb++ increment piece.
[5] This increment made aNumb = 2 increased by 1, so aNumb is now aNumb = 3
[6] Now we check the 2nd piece again: aNumb < y??? TRUE
[7] now printf statement
[8] increment by 1, so aNumb = 4
...... and so on
[nth] so until aNumb = 7 (and remember our y = 8), great. Is aNumb < y ?? True! So printf statement.
Then increment by 1, omg, aNumb = 8, and this fail the condition aNumb < y since 8 < y, so the for loop comes to an end.
As you can see, each time we going through the current number of aNumb, the compiler will check the condition aNumb < y, and if it is true, we continues.
Since we are constructing an if-statement: if the user enters [1]. But to save time and space, we can do another check simultaneously.
If we do the check [1] or [2] and checking even and odd numbers separately, we will require 4 if statements, and 2 for loops. To keep your construct, we only require 1 for loop, and 2 if statements. The following is the outcome:
ASK FOR AN INPUT [1] or [2] (z variable):
if ( z == 1)
{
print only the even through a loop
}
if ( z == 2)
{
print only the odd one through a loop
}That's a waste of time and bad logic.
Look at your original program. We begins the for loop
AFTER the user instructs [1] or [2]. We have that value saved in the variable
z . Thus, we don't need to worry about losing track of it. We can use it at any time we want in this particular program.
After the users put down [1], z = 1, good. Now the loop begins, it begins with i = x (let say x = 2), so i = 2. Great.
We can now print out the even numbers using the modulo method.
Basically:
a_number % 2 == 0 is even
(a_number % 2 != 0) is odd
a_number % 2 == 1 is odd
The modulo method gives the remainder of a division. For an even number, the reminder is always zero. Plain zero.
So what if we do the check z == 1 and i%2 == 0 inside the same if condition, and connect them using the AND operator && ??
Note that && is the AND operator, and || (two bars) is the OR operator
If we check both (z == 1 && (i%2 == 0)) and is true, then the compiler will only print out the even numbers.
We perform a similar code for the odd conditions.
When you print, you are printing the variable i. Remember, x and y are just the initial and end points. Your loop is going from x to y, and in between there are other values too! So if you just printf x,y you will never get anything.
So your printf statement should only print the variable i
PS: For EE student, don't underestimate the power of programming, and the need of programming. You definitely need it.