March 21st, 2013, 05:17 PM

Homework Help...?
Hello, I'm a beginner to the C language. Today, I got assigned homework, and I need help. Below are the questions and what I got (but I do not think it is right):
1) Write a C statement which will set a to 1 if at least two of the three integer variables x, y, z are true (i.e. if any two or all three are true).
I know it's an if/else statement, but I don't understand what to put (like how to write it so that just 2 or all 3 variables need to be true for a to equal 1).
2) Write a C statement to perform the following operation: if x is between 3.0 and 2.0 inclusive, set y equal to x, otherwise set y to zero.
Code:
if (3.0 <= x <=2.0) y = x;
else y = 0;
3) Given the initialization: double z1 = 1.0, z2 = z1/2; use z1,z2 as a twopoint sliding window (i.e. z1=z2; z2=z1/2; slides the window) and write a loop to find the value of z1 such that 1 + z1 is greater than 1 but 1 + z2 is equal to 1.
No clue how to solve it.
4) How many times is the body of the following loop executed?
Code:
int i, x=0;
for( i = 0; i < 11; i += 3)
{ x += i; }
I put 5. My rationale is that it'll print 0, 3, 6, 9, and 12 (it'll add 3 to 9 because 9 is less than 11; after 12 the statement is invalid). Is this right?
5) Write a for statement using integer counter i which will print the integer values starting at 0 and ending with 44.
Code:
int i; for(i = 0; i <= 44; ++i)
{ printf( "%i \n" , i); }
Can anyone help? I really want to learn this language. Help is much appreciated!
March 21st, 2013, 05:33 PM

I ♥ ManiacDan & requinix
This is a sig, and not
necessarily a comment on the OP:
Please don't be a
help vampire!
March 21st, 2013, 05:48 PM

Yeah, I posted it here to see if I can get more replies.
March 21st, 2013, 05:50 PM

5)
Did you test it? ;)
2)
You cannot write it like this, you have to separate the condition:
Code:
if (3.0 <= x && x <=2.0) y = x;
else y = 0;
4)
almost correct: it will do the condition validation with i being 12, but the body of the loop will not be executed as 12 is not lower than 11.
3)
Sounds like you will have to use a loop,
Not sure if you have the question correct, but if it is, I strongly suggest you try look at it at the "opposite" direction:
Which value will z2 have to make 1+z2 equal to 1?
What value will z1 have to give the correct z2?
what will the value of 1+z1 be?
How does this match against the rule that 1+z1 should be greater than 1?
1)
how is the integers classified as true? as 1 or nonzero?
But you should be able do it with if similar to the code from 2).
Using 1 as true:
[code]
if(x==1 & y == 1) {
} else if (..) {
}
[code]
You will then have to find all the combination that will set a=1.
if integers is true for all nonzero value, you wil have to look for "not 0" value.
March 21st, 2013, 09:33 PM

1) I don't really understand the process.
2) Got it! :)
3) Not sure where to start with the loop.
4) Is it 4? I ran the program and got 0, 3, 6, and 9.
5) Got it! :)
March 22nd, 2013, 02:43 AM

@TParker
In addition, for future reference, the following free online book may be helpful
http://publications.gbdirect.co.uk/c_book/the_c_book.pdf
Regards,
Dariyoosh