Tutorial 3 Solutions

  1. Explain what is wrong with the following while loops
  2. Design and write a program that reads in an integers and prints out that many asterisks, each on a new line. Implement one version using a while loop and another version using a for loop
    ./asterisks
    Please enter an integer: 5
    *
    *
    *
    *
    *
    
    #include <stdio.h>
    
    int main(void) {
        int i, n;
    
        printf("Please enter an integer: ");
        scanf("%d", &n);
    
        i = 0;
        while (i < n) {
            printf("*\n");
            i =  i + 1;
        }
    
        return 0;
    }
    
    #include <stdio.h>
    
    int main(void) {
        int i, n;
    
        printf("Please enter an integer: ");
        scanf("%d", &n);
    
        i = 0;
        for (i=0; i < n i++) {
            printf("*\n");
        }
     
        return 0;
    }
    
    

  3. Consider an algorithm for the sum of squares given input integer k. The program will print the number in the first column with their squares (power of 2) in the second.
  4. Consider the following code
    #include <stdio.h>
    
    int main(void) {
        int number;
        int row, column;
    
        // Obtain input
        printf("Enter number: ");
        scanf("%d", &number);
        for (row = 1; row <= number; row = row + 1) {
            for (column = 1; column <= number; column = column + 1) {
                printf("*");
            }
            printf("\n"); // Start a new line
        }
    
        return 0;
    }
    
    What is the output if the user types in the number 5?
    Enter number: 5
    *****
    *****
    *****
    *****
    *****
    

    Modify the program so that it prints out a triangle that consists of number asterisks eg

    Enter number: 5
        *
       **
      ***
     ****
    *****
    
    
    #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) {
                if (column <= (number - row)) {
                    printf(" ");
               } else {
                    printf("*");
               }
                column = column + 1;
            }
            printf("\n");
            row = row + 1;
        }
    
        return 0;
    }
    
    
  5. What is an array?

    An array is a collection of elements with the same data type. Each element is accessed providing the name of the array and an index. The index range is from 0 through to N-1, where N is the number of elements in the array. This is also known as zero-based indexing.

  6. Give an expression that sums the first and third element of an array called numbers?
    numbers[0] + numbers[2]
    
    Note third element is accessed using numbers[2] since C uses zero based indexing.
  7. If an array is declared as int numbers[20]; and your program assigns a value to each element in the array, what is the problem with the statement x = numbers[20]; ?

    Because arrays use zero-based indexing, accessing the element indexed 20 is accessing the 21st item in the array. This will access a value that is not within the boundaries of the array.

    Behaviour of a program that does this is undefined and it is possible, for example, that it will cause the program to terminate. Sometimes it will retrieve the value of another variable.

    How can you easily detect such errors before they have mysterious effects?

    You can use dcc to compile your program instead of gcc to help find these errors. It shows you what lines your code goes over the array bounds as your program is running.

    ~rz9280/bin/dcc -o prog prog.c
    
  8. How would you declare a variable squares to be an array of integers with 15 elements?
    int squares[15];
    

    Write a C code fragment to store, in each element of this array, the square of the index of that element, e.g., squares[5] would contain the value 25.

    #include <stdio.h>
    
    #define ELEMENTS    15
    
    int main(void) {
        int squares[ELEMENTS];
        int i;
    
        // The next two lines is what the question asks you to do
        for (i = 0; i < ELEMENTS; i = i + 1) {
            squares[i] = i * i;
        }
        // Let's print the array to check
        for (i = 0; i < ELEMENTS; i = i + 1) {
            printf("squares[%d] has the value %d\n", i, squares[i]);
        }
        return 0;
    }
    
  9. Write a C program which reads 5 numbers then reads another number and prints how many times that number occurred in the first 5. For example:

    a.out
    Enter 5 numbers: 1 3 1 3 1
    Enter a number: 1
    1 occurred 3 times in the 5 numbers read
    
    #include <stdio.h>
    
    #define N_NUMBERS 5
    
    int main(void) {
        int x[N_NUMBERS], i, j, match, count;
        printf("Enter %d numbers: ", N_NUMBERS);
        i = 0;
        while (i < N_NUMBERS) {
            scanf("%d", &x[i]);
            i = i + 1;
        }
        count = 0;
        printf("Enter a number: ");
        scanf("%d", &match);
        j = N_NUMBERS - 1;
        while (j >= 0) {
            if (x[j] == match) {
                count = count + 1;
            }
            j = j - 1;
        }
        printf("%d occurred %d times in the %d numbers read\n", match, count, N_NUMBERS);
        return 0;
    }
    

  10. Write a C program that calculates the dot-product of two vectors of integers.

    Reminder you calculate the dot product by multiplying corresponding elements and summing the result.

    For example:

    ./dot_product
    Enter vector 1 of 5 integers: 1 3 1 3 2
    Enter vector 2 of 5 integers: 2 1 2 1 2
    Their dot-product is 14.
    
    #include <stdio.h>
    
    #define VECTOR_LENGTH 5
    
    int main(void) {
        int vector1[VECTOR_LENGTH];
        int vector2[VECTOR_LENGTH];
        int i, dotProduct;
    
        printf("Enter vector 1 of %d positive numbers: ", VECTOR_LENGTH);
        for (i = 0; i < VECTOR_LENGTH; i = i + 1) {
            scanf("%d", &vector1[i]);
        }
    
        printf("Enter vector 2 of %d positive numbers: ", VECTOR_LENGTH);
        for (i = 0; i < VECTOR_LENGTH; i = i + 1) {
            scanf("%d", &vector2[i]);
        }
    
        dotProduct = 0;
        for (i = 0; i < VECTOR_LENGTH; i = i + 1) {
            dotProduct = dotProduct + vector1[i] * vector2[i];
        }
    
        printf("Their dot-product is %d.\n", dotProduct);
        return 0;
    }
    
  11. Write a C program which reads numbers until end-of-input is reached and then prints the middle number.

    So if 9 numbers were entered the program should print the 5th number entered. And if 27 numbers were entered the program should print the 14th number entered.

    If an even number of numbers is entered there are two middle numbers, print the second of them.

    For example if 8 numbers are entered, prints the 5th.

    You can assume at most 10,000 numbers will be entered.

    #include <stdio.h>
    
    #define MAX_NUMBERS 100000
    
    int main(void) {
        int numbers[MAX_NUMBERS], howMany, returnValue, middleIndex;
        howMany = 0;
        returnValue = 1;
        while (returnValue == 1) {
            returnValue = scanf("%d", &numbers[howMany]);
            howMany = howMany + 1;
        }
        howMany = howMany - 1;
        middleIndex = howMany / 2;
        printf("%d\n", numbers[middleIndex]);
        return 0;
    }
    

Extra Exercises for practice