COMP1911 23T2 Introduction to Programming
  1. Consider the two code snippets below, what will be the output after execution of each of the programs for inputNum = 3.
    int inputNum, i; 
    
    printf("Enter a number: "); 
    scanf("%d", &inputNum); 
    
    i = inputNum; 
    
    if(i <= 5){
      printf("%d\n", i * i); 
      i++; 
    }
    
    int inputNum, i; 
    
    printf("Enter a number: "); 
    scanf("%d", &inputNum); 
    
    i = inputNum; 
    
    while(i <= 5){
      printf("%d\n", i * i); 
      i++; 
    }
    

  2. Discuss the errors in these while loops
    int i;
    
    while (i < 100) {
        printf("%d\n", i);
        i = i + 1;
    }
    

    int i = 0;
    int j = 0;
    
    while (j = 1 || i < 100) {
        printf("%d\n", i);
        i = i + 1;
    }
    

    int i = 0;
    int n = 10;
    while (i < n) {
        printf("%d\n", i);
        n = n + i;
        i = i + 1;
    }
    

    int i = 0;
    while (i < 10)
        printf("%d\n", i);
        i = i + 1;
    

    • Using pseudo code, write an algorithm for printing the numbers from k down to 1 together with their squares. You can assume k is 10

    • Translate the pseudo code you have just written into a functioning C program. Use a while loop.

    • Modify your C code so that your program, after printing a number and its square on a line, prints the string "Number is even" if the number (not its square) is even and otherwise prints the string "Number is odd" if the number is odd.

  3. Write a program that reads in an integers and prints out that many asterisks, each on a new line.
    ./asterisks
    Please enter an integer: 5
    *
    *
    *
    *
    *
    

  4. Write a program that repeatedly reads in integers until a negative integer is read in. Your program should print out how many integers were read in. For example:
    ./readInts
    Please enter some integers:
    10 100 999 78 -3
    You entered 4 integers
    
    ./readInts
    Please enter some integers:
    1 2 3 4 5 6 7 8 9 
    10 11 12 -9
    You entered 12 integers
    

  5. Write a C program multipleOfTen.c which reads 2 integers and then prints all multiples of ten between those numbers.

  6. Write a C program log10.c which reads a positive integer, and calculates the integer part of its base 10 logarithm. Hint: repeatedly divide by 10.

  7. Consider the following code
    #include <stdio.h>
    
    int main(void) {
        int number;
        int row, column;
    
        // Obtain input
        printf("Enter number: ");
        scanf("%d",&number);
    
        row = 1;
        while (row <= number) {
            column = 1;
            while (column <= number) {
                printf("*");
                column = column + 1;
            }
            printf("\n");
            row = row + 1;
        }
    
        return 0;
    }
    
    
    What is the output if the user types in the number 5?

    Modify the program so that it prints out a triangle that consists of number asterisks eg
    Enter number: 5
        *
       **
      ***
     ****
    *****
    

  8. Write a program diagonal.c that reads an integer and prints the following pattern:
    ./diagonal
    Enter an integer: 10
    *
     *
      *
       *
        *
         *
          *
           *
            *
             *
    

  9. Write a program cross.c that reads an integer and prints the following pattern. You may assume your input is a positive integer >= 5
    ./cross
    Enter size: 5
    *---*
    -*-*-
    --*--
    -*-*-
    *---* 
    

  10. Write a program that reads two integers (height x length) and prints a rectangular, asterisk outline with the specified dimensions, for example:
    ./rectangle
    Enter rectangle height and length: 3 5
    *****
    *   *
    *****
    

  11. Design and write a program that an integers and prints a diamond, asterisk outline with the specified side length, for example:
    ./diamond
    Enter side length: 3
      *
     * *
    *   *
     * *
      *
    ./diamond
    Enter side length: 6
         *
        * *
       *   *
      *     *
     *       *
    *         *
     *       *
      *     *
       *   *
        * *
         *
    

  12. Write a program that reads in an integer n, and then prints a nxn multiplication table. For example:
    ./multiplicationTable
    Enter multiplication table size: 5
     1|   1    2    3    4    5
     2|   2    4    6    8   10
     3|   3    6    9   12   15
     4|   4    8   12   16   20
     5|   5   10   15   20   25
    

  13. What is a function? Discuss the main features of functions? What is the type of a function? What is a function prototype? What are some of the benefits of using functions?

  14. A student wrote the following code to read in a number, n and print out n asterisks.
    void getSquare(void);
    
    int main(void){
        int n;
        printf("Enter a number ");
        scanf("%d",&n);    
        getSquare();
        printf("The square of %d is %d\n", n, sqr);
        return EXIT_SUCCESS;
    }
    
    int getSquare(void) {
        int i;
        int count = 1;
    
        i = 0;
        while ( i < n ) {
            count *= n
            i = i + 1;
        }
        printf("\n");    
    }
    

    It had the following compile time error:

    getSquare.c:17:17: error: use of undeclared identifier 'n'
        while ( i < n ) {
    

    What is wrong with the code? How can we fix it?