Download presentation
Presentation is loading. Please wait.
1
Arrays
2
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
3
From code to a runnable program
Preprocessor Deals with trigraphs, macros, line splicing Compiler Creates assembly code Assembler Creates object code that is machine runnable Linker Creates links to other library functions – e.g. where is printf? 2018 Risto Heinsar
4
#define macros (preprocessor directives)
#define macros are processed before compilation (preprocessor) Uses “find and replace” principle Positioned right after #include statements #define <name> <expression> #define SIZE 7 #define TEXTFIELD_LEN 30 #define OPTIONS "settings.ini" Purpose for now: removing magical numbers, using constants Unlike variables, these cannot be changed during runtime! 2018 Risto Heinsar
5
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
6
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 textField[15]; // array for chars The arrays can be N-dimensional and the lengths of the dimensions may vary int elements[N][M][K]; 2018 Risto Heinsar
7
1-dimentional array (vector)
When declaring, the number in the square brackets defines array size int numbers[5]; 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]); numbers[0] numbers[1] numbers[2] numbers[3] numbers[4] 2018 Risto Heinsar
8
Sample 1: Arrays #include <stdio.h> #define NUM_COUNT 3
#define NUM_COUNT 3 int main(void) { int scoreTable[NUM_COUNT]; scoreTable[0] = 93; scoreTable[1] = 85; scoreTable[2] = 51; printf("The students achieved the following results: %d, %d and %d\n", scoreTable[0], scoreTable[1], scoreTable[2]); return 0; } 2018 Risto Heinsar
9
Sample 2: arrays and loops
#include <stdio.h> #define NUM_COUNT 5 int main(void) { int numArr[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("%d\n", numArr[i]); } return 0; 2018 Risto Heinsar
10
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€ -> 1x500€, 2x200€, 2x20€, 1x5€ 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€ -> 1x500€, 1x200€, 1x100€, 1x50€, 1x20€, 1x10€, 13x5€ Validate! Both 103€ and -20€ should trigger an error. There’s a pseudocode to help you! 2018 Risto Heinsar
11
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
12
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
13
Advanced tasks Task 1: Task 2: Task 3: 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.