Lab 10 – 2017

Slides: [ EST ][ ENG ]

Tasks

Coding style (1p)

  • Fix badly formatted code [ Link ] according to [ Style guide ]
    • Pick code according to your last 2 digits of student code

NB! No need to understand the code! This task is for code styling purposes only!

 

Compound interest calculator [ Link ] (3p)

  • Calculate compound interest for n years (1p)
    • Ask user how many years should be calculated (n)
    • Ask user the initial amount of money (P)
    • Ask the user interest rate (r)
    • Display results from year 1 to n
    • Create a function which will return the amount of money at one point in time
  •  Different compounding intervals (0.5p)
    • daily
    • weekly
    • monthly
    • quarterly
    • yearly
  • Additional deposits per interval (0.5p)
  • Additional withdrawals per interval (0.5p)
  • Withdrawals / deposits calculate out of sync with intervals (0.5p)
    • e.g. quarterly interest calculation, monthly deposits 
Compound interest calculation formula

 

Functions Task (1p)

     Split the following code into functions:

  • Print Function
    • Prints vector, doesn’t return anything, is used in the code more than once.
  • Random Generator Function
    • Generates random number within range, returns generated number.
  • Sorting Function
    • Sorts the vector in descending order, doesn’t return anything.
  • Multiplier Function
    • Multiplies array elements with a number, doesn’t return anything. (original values must remain)

N.B. Print the vector after each step (as does the initial code)

 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define VECTOR_SIZE 20

int main(void) {

    int min = 10;
    int max = 100;
    int vector[VECTOR_SIZE] = {0};
    int number = 0;
    int tmp = 0;

    // Set seed (UNIX timestamp of current time)
    srand(time(NULL));

    // Fill array with random numbers
    for (int num = 0; num < VECTOR_SIZE; num++) {
        number = ((rand() % (max - min)) + min);
        vector[num] = number;
    }

    // Display vector
    for (int i = 0; i < VECTOR_SIZE; i++) {
        printf("%4d", vector[i]);
    }
    printf("\n");

    // Sort the array in Desc. order
    for (int i = 0; i < VECTOR_SIZE - 1; i++) {
        for (int j = 0; j < VECTOR_SIZE - i - 1; j++) {
            if (vector[j] < vector[j + 1]) {
                tmp = vector[j + 1];
                vector[j + 1] = vector[j];
                vector[j] = tmp;
            }
        }
    }

    // Display vector
    for (int i = 0; i < VECTOR_SIZE; i++) {
        printf("%4d", vector[i]);
    }
    printf("\n");

    // Ask user for a magical number
    printf("Enter a number: ");
    scanf("%d", &number);
    
    // Multiply array elements with some number
    for (int i = 0; i < VECTOR_SIZE; i++) {
        printf("%5d", vector[i] * number);
    }
    printf("\n");

    // Display vector (must have the values untouched after sorting)
    for (int i = 0; i < VECTOR_SIZE; i++) {
        printf("%4d", vector[i]);
    }
    printf("\n");

    return 0;
}