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?
And the function :Code:for(i=0 ; i< numOfBL ; i++) printf("\n"); ans = getans();
And in the getans() function inside while loop. how does that work?.Code:for (i=0; i<numOfBL; i++) printf("\n");
Thank a lot guys. Ones I know the answers I will modify the program as teacher said.Code:while (ans == -1) { ans = getchar( ); ans = tolower(ans); }
Thanks