1. ## 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 two-point 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!

2. Q1) One way to approach this would be to make a new variable that counts how many of x,y,z are true.

Q2) C syntax does not allow things like
Code:
if( a < x < b) /* do stuff */
You need to think of it like "If a is less than x and x is less than b".

Q3) I don't understand what is meant by "sliding window" so I have no idea what the question is about.

Q4) Well, technically speaking, it won't print anything because your code contains no printf. Why not run your code (with printf) and check whether you are right?

Q5) Try it and see!

3. 1) So how would I go about doing that? Sorry, I am a beginner.

2) So would this code work:

Code:
if(x >= 3.0 && x <= 2.0) y = x;
else y = 0
3) Is there any other way to solve it without the "sliding window"?

4) I tried it and got 4 numbers (0,3,6,9).

5) The code I made worked.

4. Bump! I need help!

5. I understand #2, 3 and 5. I still have no clue about #1 and 4.

6. Originally Posted by JParker
Bump! I need help!
REALLY???

well you know how to do and if statement right?

so it is simple,

compare it like you would on paper

you have 3 numbers,
take two and compare them
then take two more and compare them

and you KEEP mentioning "sliding window" but yet dont tell us what your even talking about, so no help there!

PS, this isnt facebook, things like BUMP and posting over and over with no patience for us to help, just ........ people off and they dont help you

7. 3) Given the initialization: double z1 = 1.0, z2 = z1/2; use z1,z2 as a two-point 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.
I am guessing this is a way to calculate epsilon; but, I am not sure.
How to (portably) get DBL_EPSILON in C/C++ - Stack Overflow

I kept getting the wrong value for epsilon; turns out the compiler optimizes the code and results in LDBL_EPSILON under MinGW GCC.
http://en.wikipedia.org/wiki/Machine...mation_using_C

Tim S.

8. Originally Posted by stahta01
I am guessing this is a way to calculate epsilon; but, I am not sure.
How to (portably) get DBL_EPSILON in C/C++ - Stack Overflow

I kept getting the wrong value for epsilon; turns out the compiler optimizes the code and results in LDBL_EPSILON under MinGW GCC.
Machine epsilon - Wikipedia, the free encyclopedia

Tim S.
???

Sorry, but I have no clue what this means. I really wish I knew C

9. #1 is easy:

Count up the number of true's in the variables x,y, and z.
Code:
//false is 0, anything else is true
#define false 0
#define true !false

int trueCount=0;
if(x!=0) trueCount++;
if(y!=0) trueCount++;
if(z!=0) trueCount++;

Then a simple
if(trueCount > 1)
a=1;
It's always tempting to work out more elegant and complex code logic - but avoid being as complex or elegant as you can. By definition, to debug such code, you have to be MORE elegant and complex, and that isn't always possible - or as clear.

10. 3) Given the initialization: double z1 = 1.0, z2 = z1/2; use z1,z2 as a two-point 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.
Looks for me that you are supposed to write a for-loop.

Bye, Andreas

11. The loop will execute 4 times. Increments are made before the test of the next loop.
Code:
int main(void) {
int i, x=0,n=0;
for( i = 0; i < 11; i += 3)
{
x += i;++n;
}

printf("loop counter: %d\n",n);
return 0;
}

12. Spamming the net.

13. Originally Posted by AndiPersti
Looks for me that you are supposed to write a for-loop.

Bye, Andreas
I used a while loop to do it, myself.
But, I tend not to use for loops except for constant iteration loops.

Tim S.