Arrays Lab task 1 loops Use your week

Arrays

Lab task #1: loops • Use your week 1 code (odd or even? ) as the baseline • You can use our code sample if you don’t have one • User must be able to enter multiple numbers without restarting the program • User must be able to exit the program when they desire to do so • Tips: • Place the functional part of your code in a loop • Place a prompt in that loop so user can decide when to stop • The loop must only be stopped once the user says so – do not count iterations! • Use either the loop condition or break statement to stop the loop 2018 Risto Heinsar 2

From code to a runnable program 1. Preprocessor • Deals with trigraphs, macros, line splicing 2. Compiler • Creates assembly code 3. Assembler • Creates object code that is machine runnable 4. Linker • Creates links to other library functions – e. g. where is printf? 2018 Risto Heinsar 3

#define macros (preprocessor directives) • #define macros are processed before compilation (preprocessor) • Uses “find and replace” principle • Positioned right after #include statements #define <name> <expression> SIZE 7 TEXTFIELD_LEN 30 OPTIONS "settings. ini" • Purpose for now: removing magical numbers, using constants • Unlike variables, these cannot be changed during runtime! 2018 Risto Heinsar 4

The array data structure • Array is a collection of elements, that have the same data type • • Integers (int) Floating point numbers (float, double) Characters (char) // character array is also known as a string etc. • Array elements are indexed using integers, starting from zero • Accessing a member in the array is done by using its index • 1 -dimensional arrays are also known as vectors • 2 -dimensional arrays are also known as matrixes 2018 Risto Heinsar 5

Declaring an array • The size of an array is set at the time of declaration by the number in the square brackets int numbers[10]; // an array for 10 integers char text. Field[15]; // array for 14 + 1 chars • The arrays can be N-dimensional and the lengths of the dimensions may vary int elements[N][M][K]; 2018 Risto Heinsar 6

1 -dimentional array (vector) • When declaring, the number in the square brackets defines array size int numbers[5]; numbers[0] numbers[1] numbers[2] numbers[3] numbers[4] • When accessing a member of the array, we specify the index of the element in the square brackets numbers[index] • To print out the third element of an array we would use: printf("%d", numbers[2]); 2018 Risto Heinsar 7

Sample 1: Arrays #include <stdio. h> #define NUM_COUNT 3 int main(void) { int score. Table[NUM_COUNT]; score. Table[0] = 93; score. Table[1] = 85; score. Table[2] = 51; printf("The students achieved the following results: %d, %d and %dn", score. Table[0], score. Table[1], score. Table[2]); return 0; } 2018 Risto Heinsar 8

Sample 2: arrays and loops #include <stdio. h> #define NUM_COUNT 5 int main(void) { int num. Arr[NUM_COUNT] = {19, 2, -5, 133, 0}; int i; printf("The numbers stored in the array are: n"); for (i = 0; i < NUM_COUNT; i++) { printf("%dn", num. Arr[i]); } return 0; } 2018 Risto Heinsar 9

Homework for next week • Model 2 algorithms in UML: one using loops and one without • Client wishes to withdraw cash from an ATM. Read the desired amount, validate it and output using the minimum amount of bank notes • 945€ -> 1 x 500€, 2 x 20€, 1 x 5€ • Write a program in C • Client wishes to withdraw cash from an ATM. Read the desired amount, validate it and output using the maximum number of unique bank notes • 945€ -> 1 x 500€, 1 x 200€, 1 x 100€, 1 x 50€, 1 x 20€, 1 x 10€, 13 x 5€ • Validate! Both 103€ and -20€ should trigger an error. • There’s a pseudocode to help you! 2018 Risto Heinsar 10

Lab task #2: finding min, max – UML • The following task must be modelled in UML! • User will enter 5 numbers from the keyboard • The program will find the minimum and maximum value from those numbers • The program will print out the original array • The program will print out the minimum and maximum values from that array 2018 Risto Heinsar 11

Lab task #2: finding min, max – code • Create a program based on the algorithm composed previously (slight alterations!) • User will input 5 numbers. Those numbers are stored in an array • After input, the following result will be printed on the screen: • The list of the numbers that were entered • The smallest number and the index of that number • The biggest number and the index of that number • If either the smallest or the biggest number is entered multiple times, display the last occurrence 2018 Risto Heinsar 12

Advanced tasks • Task 1: • Use a suitable formatting • Find the minimum and maximum value in the same loop • Find the arithmetic mean, sum and product of the given numbers • Task 2: • If the min/max value is present multiple times, display all of the positions • Task 3: • Allow the user to specify how many numbers will be entered • The absolute maximum is set to 50 numbers • Limit the user’s input so that the user couldn’t overflow the array 2018 Risto Heinsar 13
- Slides: 13