lab08
by typing:
mkdir lab08Change to this directory by typing:
cd lab08
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.
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 1024As usual autotest is available to help you test your program.
~cs1511/bin/autotest lab08 read_ten.c
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; }
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 1As usual autotest is available to help you test your program.
~cs1511/bin/autotest lab08 reverse_ten.c
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; }
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
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"); }
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 4096As usual autotest is available to help you test your program.
~cs1511/bin/autotest lab08 odd_even_negative.c
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"); }
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 4As usual autotest is available to help you test your program.
~cs1511/bin/autotest lab08 odd_even_eof.c
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"); }
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 4As usual autotest is available to help you test your program.
~cs1511/bin/autotest lab08 odd_even_argument.c
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"); }
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: 6As usual autotest is available to help you test your program.
~cs1511/bin/autotest lab08 indivisible.c
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"); }
They do not get marked.
There is no need to submit them with give.