COMP 1917 Computing 1
Session 2, 2016

Tutorial Solutions - Week 3


  1. Write a program that given three numbers first, second and third, finds out if the numbers are in strictly descending order (first > second > third). Is there an alternative way to write this program?
      #include <stdio.h>
     
      int main(int argc, char* argv[]){
         int first, second, third;
         printf("Enter numbers: ");
         scanf("%d %d %d", &num);
         if(first > second && second > third){
            printf("ASCENDING!!!!");
        }else{
            printf("What a shame:(");
        }
    
         return 0;
      }
    
      #include <stdio.h>
     
      int main(int argc, char* argv[]){
         int first, second, third;
         printf("Enter numbers: ");
         scanf("%d %d %d", &num);
         if(first > second){
            
            if(second > third){
                printf("ASCENDING!!!!\n");
            }else{
                printf("What a shame:(\n");       
            }
    
         }else{
            printf("What a shame:(\n");
         }
    
         return 0;
      }
    
  2. Modify previous program to check if three numbers are in ascending or descending order.
      #include <stdio.h>
     
      int main(int argc, char* argv[]){
         int first, second, third;
         printf("Enter numbers: ");
         scanf("%d %d %d", &num);
         if(first > second && second > third){
            printf("ASCENDING!!!!\n");
         }else if(third > second && second > first)
            printf("DESCENDING!!!!\n");
         }else{
            printf("I don't know what that order is :/\n");
         }
    
         return 0;
      }
    
  3. Write a program that allows the user to enter at integer and then prints out that many stars, each on a new line.
    % ./stars
    Please enter an integer: 5
    *
    *
    *
    *
    *
    
    #include 
    
    int main( void )
    {
       int num;
       int row;
    
       // Obtain input
       printf("Enter number: ");
       scanf("%d", &num);
       for( row = 1; row <= num; row++ ) {
          printf("*\n");
       }
    
       return 0;
    }
    
  4. Consider the following code:
     1 #include <stdio.h>
     2
     3 int main( void )
     4 {
     5    int num;
     6    int row, col;
     7
     8    // Obtain input
     9    printf("Enter number: ");
    10    scanf("%d", &num);
    
    11    for(col = 1; col <= num; col++){
    12       printf("*");
    13    }
    14    printf("\n");
    15
    16    for(row = 2; row < num; row++) {
    17        for(col = 1; col <= num; col++) {
    18            if(col == 1 || col == num){
    19                printf("*");
    20            }else{
    21                printf(" ");
    22            }
    23        }
    24        printf("\n"); // Start a new line
    25    }
    26
    27    for(col = 1; col <= num; col++){
    28       printf("*");
    29    }
    30
    31    return 0;
    32 }
    
    What is the output if the user types in the number 4?
    ****
    *  *
    *  *
    ****
    
    Modify the program so that it prints a triangle instead of a square:
    $ ./triangle
    Enter n ? 6
    *
    **
    ***
    ****
    *****
    ******
    
    Change line 12 to:
              for(col = 1; col <= row; col++) {
    
    How would you change your program to print the triangle in different orientations?
    (a) ******   (b)  ******    (c)     *
        *****          *****           **
        ****            ****          ***
        ***              ***         ****
        **                **        *****
        *                  *       ******
    
    (a) change line 12 to:
              for(col = 1; col <= num+1-row; col++) {
    
    (b) replace lines 12-14 with:
              for(col = 1; col < row; col++) {
                  printf(" ");
              }
              while( col <= num ) {
                  printf("*");
                  col++;
              }
    
    (c) replace lines 12-14 with:
              for(col = 1; col <= num-row; col++) {
                  printf(" ");
              }
              while( col <= num ) {
                  printf("*");
                  col++;
              }
    
  5. Write a function
    int isPrime( int n )
    
    which accepts a positive integer n and returns 1 if n is prime, 0 otherwise. (Recall that an integer is prime if it has no factors other than itself and 1).
    int isPrime( int n )
    {
      int result;
      int factor = 1; // 1 is always a factor
      int k=2;
      /*
         keep searching until we find a factor larger than 1,
         or we have exhausted all possibilities
      */
      while( k*k <= n && factor == 1 ) {
        if( n % k == 0 ) {
          factor = k;
        }
        k++;
      }
      // not prime if n is 1, or n has a factor larger than 1                       
      if( n == 1 || factor > 1 ) {
        result = 0;  // n is not prime                                
      }
      else {         // no factors                                    
        result = 1;  // n is prime                                    
      }
      return( result );
    }
    
  6. Write a program that generates two random numbers between 1 and 107 and check if a smaller number is a factor of a larger.
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(){
        int a, b, temp;
        srand(time(NULL));
        a = rand() % 107 + 1;
        b = rand() % 107 + 1;
    
        if(a < b){
            temp = a;
            a = b;
            b = temp;
        }
    
        if(a % b == 0){
            printf("%d is a factor of %d\n",b, a);
        }else{
            printf("%d is not a factor of %d\n", b, a);
        }
    
        return 0;
    }