Ceng230.01 - hw04.

Due: Wed, July 24, 2002


Take the mod_#questions(your7-digit-studentID), the result will give you which one of the below assignments you are supposed to do. (note, in this assignment, the #questions is 6.)

For example, say, your id is 1234567, then the question you'll solve is:
1234567 % 6 = 1.
(if you get '0' as a result, you'll be solving the 4th question.)


  1. Write a program to print-out geometrical shapes. You must first read a character specifying the shape, and then read the numbers specifying the shape. You must have a function to draw each of these shapes. See the sample-run below.
    which shape ? (s)quare, (r)ectangle, (t)riangle : s
    enter the length of a side of the square : 6
    
    ******
    ******
    ******
    ******
    ******
    ******
    
    would you like to continue (y/n) ? y
    which shape ? (s)quare, (r)ectangle, (t)riangle : r
    enter height and width of the rectangle : 6 8
    
    ********
    ********
    ********
    ********
    ********
    ********
    
    would you like to continue (y/n) ? y
    which shape ? (s)quare, (r)ectangle, (t)riangle : t
    enter the height of the triangle : 7
          *
         ***
        *****
       *******
      *********
     ***********
    *************
    
    would you like to continue (y/n) ? n
    
    
    /* solution by: eda pinar atasoy */
    
    #include <stdio.h>
    /* globals */
    int i,j,k,side,x,y,h;
    int blank,star;
    char c,again;
    
    /* function declerations */
    void square();
    void rect();
    void tri();
    
    int main()
    {	
    start:
    	printf("which shape ? (s)quare, (r)ectangle, (t)riangle : ");
    	scanf("%s", &c);
    
    	if(c=='s')
    		square();	
    	else if(c=='r')
    		rect();
    	else if(c=='t')
    		tri();
        printf("would you like to continue ? ");
    	scanf("%s", &again);
    	if(again=='y')
    		goto start;
    
    	return 0;
    }
    
    /* functions */
    void square()
    {
    	printf("enter the length of a side of the square : ");
    	scanf("%d", &side);
    
    	for(i=0; i<side; i++)
    	{
    		for(j=0; j<side; j++)
    			printf("*");
    		printf("\n");
    	}
    }
    
    void rect()
    {
    	printf("enter height and width of the rectangle : ");
    	scanf("%d %d", &y, &x);
    
    	for(i=0; i<y; i++)
    	{
    		for(j=0; j<x; j++)
    			printf("*");
    		printf("\n");
    	}
    }
    
    void tri()
    {
    	printf("enter the height of the triangle : ");
    	scanf("%d", &h);
    
    	blank=h-1;
    	star=1;
    
    	for(i=0; i<h; i++)
    	{
    		for(j=0; j<blank; j++)
    			printf(" ");
    		for(k=0; k<star; k++)
    			printf("*");
    		printf("\n");
    		star += 2;
    		blank--;
    	}
    }
    	
    
    

  2. Write a program that prompts the user to enter a series of characters from the keyboard terminated by the newline character ('\n'). This program will output a summation of how many upper case letters, how many lower case letters, how many blank spaces and how many other characters were encountered and the total number of characters.
    Sample-run:
    enter the input string: This is JUst a Sample Run of the stupid homeWork. 
    
    Upper case letters : 6 
    Lower case letters : 33 
    Blank spaces       : 9 
    Other characters   : 1 
    Grand total        : 49 
    
    
    Notes:
    /*solution by: Mert Yetkin*/
    #include<stdio.h>
    int a,up=0,low=0,blank=0,other=0,tot=0;
    void main()
    {
     char a;
    while((a = getchar()) !='\n'){
       if(a>='A' && a<='Z') up++;
       else if(a>='a' && a<='z') low++;
       else if(a== ' ') blank++;
       else other++;
       tot++;
    }
    printf("upper case letters=%d\nlower case letters=%d\nblank characters=%d\nother characters=%d\ngrand total=%d\n",up,low,blank,other-1,tot-1);
    }
    
    
    

  3. Write a function that computes the circumference of a circle, given its radius. Write another function that computes the surface area of a circle, given its DIAMETER. Also write the main function that gets the radius as input, uses your functions to compute the circumference and area, and prints the circumference&area. Use #define directive to define PI = 3.13159.
    /*solution by: Berkin Ugurlu */
    #include <stdio.h>
    
    #define PI 3.13159
    
    float circum(int r){
     return 2*PI*r;
    }
    float area(int d){
     return PI*d*d/4;
    } 
    int main ()
    {
    int r;
    printf("Enter radius: ");
    scanf("%d",&r);
    printf("Circumference =%f\n",circum(r));
    printf("Area =%f\n",area(2*r));
    return 0;
    }
    
    

  4. Write a function that computes takes 5 floating point arguments and computes the average and standard deviation (STD); prints out the average and returns the standard deviation. Also write a main program that gets the five values as input and uses the function to compute the STD. Recall that the standard deviation is the square-root of the sum of the squared differences between the values and the mean divided by the number of values.
    /*by: Evgueni Stepanov*/
    #include <stdio.h>
    #include <math.h>
    
    int main ()
    {
    float num1, num2, num3, num4, num5;
    double std;
    double stdf(float, float, float, float, float);
    
    	printf("Please enter 5 numbers: ");
    	scanf("%f %f %f %f %f", &num1, &num2, &num3, &num4, &num5);
    	std = stdf(num1, num2, num3, num4, num5);
    	printf("Standard deviation:\t%f\n", std);
    	
    system("PAUSE");
    return 0;
    }
    
    double stdf(float num1, float num2, float num3, float num4, float num5)
    {
    double std, average, x;
    
    	average = (num1 + num2 + num3 + num4 + num5)/5;
    	printf("Average :\t\t%lf\n", average);
    	x = ((num1 - average) * (num1 - average) + 
    		 (num2 - average) * (num2 - average) + 
    		 (num3 - average) * (num3 - average) + 
    		 (num4 - average) * (num4 - average) + 
    		 (num5 - average) * (num5 - average))/5;
    	std = sqrt(x);
    
    return std;
    }
    
    

  5. Print out a 10x10 multiplication table.
    Sample output:
        1   2   3   4   5   6   7   8   9   10  
        ==  ==  ==  ==  ==  ==  ==  ==  ==  ==
    1 | 1   
    2 | 2   4   
    3 | 3   6   9   
    4 | 4   8   12  16  
    5 | 5   10  15  20  25  
    6 | 6   12  18  24  30  36  
    7 | 7   14  21  28  35  42  49  
    8 | 8   16  24  32  40  48  56  64  
    9 | 9   18  27  36  45  54  63  72  81  
    10| 10  20  30  40  50  60  70  80  90  100 
    
    
    /* by Ahmet SACAN */
    #include <stdio.h>
    
    void NewLine(){
     printf("\n");
    }
    
    void main(){
     int r,c;
    
     printf("     ");
     for(c=1; c<=10; c++)
      printf("%d   ", c);
     NewLine();
    
     printf("     ");
     for(c=1; c<=10; c++)
      printf("==  ");
     NewLine();
    
     for(r=1; r<=10; r++){
      printf("%2d | ", r);
      for(c=1; c<=r; c++)
       printf("%-4d", r*c);
      NewLine();
     }
    }
    
    

  6. Write a function that randomly selects a card from a poker deck.
    • There are 13 cards: 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, and A
    • The J, Q, and K have a numeric value of 10.
    • The A has a numeric value of 11.
    • Each card has an equal probability of being selected.
    The function should:
    • Print the randomly selected card.
    • Return the numeric value of the card as an integer variable
    Write a main program to do the following:
    • Use the functions time and srand to seed the random number generator.
    • Use the function above to generate 2 random cards
    • Add up the two numeric values returned by the functions and print their sum.
    /*by Ahmet SACAN
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    const char SUITS[4][12] = {"hearts", "diamonds", "clubs", "spades"};
    const char CARDS[13][8] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9",
     "10", "Jack", "Queen", "King"};
    int SelectCard(){
     int num, suit;
     suit = rand()%4;
     num = rand()%13;
    
     printf("%s of %s\n", CARDS[num], SUITS[suit]);
     return num == 0 ? 11 : num >= 9 ? 10 : num+1;
    }
    
    void main(){
     int x,y;
     srand((unsigned)time(NULL));
     x = SelectCard();
     y = SelectCard();
     printf("your total is: %d\n", x+y);
    } 
    
    


Bonus Questions

  1. Guess Game
    Write a program that keeps a number between 1 and 1000 "in mind", and keeps asking the user until he/she guesses the number correctly. For his/her each guess, the program must tell whether the number entered is lower or higher than the correct number. After the guessing-game is finished, you must prompt how many tries it took the user to find the number, and ask whether to play again.
    Sample-run:
    
    I have a number between 1 and 1000.
    Can you guess my number?
    Please type your first guess.
    ? 50
    Too low. Try again.
    ? 500
    Too low. Try again.
    ? 750
    Too low. Try again.
    ? 900
    Too high. Try again.
    ? 850
    Too low. Try again.
    ? 875
    Too low. Try again.
    ? 890
    Too low. Try again.
    ? 895
    Too low. Try again.
    ? 897
    Too low. Try again.
    ? 899
    Too high. Try again.
    ? 898
    
    You guessed the number in 11 tries.!
    Would you like to play again?
    Please type ( 1=yes, 2=no )? 2
    
    Thanx for playing the number-guessing game. Yine bekleriz.
    
  2. Write a function that computes the square root of a floating point number. You CAN NOT use the math library. You must actually compute this yourself. The function should take a float as an input argument and should return a float. You should also write a function that takes no arguments, prompts the user for the input, uses scanf to obtain a float, checks to make sure its not negative, and then returns that float. Also write a main program that uses both functions and prints the square root.

    Square roots can be computed using an algorithm called the Babylon method. The algorithm is described at:
    http://personal.bgsu.edu/~carother/babylon/Babylon1.html
    The algorithm requires a number of steps (or iterations) before it converges to a precise value. Your function should perform at least 20 iterations.
    Sample Run:
    
    enter a positive number : 4
    iteration 00  : 1.000000
    iteration 01  : 2.500000
    iteration 02  : 2.050000
    iteration 03  : 2.000610
    iteration 04  : 2.000000
    iteration 05  : 2.000000
    iteration 06  : 2.000000
    iteration 07  : 2.000000
    iteration 08  : 2.000000
    iteration 09  : 2.000000
    iteration 10  : 2.000000
    iteration 11  : 2.000000
    iteration 12  : 2.000000
    iteration 13  : 2.000000
    iteration 14  : 2.000000
    iteration 15  : 2.000000
    iteration 16  : 2.000000
    iteration 17  : 2.000000
    iteration 18  : 2.000000
    iteration 19  : 2.000000
    iteration 20  : 2.000000
    would you like to continue (y/n) ? y
    
    enter a positive number : 3673.1
    iteration 00  : 1.000000
    iteration 01  : 1837.050049
    iteration 02  : 919.524780
    iteration 03  : 461.759674
    iteration 04  : 234.857117
    iteration 05  : 125.248421
    iteration 06  : 77.287468
    iteration 07  : 62.406319
    iteration 08  : 60.632072
    iteration 09  : 60.606113
    iteration 10  : 60.606106
    iteration 11  : 60.606106
    iteration 12  : 60.606106
    iteration 13  : 60.606106
    iteration 14  : 60.606106
    iteration 15  : 60.606106
    iteration 16  : 60.606106
    iteration 17  : 60.606106
    iteration 18  : 60.606106
    iteration 19  : 60.606106
    iteration 20  : 60.606106
    would you like to continue (y/n) ? n
    

  3. Write the TicTacToe game as assigned in asg03.html#b2, using arrays.