COMP1511 17s1 Introduction to Programming

Objectives

In these revision lab exercises you will revise topics relevant to the week 8/9 exam.

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples. You should also have read the lab assessment guidelines.

Getting Started

As usual create a new directory for this lab called lab08 by typing:
mkdir lab08
Change to this directory by typing:
cd lab08

Introduction

These revision exercises are designed to prepare you for the mid-session exam.

These revision exercises are not assessable.

They do not get marked.

There is no need to submit them with give.

Revision lab times will be announced which you can attend to get help with these revision exercises.

Revision Exercise: Read Ten

Write a C program read_ten.c which reads 10 integers from standard input, stores them in an array, and prints them on one line.

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

Match the the example below EXACTLY.

./read_ten
1
2
4
16
32
64
128
256
512
1024
Numbers were: 1 2 4 16 32 64 128 256 512 1024
As usual autotest is available to help you test your program.
 ~cs1511/bin/autotest lab08 read_ten.c
Sample solution for read_ten.c
// Read MAX_NUMBERS integers and print them on a single line
// Andrew Taylor - andrewt@unsw.edu.au
// 27/3/2017
//
// 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.


#include <stdio.h>

#define MAX_NUMBERS 10

int main(void) {
    int numbers[MAX_NUMBERS], i;

    i = 0;
    while (i < MAX_NUMBERS) {
        scanf("%d", &numbers[i]);
        i = i + 1;
    }
    printf("Numbers were:");

    i = 0;
    while (i < MAX_NUMBERS) {
        printf(" %d", numbers[i]);
        i = i + 1;
    }
    printf("\n");
    return 0;
}

Revision Exercise: Reverse Ten

Write a C program reverse_ten.c which reads 10 integers from standard input, and prints them on them one line in reverse order.

Hint: use read_ten.c as a starting point.

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

Match the the example below EXACTLY.

./reverse_ten
1
2
4
16
32
64
128
256
512
1024
Numbers were: 1024 512 256 128 64 32 16 4 2 1
As usual autotest is available to help you test your program.
 ~cs1511/bin/autotest lab08 reverse_ten.c
Sample solution for reverse_ten.c
// Read 10 integers and print them in reverse order on a single line
// Andrew Taylor - andrewt@unsw.edu.au
// 27/3/2017
//
// 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.


#include <stdio.h>

#define MAX_NUMBERS 10

int main(void) {
    int numbers[MAX_NUMBERS], i;

    i = 0;
    while (i < MAX_NUMBERS) {
        scanf("%d", &numbers[i]);
        i = i + 1;
    }
    printf("Numbers were:");

    i = MAX_NUMBERS - 1;
    while (i >= 0) {
        printf(" %d", numbers[i]);
        i = i - 1;
    }
    printf("\n");
    return 0;
}

Revision Exercise: Odd-Even Ten

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

Hint: use read_ten.c as a starting point.

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.

./odd_even_ten
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
./odd_even_ten
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 help you test your program.
 ~cs1511/bin/autotest lab08 odd_even_ten.c
Sample solution for odd_even_ten.c
// Read MAX_NUMBERS integers and print odd and even integers on separate lines
// Andrew Taylor - andrewt@unsw.edu.au
// 27/3/2017
//
// 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.


#include <stdio.h>

#define MAX_NUMBERS 10

int main(void) {
    int numbers[MAX_NUMBERS], i;

    i = 0;
    while (i < MAX_NUMBERS) {
        scanf("%d", &numbers[i]);
        i = i + 1;
    }

    printf("Odd numbers were:");
    i = 0;
    while (i < MAX_NUMBERS) {
        if (numbers[i] % 2 == 1) {
            printf(" %d", numbers[i]);
        }
        i = i + 1;
    }
    printf("\n");

    printf("Even numbers were:");
    i = 0;
    while (i < MAX_NUMBERS) {
        if (numbers[i] % 2 == 0) {
            printf(" %d", numbers[i]);
        }
        i = i + 1;
    }
    printf("\n");

    return 0;
}

A sample solution for odd_even_ten.c using for loops and a function
// Read MAX_NUMBERS integersers and print odd and even integers on separate lines
// Andrew Taylor - andrewt@unsw.edu.aus
// 27/3/2017
//
// 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.


#include <stdio.h>

#define MAX_NUMBERS 1000

void print_odd_even(int array_length, int array[], int odd);

int main(void) {
    int numbers[MAX_NUMBERS];

    for (int i = 0; i < MAX_NUMBERS; i++) {
        scanf("%d", &numbers[i]);
    }

    printf("Odd numbers were:");
    print_odd_even(MAX_NUMBERS, numbers, 1);

    printf("Even numbers were:");
    print_odd_even(MAX_NUMBERS, numbers, 0);

    return 0;
}

// print even numbers in array if odd == 0, print odd numbers if odd == 1

void print_odd_even(int array_length, int array[], int odd) {
    for (int i = 0; i < array_length; i++) {
        if (array[i] % 2 == odd) {
            printf(" %d", array[i]);
        }
    }
    printf("\n");
}

Revision Exercise: Odd-Even Negative

Write a C program odd_even_negative.c which reads integers from standard input until it reads a negative integer. It should then print the odd numbers on one line then print the even numbers on one line.

Hint: use odd_even_ten.c as a starting point.

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

You can assume a negative integer will always be read.

You can assume at most 1000 integers are read before a negative integer is read.

Match the the example below EXACTLY.

./odd_even_negative
1
2
3
2
-42
Odd numbers were: 1 3
Even numbers were: 2 2
./odd_even_negative
1
2
4
16
32
64
128
256
512
1024
2048
4096
-8192
Odd numbers were: 1
Even numbers were: 2 4 16 32 64 128 256 512 1024 2048 4096
As usual autotest is available to help you test your program.
 ~cs1511/bin/autotest lab08 odd_even_negative.c
Sample solution for odd_even_negative.c
// Read integers until a negative inetger is read and then print odd and even integers on separate lines
// Andrew Taylor - andrewt@unsw.edu.au
// 27/3/2017
//
// 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.


#include <stdio.h>

#define MAX_NUMBERS 1001

void print_odd_even(int array_length, int array[], int odd);

int main(void) {
    int numbers[MAX_NUMBERS];
    int n;

    n = 0;
    scanf("%d", &numbers[0]);
    while (numbers[n] >= 0 && n < MAX_NUMBERS - 1) {
        n = n + 1;
        scanf("%d", &numbers[n]);
    }

    printf("Odd numbers were:");
    print_odd_even(n, numbers, 1);

    printf("Even numbers were:");
    print_odd_even(n, numbers, 0);

    return 0;
}

// print even numbers in array if odd == 0, print odd numbers if odd == 1

void print_odd_even(int array_length, int array[], int odd) {
    for (int i = 0; i < array_length; i++) {
        if (array[i] % 2 == odd) {
            printf(" %d", array[i]);
        }
    }
    printf("\n");
}

Revision Exercise: Odd-Even EOF

Write a C program odd_even_eof.c which reads integers from standard input until it reaches end-of-input.

It should then print the odd numbers on one line then print the even numbers on one line.

Hint: use odd_even_negative.c as a starting point.

You may assume that the program's input will contain only integers.

You can assume at most 1000 integers will be read before end-of-input is reached.

Match the the example below EXACTLY.

./odd_even_eof
1
2
4
16
32
64
128
256
512
1024
2048
4096

Odd numbers were: 1
Even numbers were: 2 4 16 32 64 128 256 512 1024 2048 4096
./odd_even_eof
3
-3
4
-4
4

Odd numbers were: 3 -3
Even numbers were: 4 -4 4
As usual autotest is available to help you test your program.
 ~cs1511/bin/autotest lab08 odd_even_eof.c
Sample solution for odd_even_eof.c
// Read integers until EOF (or non-number) and then print odd and even integers on separate lines
// Andrew Taylor - andrewt@unsw.edu.au
// 27/3/2017

#include <stdio.h>

#define MAX_NUMBERS 1001

void print_odd_even(int array_length, int array[], int odd);

int main(void) {
    int numbers[MAX_NUMBERS];
    int n;

    n = 0;
    while (n < MAX_NUMBERS && scanf("%d", &numbers[n]) == 1) {
        n = n + 1;
    }

    printf("Odd numbers were:");
    print_odd_even(n, numbers, 1);

    printf("Even numbers were:");
    print_odd_even(n, numbers, 0);

    return 0;
}

// print even numbers in array if odd == 0, print odd numbers if odd == 1

void print_odd_even(int array_length, int array[], int odd) {
    for (int i = 0; i < array_length; i++) {
        if (array[i] % 2 == odd || array[i] % 2 == -odd) {
            printf(" %d", array[i]);
        }
    }
    printf("\n");
}

Revision Exercise: Odd-Even Argument

Write a C program odd_even_argument.c which is given integers as command-line arguments, and prints the odd numbers on one line then prints the even numbers on one line.

Hint: use odd_even_eof.c as a starting point.

You may assume that the program's command line arguments will only be integers.

You can assume at most 1000 command line arguments will be supplied.

Match the the example below EXACTLY.

./odd_even_argument 1 2 4 16 32 64 128 256 512 1024 2048 4096 
Odd numbers were: 1
Even numbers were: 2 4 16 32 64 128 256 512 1024 2048 4096
./odd_even_argument 3 -3 4 -4 4 
Odd numbers were: 3 -3
Even numbers were: 4 -4 4
As usual autotest is available to help you test your program.
 ~cs1511/bin/autotest lab08 odd_even_argument.c
Sample solution for odd_even_argument.c
// Read integers until EOF (or non-number) and then print odd and even integers on separate lines
// Andrew Taylor - andrewt@unsw.edu.au
// 27/3/2017

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

void print_odd_even(int array_length, int array[], int odd);

int main(int argc, char *argv[]) {
    int numbers[argc - 1];

    for (int i = 1; i < argc; i++) {
        numbers[i - 1] = atoi(argv[i]);
    }

    printf("Odd numbers were:");
    print_odd_even(argc - 1, numbers, 1);

    printf("Even numbers were:");
    print_odd_even(argc - 1, numbers, 0);

    return 0;
}

// print even numbers in array if odd == 0, print odd numbers if odd == 1

void print_odd_even(int array_length, int array[], int odd) {
    for (int i = 0; i < array_length; i++) {
        if (array[i] % 2 == odd || array[i] % 2 == -odd) {
            printf(" %d", array[i]);
        }
    }
    printf("\n");
}

Revision Exercise: Indivisible

Write a C program indivisible.c which reads integers > 1 from standard input until it reaches end-of-input.

It should then print the integers read which are not exactly divisible by any other of the integers read.

In other words it should not print an integer if another integer that has been read is a factor of that number.

Hint: use odd_even_eof.c as a starting point.

You may assume that the program's input will contain only integers.

You may assume that all integers are > 1.

You can assume at most 1000 integers will be read before end-of-input is reached.

Match the the example below EXACTLY.

./indivisible
42
7
6
12

Indivisible numbers: 7 6
./indivisible
2
3
4
5
6
7
8
9
10

Indivisible numbers: 2 3 5 7
./indivisible
5
6
5

Indivisible numbers: 6
As usual autotest is available to help you test your program.
 ~cs1511/bin/autotest lab08 indivisible.c
Sample solution for indivisible.c
// Read integers > 1 until EOF (or non-number) and then porint integers not exactly divisible by any other integer read
// 27/3/2017

#include <stdio.h>

#define MAX_NUMBERS 1001

void  print_divisble(int array_length, int array[]);

int main(void) {
    int numbers[MAX_NUMBERS];
    int n;

    n = 0;
    while (n < MAX_NUMBERS && scanf("%d", &numbers[n]) == 1) {
        n = n + 1;
    }

    printf("Indivisible numbers:");
    print_divisble(n, numbers);
    return 0;
}

// print integers in array which are not exactly divisible by any other integers in array

void print_divisble(int array_length, int array[]) {

    for (int i = 0; i < array_length; i++) {
        int factors = 0;
        for (int j = 0; j < array_length; j++) {
            if (array[i] % array[j] == 0) {
                factors++;
            }
        }
        if (factors == 1) {
            printf(" %d", array[i]);
        }
    }
    printf("\n");
}

Submission/Assessment

These revision exercises are not assessable.

They do not get marked.

There is no need to submit them with give.