Thread: VERY newb homework assignment

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    3

    VERY newb homework assignment

    here is my hw assignment. i have absolutely no experience with C. so any help would be appreceated

    write a program to calculate the highest and lowest number that can be stored in: a char, an int (signed and unsigned), a short int, a long int. once you have done this look at the include files limits.h and float.h do the values in these files match the ones you calculated?

    i really have no idea where to start because this isnt in my book, itwas just assigned to us expected that we know some C.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Read up on loops and integer overflow.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    What you need to do is first find out how many bytes each of these types uses. There are 8 bits in each byte and every bit doubles the amount of combinations of those bits.
    So for an unsigned char (1 byte) it would be 2^8 or (2*2*2*2*2*2*2*2)=256 (0-255). BTW a signed value uses 1 bit as the sign bit.

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Have two variables of each type, one to be incremented and one to hold the previous value. Then make a loop that increments the variable by one and stores it's value in the other variable before incrementing. Then check if the values are the same (which means it has hit a maximum/minimum) and break the loop at that point.
    Do another loop for signed integers which decrements instead to get the minimum value.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Hint: use the fact that many types are represented, stored and manipulated as powers of 2. Thus the limits must also be a power of 2.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    Registered User vinit's Avatar
    Join Date
    Apr 2006
    Location
    India
    Posts
    39
    Hint: use the fact that many types are represented, stored and manipulated as powers of 2. Thus the limits must also be a power of 2.
    Jafet is right, just keep in mind about sign & unsigned
    e.g. if int is tored in two bytes then its range is "-32768 to +32767" (signed int) i.e 2^15
    if unsigned int then its "0 to 65535" i.e 2 ^ 16

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. cool math homework assignment
    By BobMcGee123 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-23-2007, 08:21 PM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Need help with homework assignment plz..
    By RVDFan85 in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2006, 10:33 PM
  5. Frustration with Homework assignment
    By Jake in forum C++ Programming
    Replies: 3
    Last Post: 10-31-2001, 01:26 PM