Thread: Newbie college student

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    77

    Newbie college student

    Hi guys its me again..ok I am working on a new assignment now. I last one I got full marks . Thanks guys.

    Ok teacher gave us this code and he wants us to rewrite it. I don't understand some parts of it so I will post the code here.

    (a) Modify the program below to provide the following functions:-

    (1) to draw the face value 1 of the dice
    (2) to draw the face value 2 of the dice
    (3) to draw the face value 3 of the dice
    (4) to draw the face value 4 of the dice
    (5) to draw the face value 5 of the dice
    (6) to draw the face value 6 of the dice
    (7) write a function to find the face value of the dice using
    random generator. This function prototype is as follows:-

    int randn(int n); /* where n controls the range of numbers */

    (8) write a function to print blank lines, The function prototype
    is as follows :-

    void printBLine(int n) /*where n is the # of blank lines */

    (b) use separate file compilation. Use the example of part (G) in the notes as your reference. It outlines how you separate the files and the steps required to compile, run and produce your output.
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    char getans();
    
    int main()
    {
    int r=0, i=0, numOfBL=2;
    char ans=0;
    
    ans=getans();
    srand(time(0));
    
    while (ans=='y' || ans=='Y')
    
    {
    r=rand()% 6+1;
    
    /* Write blank lines*/
    
    for (i=0; i<numOfBL; i++)        
    printf("\n");
    
          switch (r)
          {
            case 1 : printf("\n * \n"); 
                          break;
            case 2 : printf(" * \n");
                          printf(" * \n");   
                           break;
            case 3 : printf("*   \n");
                          printf(" *  \n");
                          printf("  * \n");  
                          break;
            case 4 :  printf(" * * \n\n");
                          printf(" * * \n"); 
                          break;
            case 5 : printf(" * * \n");
                          printf("  *  \n");
                          printf(" * * \n"); 
                         break;
            case 6 : for(i=0 ; i<3 ; i++)
                             printf(" * * \n");
                         break;
          }  /*endSwitch */
         
          /* write blank lines */
          for(i=0 ; i< numOfBL ; i++) 
                printf("\n");
           ans = getans();
        }
    
        /* write blank lines */
         for(i=0 ; i< numOfBL ; i++) 
                printf("\n");
    
         return 0;
    }
    
    char getans()
     {
       int ans = -1 ;
       printf("Throw y/n ?");
       while (ans == -1)
        {
          ans = getchar( );
          ans = tolower(ans);
        }
       fflush(stdin); 
       return ans;
    }

    Why does the two for loops has no starting and ending braces and why does the second for loop has a user definded function in it?
    Code:
     
    for(i=0 ; i< numOfBL ; i++) 
    printf("\n");
    ans = getans();
    And the function :
    Code:
    for (i=0; i<numOfBL; i++)
    printf("\n");
    And in the getans() function inside while loop. how does that work?.
    Code:
    while (ans == -1)
        {
          ans = getchar( );
          ans = tolower(ans);
        }
    Thank a lot guys. Ones I know the answers I will modify the program as teacher said.

    Thanks
    Last edited by jat421; 02-05-2005 at 05:20 PM.

  2. #2
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    Code:
    Why does the two for loops has no starting and ending braces and why does the second for loop has a user definded function in it?
    When for loop has no braces then it will work the same thing like the code were like this
    Code:
    for(i=0 ; i< numOfBL ; i++)
    { 
    printf("\n");
    }
    Code:
    And in the getans() function inside while loop. how does that work?.
    Code:
    
    while (ans == -1)
        {
          ans = getchar( );
          ans = tolower(ans);
        }
    ans = getchar() -> I think this is get one character!
    I dont know what is "tolower"
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  3. #3
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    I dont know what is "tolower"
    I think it means to lower case, likewise "toupper" would put the character in uppercase.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    Thanks a lot guy , that answers my questions. Back to modifying the program

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating in a sequential file?
    By Ronnyv1 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 04:41 PM
  2. Database assignment is Killing me!
    By Boltrig in forum C Programming
    Replies: 2
    Last Post: 11-29-2007, 03:56 AM
  3. Newbie college student :)
    By jat421 in forum C Programming
    Replies: 15
    Last Post: 01-22-2005, 09:22 PM
  4. LinkList Sorting in C
    By simly01 in forum C Programming
    Replies: 3
    Last Post: 11-25-2002, 01:21 PM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM