Lost ("short int" and "while" statements)

This is a discussion on Lost ("short int" and "while" statements) within the C Programming forums, part of the General Programming Boards category; Got a problem to work out this week for my C class. I don't even understand what it's asking. Write ...

  1. #1
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31

    Lost ("short int" and "while" statements)

    Got a problem to work out this week for my C class. I don't even understand what it's asking.

    Write a program to identify the smallest negative number and largest positive number that can be stored in a variable of data type “short int”. Your program MUST use while statement (and break if necessary) only for looping.

    Call this program positive-negative.c.
    Declaration of a short int variable: short int <variable name>;
    Can anyone help me understand what it's asking me to do? Perhaps in more of the laymans terms style?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,625
    You need to find out the highest number a variable of short int can store.
    A variable can only hold a finite number, remember.
    So the exercise asks you to use a loop to find the highest positive number it can store and the highest negative number it can store.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Big hint: if you add one to the largest value a data type can store, it "wraps" with overflow. In other words, <max>+1 is 0, for an unsigned number, or something negative for a signed number.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    Something like...

    Code:
    #include <stdio.h>
    main()
    {
    short int c = 0;
    short int lowest;
    short int largest;
    
    do {
    lowest = c;
    c--;
    } while c < 0;
    
    largest = c;
    
    printf(lowest);
    printf(largest);
    }
    ...?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,625
    No way, no how. The logic is correct, but the code is incorrect.
    While you're at it, indent the code and let main return int.
    And you shouldn't spoil homework for others. They won't learn.
    Last edited by Elysia; 02-26-2008 at 06:16 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    To be more specific, the idea works but the syntax is atrocious.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    Wanna correct my code? It's been forever since I took C.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by theonetruehero View Post
    Wanna correct my code? It's been forever since I took C.
    That's no fun. But I will tell you that there's a parse error before "c" on the line that says
    Code:
    } while c > 0;
    and that you are converting integer to char * without a cast on the two "printf" lines.

    (IOW, you need to look up the syntax of do-while (the conditional needs to be in parentheses) and printf (the first argument is what should actually be printed).)

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    That's no fun. But I will tell you that there's a parse error before "c" on the line that says

    Code:
    } while c > 0;

    and that you are converting integer to char * without a cast on the two "printf" lines.

    (IOW, you need to look up the syntax of do-while (the conditional needs to be in parentheses) and printf (the first argument is what should actually be printed).)
    Ack. I think I fixed the problem with the integer to char portion. But I really have no recollection of what would make the other line mess up. Here's what I got:

    Code:
    #include <stdio.h>
    main()
    {
    short int c = 0;
    short int lowest;
    short int largest;
    
    do {
    lowest = c;
    c--;
    } while c < 0;
    
    largest = c;
    
    printf("Smallest: %d Largest %d", lowest, largest);
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It still needs to be "int main". And the conditional of a do-while needs to be in parentheses (the same with a while, or an if).

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    hmm maybe

    Code:
    #include <stdio.h>
    int main()
    {
            short int c = 0;
            short int lowest;
            short int largest;
    
            do
            {
               lowest = c;
               c--;
            }  while(c < 0, largest = c);
    
    printf("Smallest: %d Largest %d", lowest, largest);
    }

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,625
    Nope.
    First you can't do "x, y" in a condition and second you don't have to.
    You aren't supposed to assign in the condition.

    More correctly:
    Code:
    #include <stdio.h>
    int main()
    {
            short int c = 0;
            short int lowest;
            short int largest;
    
            do
            {
               lowest = c--;
            }  while(c < 0);
    
    	largest = c;
    	printf("Smallest: &#37;d Largest %d", lowest, largest);
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    even better, just use limits.h

    Screw the while loop, that's what limits.h is for.

    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main(void)
    {
        printf("Short min: &#37;d, Short max: %d\n", SHRT_MIN, SHRT_MAX);
        return 0;
    }
    It's obvious that's not what they want you to do... but the above is how I'd do it -- even if I was given this problem.
    Last edited by zacs7; 02-26-2008 at 06:30 PM.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,625
    I believe the exercise is more about to find out how memory and unsigned/signed and variables works, not finding out about the maximum value they can hold.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    First you can't do "x, y" in a condition and second you don't have to.
    First -- you're wrong. Second, I concur.

    Ever seen code like this?
    Code:
    for(x = 0, y = 1; ...
    That's using the comma operator. Same idea anywhere else: the left side is evaluated, then the right side, and the whole expression gets the value of the right side. So this
    Code:
    do
        code();
    while(expression());
    is the same as
    Code:
    do
        ;
    while(code(), expression());
    (Your compiler might want you to put extra parentheses around there, you never know. Oh, and the semicolon is necessary. )

    I was going to suggest limits.h, too. If you read the problem carefully:
    Your program MUST use while statement (and break if necessary) only for looping.
    In other words, when your program loops, you have to use a while loop, and not, say, a for loop. But I figured that would be temping fate . . . .

    There's another way you can do this problem, much more efficiently, and still using a while loop. Consider the fact that the maximum value of any data type with most systems is one less than a power of two . . . or that an unsigned value, subtracted by one, gives you the maximum unsigned value, and the signed value should be easy to figure out from there. Okay, you don't need a loop for that solution. And both of these are rather unportable, so just ignore the suggestions -- just some random thoughts.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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