Thread: Problem starting a homework problem for class. Please help!

  1. #16
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    maxorator: I would do something like this but we haven't learned any of this.
    jafet : We have not learned anything about arrays either.

    So I am not sure what to do next. Have gotten some helpful hints but nothing that makes me change my mind on what to do. If you have any other suggestions please let me know. Thanks!

  2. #17
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Which of these havent you learned: switch, for, fgets, printf, arrays?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #18
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    fgets and arrays are the two that I have not learned yet.

  4. #19
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    guys, arrays havent been discussed yet, but still all your suggestions involve arrays. LOL.

    zac_haryy , try this. this isnt the best solution but i think it works. I had this problem in my exam last year. here's how i solved it (without arrays);

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void) {
    	int input, num1=-1, num2=-1, num3=-1, num4=-1, num5=-1;
    	puts("Enter number:");
    	scanf("%d",&input);
    	num1=input%10;
    	input=input/10;
    	if(input!=0) {
    		num2=input%10;
    		input=input/10;
    	}
    	if(input!=0) {
    		num3=input%10;
    		input=input/10;
    	}
    	if(input!=0) {
    		num4=input%10;
    		input=input/10;
    	}
    	if(input!=0) {
    		num5=input%10;
    		input=input/10;
    	}
    	switch(num5) {
    		case 1: printf("one "); break;
    		case 2: printf("two "); break;
    		case 3: printf("three "); break;
    		case 4: printf("four "); break;
    		case 5: printf("five "); break;
    		case 6: printf("six "); break;
    		case 7: printf("seven "); break;
    		case 8: printf("eight "); break;
    		case 9: printf("nine "); break;
    		case 0: printf("zero "); break;
    	}
    	switch(num4) {
    		case 1: printf("one "); break;
    		case 2: printf("two "); break;
    		case 3: printf("three "); break;
    		case 4: printf("four "); break;
    		case 5: printf("five "); break;
    		case 6: printf("six "); break;
    		case 7: printf("seven "); break;
    		case 8: printf("eight "); break;
    		case 9: printf("nine "); break;
    		case 0: printf("zero "); break;
    	}
    	switch(num3) {
    		case 1: printf("one "); break;
    		case 2: printf("two "); break;
    		case 3: printf("three "); break;
    		case 4: printf("four "); break;
    		case 5: printf("five "); break;
    		case 6: printf("six "); break;
    		case 7: printf("seven "); break;
    		case 8: printf("eight "); break;
    		case 9: printf("nine "); break;
    		case 0: printf("zero "); break;
    	}
    	switch(num2) {
    		case 1: printf("one "); break;
    		case 2: printf("two "); break;
    		case 3: printf("three "); break;
    		case 4: printf("four "); break;
    		case 5: printf("five "); break;
    		case 6: printf("six "); break;
    		case 7: printf("seven "); break;
    		case 8: printf("eight "); break;
    		case 9: printf("nine "); break;
    		case 0: printf("zero "); break;
    	}
    	switch(num1) {
    		case 1: printf("one "); break;
    		case 2: printf("two "); break;
    		case 3: printf("three "); break;
    		case 4: printf("four "); break;
    		case 5: printf("five "); break;
    		case 6: printf("six "); break;
    		case 7: printf("seven "); break;
    		case 8: printf("eight "); break;
    		case 9: printf("nine "); break;
    		case 0: printf("zero "); break;
    	}
    	getch();
    }
    i found a bug though, if you type in multiple zeros, it only prints out 1 zero. but hey, there's no such number as multiple zeros right?
    and if you type in "0563", the zero isnt outputted. but i leave how to fix that up to you. 0563 isnt a legal number anyways.
    Last edited by sangken; 10-09-2006 at 06:48 PM.
    It is not who I am inside but what I do that defines me.

  5. #20
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    Thanks for the help!! I am going to take a look at this tomorrow.

  6. #21
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    "You may assume the input is less than one million."

    Then it's pruh-tty simple then, dah-lin. You divide X by 100,000. If the result is zero, then X is not larger than 100,000, so don't print anything. Otherwise print that digit. Then take X % 100,000, and repeat the process with 10,000, etc... until you reach 1.

    Code:
    12,345 / 100,000 = 0 don't print
    12,345 % 100,000 = 12,345
    12,345 / 10,000 = 1 print "one"
    12,345 % 10,000 = 2,345
    2,345 / 1,000 = 2 print "two"
    2,345 % 1,000 = 345
    etc...
    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;}

  7. #22
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    #include <stdio.h>
    
    int main(void) 
    {
        int mask = 100000;
        int input;
    
        printf("Enter number : ");
        scanf("%d",&input);
    
        while ( mask > 0 )
        {
           if ( (input >= mask) || ( (input == 0) && (mask == 1) ) )
           {
               switch( input / mask )
               {
                   case 0: printf("zero "); break;
                   case 1: printf("one "); break;
                   case 2: printf("two "); break;
                   case 3: printf("three "); break;
                   case 4: printf("four "); break;
                   case 5: printf("five "); break;
                   case 6: printf("six "); break;
                   case 7: printf("seven "); break;
                   case 8: printf("eight "); break;
                   case 9: printf("nine "); break;
               };
    
               input %= mask;
           }
           mask /= 10; 
        }
        printf("\n");
    
        /* Pause if you want */
    
        return(0);
    }

  8. #23
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    hmmm, i thought that the data type int could only hold values from (-32,746) to (32,747)??? so im assuming the all beyond those values would be sifted out.

    but then again, that's a different case if he's gonna use long. i just assumed int.
    It is not who I am inside but what I do that defines me.

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try using a modern compiler.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #25
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    ahh so in modern compilers, int isnt 4bytes big anymore? ok
    It is not who I am inside but what I do that defines me.

  11. #26
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    4 bytes = 32 bits (assuming 8-bits/byte)

    32-bits unsigned range is 0 to +4294967295
    32-bits signed range is -2147483648 to +2147483647

    Perhaps you were thinking of the range of a (16-bit) short?

  12. #27
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    ahhh.. jeeze.. didnt know that.. last time i heard (from my instructor), int can only hold those aforementioned values. my my, how ancient am i.
    It is not who I am inside but what I do that defines me.

  13. #28
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by sangken
    ahhh.. jeeze.. didnt know that.. last time i heard (from my instructor), int can only hold those aforementioned values. my my, how ancient am i.
    Well, it depends really on what you mean by "can only hold..."

    The standard defines INT_MIN and INT_MAX in limits.h, as:
    Code:
    INT_MIN = -32767
    INT_MAX = 32767
    However, more compilers use 32bit integers now, so the size the variable can use is much larger. But if you're trying to be portable with old compilers, don't assume anything past the above. Naturally you can test the size of the integer on your compiler. Or use long instead if you really require larger numbers.

    In your teacher's case, I suspect they were used to teaching on some old piece of crap like Borland's old TC 3.0 or something, so that's the maximum you could actually fit in an int. Most schools (or their students that show up here anyway) seem stuck in the 16bit era.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Me with this homework problem Please!!
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2002, 07:54 PM
  2. C Homework Problem
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-25-2002, 03:43 AM
  3. Homework problem
    By bacon in forum C Programming
    Replies: 1
    Last Post: 04-24-2002, 07:21 AM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM
  5. homework assignment problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-29-2001, 10:24 AM