COMP1911 23T2 Introduction to Programming

Objectives

In this lab, you will practice:

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples

Getting Started

Login and run following commands inside a Unix terminal

Create a new directory for this lab called lab05 by typing:

mkdir lab05
Change to this directory by typing:
cd lab05

Exercise 1: Using pass by reference / pointers to add

You need to implement a function in file sumPointers.c to sum two numbers via pointers. This function does not return anything, because all arguments are passed by reference.

Complete the TODO's in this code.

#include <stdio.h>

void sumByPointers(int *sum, int *a, int *b);

int main(void) {
    int num1;
    int num2;
    int sum;
    printf("Please enter two numbers to add: ");
    scanf("%d %d", &num1, &num2);

    sumByPointers(/* TODO */);
    printf("Sum is: %d\n", sum);

    return 0;
}

void sumByPointers(int *sum, int *a, int *b) {
    // TODO
}

Exercise 2: Sort 3

Write a function, sort3, that takes three float pointers as arguments and sorts the values they point to in increasing order.

If you would like a challenge, try to accomplish this by using a single comparison operator, >, no more than three times! Use some ingenuity to figure out how to do this.

Test your function by adding it to the following code. Include all the code in a program called sort3.c.

#include <stdio.h>

void sort3(double *x, double *y, double *z);

int main(int argc, char *argv[]) {
  double x, y, z;
  
  scanf("%lf %lf %lf", &x, &y, &z);
  sort3(&x, &y, &z);
  printf("%lf %lf %lf\n", x, y, z);
  
  return 0;
}
Your program should behave like this:
./sort3
1.3 1.2 1.1
1.100000 1.200000 1.300000
As usual autotest is available to test your program.
1911 autotest lab05 sort3.c

Beware: autotest will not just use your main function. It will also call your function with its own main.

Exercise 3: Reverse Array

The following program reverseArray.c should scanf in a number of doubles, and then reverse the array.

Beware: Please do not declare ANY new arrays in any of your functions, or change the prototypes. The point of this exercise is how to reverse an array using pointers.

Note: This program will only work for numbers as input. Entering non-numbers produces strange behaviour.

#include <stdio.h>

#define MAX_SIZE 99

void reverseArray(double numbers[], int size);
void printArray(double numbers[], int size);
int readIntoArray(double numbers[]);
void swap(double *a, double *b);

int main(void) {
    double numbers[MAX_SIZE];
    int size = readIntoArray(numbers);
    reverseArray(numbers, size);
    printArray(numbers, size);
    return 0;
}

int readIntoArray(double numbers[]) {
  double in;
  int i = 0;
  while (i < MAX_SIZE && scanf("%lf", &in) == 1) {
    numbers[i] = in;
    i++;
  }
  return i;
}

(Optional) Challenge Exercise 1: Odd Even Ten

Write a C program oddEvenTen.c which reads 10 integers from standard input, prints the odd numbers on one line, then prints the even numbers on one line.

You may assume that the program's input will contain only positive integers and will contain at least 10 integers, in other words you can assume scanf succeeeds.

Match the the example below EXACTLY.

dcc oddEvenTen.c -o oddEvenTen
./oddEvenTen
3
4
5
6
7
9
8
7
6
5

Odd numbers were: 3 5 7 9 7 5
Even numbers were: 4 6 8 6
./oddEvenTen
1
2
4
16
32
64
128
256
512
1024
Odd numbers were: 1
Even numbers were: 2 4 16 32 64 128 256 512 1024
./oddEvenTen
3
5
7
11
13
11
9
7
5
3
Odd numbers were: 3 5 7 11 13 11 9 7 5 3
Even numbers were:

As usual autotest is available to test your program.

 1911 autotest lab05 oddEvenTen.c

(Optional) Challenge Exercise 2: Vector Statistics

Write a program called vectorStatistics.c that reads a vector of 10 integers then prints the minimum, maximum & mean of the integers in exactly the format shown below.

Your program must match the examples below perfectly, so for example you must write out the average to 1 decimal place.

It must be possible by change a single #define to change length of the vector (the number of integers your program reads). Hint: use an array to store the numbers. Strictly you don't need to use an array to answer this question, but you MUST use an array for this exercise as practise and so you can reuse some of your code for one of the lab exercises next week! Hint: you've seen code to read numbers into an array, e.g. arrayEx1.c.

./vectorStatistics
Enter vector of 10 numbers: 1 2 3 4 5 5 4 3 2 1
Minimum vector value is 1
Maximum vector value is 5
Average vector value is 3.0
./vectorStatistics
Enter vector of 10 numbers: 3 4 5 6 7 8 -9 -11 43 42
Minimum vector value is -11
Maximum vector value is 43
Average vector value is 9.8
As usual autotest is available to test your program.
 1911 autotest lab05 vectorStatistics

(Optional) Challenge Exercise 3: Vector Mismatch

Write a program called vectorMismatch.c that that reads 2 vectors of 10 positive integers the prints a count of how many times the 2 vectors have the same value in the same position (just as in the previous exercise) and how many times the vectors have the same value but in a different position.

Note you have to avoid double-counting - carefully examine the examples below to understand whats wanted.

Your program must match the examples below perfectly.

Your program can assume it is given correct input (20 positive integers).

Your program must break down the problem into suitable functions

./vectorMismatch
Enter vector 1 of 10 positive numbers: 5 5 5 1 5 5 2 5 5 5
Enter vector 2 of 10 positive numbers: 6 6 6 1 6 2 6 6 6 6
Vectors match in 1 positions.
Vectors mismatch in 1 positions.
./vectorMismatch
Enter vector 1 of 10 positive numbers: 5 5 5 1 5 5 2 5 5 5
Enter vector 2 of 10 positive numbers: 1 1 1 1 2 2 6 2 5 5
Vectors match in 3 positions.
Vectors mismatch in 1 positions.
./vectorMismatch
Enter vector 1 of 10 positive numbers: 42 1 2 3 4 5 6 7 8 9
Enter vector 2 of 10 positive numbers: 9 8 7 6 5 4 3 2 1 42
Vectors match in 0 positions.
Vectors mismatch in 10 positions.
./vectorMismatch
Enter vector 1 of 10 positive numbers: 3 3 3 3 4 4 4 5 5 5
Enter vector 2 of 10 positive numbers: 5 5 5 5 5 3 3 3 4 4
Vectors match in 0 positions.
Vectors mismatch in 8 positions.
As usual autotest is available to test your program.
1911 autotest lab05 vectorMismatch

Submission/Assessment

When you are satisfied with your work, ask your tutor to assess it. You are to submit it electronically by typing (run this command in your lab05 directory):
give cs1911 lab05 sumPointers.c sort3.c reverseArray.c vectorMismatch.c vectorStatistics.c
Submit advanced exercises only if you attempt the advanced exercise.

Remember the lab assessment guidelines - if you don't finish the exercises you can finish them in your own time, submit them by 19:59:59 Sunday using give and ask ask tutor to assess them at the start of the following lab.

You can also just run the autotests without submitting by typing

1911 autotest lab05