In this lab, you will practice:
Create a new directory for this lab called lab05
by typing:
mkdir lab05Change to this directory by typing:
cd lab05
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 }
sumPointers.c
#include <stdio.h> void sumByPointers(int *sum, int *a, int *b); int main(void) { int num1; int num2; int sum = 0; printf("Please enter two numbers to add: "); scanf("%d %d", &num1, &num2); sumByPointers(&sum, &num1, &num2); printf("Sum is: %d\n", sum); return 0; } void sumByPointers(int *sum, int *a, int *b) { *sum = *sum + *a + *b; }
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.300000As 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.
sort3.c
#include <stdio.h> //Write the function, then modify it to read the numbers from a file - you may assume //that there will be exactly 3 nums in the file and you print them to the screen void sort3(double *x, double *y, double *z); void swap(double *x, double *y); int main(int argc, char *argv[]) { double x, y, z; if( scanf("%lf %lf %lf", &x, &y, &z) == 3 ){ sort3(&x, &y, &z); printf("%f %lf %lf\n", x, y, z); } else { fprintf(stderr,"Error: Incorrect format\n"); } return 0; } void sort3(double *x, double *y,double *z){ // three comparisons only if (*x > *y) { swap(x, y); } // would 'else if' work here? if(*y > *z) { swap(y, z); } if(*x > *y) { swap(x, y); } } void swap(double *x, double *y){ double tmp; tmp = *x; *x = *y; *y = tmp; }
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; }
pointerSum.c.c
#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; } void reverseArray(double numbers[], int size) { int i = 0; while (i < size / 2) { swap(&numbers[i], &numbers[size - 1 - i]); i++; } } void printArray(double numbers[], int size) { int i = 0; while (i < size) { printf("%lf ", numbers[i]); i++; } printf("\n"); } int readIntoArray(double numbers[]) { double in; int i = 0; while (i < MAX_SIZE && scanf("%lf", &in) != -1) { numbers[i] = in; i++; } return i; } void swap(double *a, double *b) { double temp = *a; *a = *b; *b = temp; }
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
oddEvenTen.c
//Read MAX_NUMBERS integers and print odd and even integers on separate lines // Andrew Taylor - andrewt@unsw.edu.au // 27/3/2018 // // Note for simplicity we are assuming scanf succeeds in reading an integer. // A robust program would check that scanf returns 1 to indicate an integer was read. // e.g. assert(scanf("%d", &numbers[i]) == 1) #include <stdio.h> #define MAX_NUMBERS 10 int main(void) { int numbers[MAX_NUMBERS] = {0}; int i = 0; while (i < MAX_NUMBERS) { scanf("%d", &numbers[i]); i = i + 1; } printf("Odd numbers were:"); int j = 0; while (j < MAX_NUMBERS) { if (numbers[j] % 2 == 1) { printf(" %d", numbers[j]); } j = j + 1; } printf("\n"); printf("Even numbers were:"); int k = 0; while (k < MAX_NUMBERS) { if (numbers[k] % 2 == 0) { printf(" %d", numbers[k]); } k = k + 1; } printf("\n"); return 0; }
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.8As usual autotest is available to test your program.
1911 autotest lab05 vectorStatistics
vectorStatistics.c
#include <stdio.h> #define SIZE 10 int main(void) { int numbers[SIZE]; int i, min, max; double sum; printf("Enter vector of %d numbers: ", SIZE); i = 0; while ( i < SIZE ) { scanf("%d", &numbers[i]); i = i + 1; } //The first element in the array is the min and the max so far min = numbers[0]; max = numbers[0]; sum = numbers[0]; i = 1; while ( i < SIZE) { if (numbers[i] < min) { min = numbers[i]; } if (numbers[i] > max) { max = numbers[i]; } sum = sum + numbers[i]; i = i + 1; } printf("Minimum vector value is %d\n", min); printf("Maximum vector value is %d\n", max); printf("Average vector value is %.1f\n", sum/SIZE); return 0; }
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
vectorMismatch.c
.
#include <stdio.h> #define VECTOR_LENGTH 10 int main(void) { int vector1[VECTOR_LENGTH]; int vector2[VECTOR_LENGTH]; int i, j; int nMatches, mMismatches; printf("Enter vector 1 of %d positive numbers: ", VECTOR_LENGTH); i = 0; while (i < VECTOR_LENGTH) { scanf("%d", &vector1[i]); i = i + 1; } printf("Enter vector 2 of %d positive numbers: ", VECTOR_LENGTH); i = 0; while (i < VECTOR_LENGTH) { scanf("%d", &vector2[i]); i = i + 1; } nMatches = 0; i = 0; while (i < VECTOR_LENGTH ) { if (vector1[i] == vector2[i]) { nMatches = nMatches + 1; vector1[i] = 0; vector2[i] = 0; } i = i + 1; } printf("Vectors match in %d positions.\n", nMatches); mMismatches = 0; i = 0; while ( i < VECTOR_LENGTH ) { j = 0; while ( vector1[i] != 0 && j < VECTOR_LENGTH ) { if (vector1[i] == vector2[j]) { mMismatches = mMismatches + 1; vector1[i] = 0; vector2[j] = 0; } j = j + 1; } i = i + 1; } printf("Vectors mismatch in %d positions.\n", mMismatches); return 0; }
lab05
directory):
give cs1911 lab05 sumPointers.c sort3.c reverseArray.c vectorMismatch.c vectorStatistics.cSubmit 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