Like Tree1Likes
  • 1 Post By rags_to_riches

Homework Help...?

This is a discussion on Homework Help...? within the C Programming forums, part of the General Programming Boards category; Hello, I'm a beginner to the C language. Today, I got assigned homework, and I need help. Below are the ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    7

    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!
    Last edited by JParker; 03-21-2013 at 02:52 PM.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    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!
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    7
    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.
    Last edited by JParker; 03-21-2013 at 04:02 PM.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    7
    Bump! I need help!

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    7
    I understand #2, 3 and 5. I still have no clue about #1 and 4.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    Quote Originally Posted by JParker View Post
    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. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    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.
    Last edited by stahta01; 03-21-2013 at 10:14 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  8. #8
    Registered User
    Join Date
    Mar 2013
    Posts
    7
    Quote Originally Posted by stahta01 View Post
    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. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    #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
    
    //In your function:
    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. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    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. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #12
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    Spamming the net.
    Matticus likes this.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    Quote Originally Posted by AndiPersti View Post
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my c++ homework please
    By dmeyers81 in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2010, 08:26 PM
  2. Please help me with my c++ homework!
    By dmeyers81 in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2010, 07:06 PM
  3. Help for Homework!
    By alionas in forum C Programming
    Replies: 4
    Last Post: 11-20-2010, 09:36 AM
  4. Homework
    By cagurtay in forum C Programming
    Replies: 3
    Last Post: 11-01-2010, 05:28 PM
  5. homework help
    By computerjunkie5 in forum C++ Programming
    Replies: 13
    Last Post: 10-27-2003, 10:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21