Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)

Similar presentations


Presentation on theme: "Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)"— Presentation transcript:

1 Arrays

2 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 of array is done by using its’ index 1-dimensional arrays are also known as vectors 2-dimensional arrays are also known as matrixes 20152

3 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 14 + 1 chars The arrays can be N-dimensional and the lengths of the dimentions may vary int elements[N][M][K] 20153

4 1-dimentional array (vector) The number in the square brackets sets the size for the array 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] 20154

5 Lab task #1 Create an algorithm and based on that a program User will input 5 numbers, that will be stored in an array After input, the following result will be printed on the screen: The list of the numbers that the user entered The smallest number entered The biggest number entered 20155

6 Improvements for the advanced Make the following changes to the code you wrote: Find the average, sum and product of the numbers entered Format Your output for better readability – try things like "%3d", "%3.2f" etc Allow the user to specify the number of elements that will be inputted. 20156

7 #define macros (preprocessor directive) #define macros are fulfilled before the code is compiled Works like “find and replace” The #define macros are placed right after the #include directives #define #define SIZE 7 #define TEXTFIELD_LEN 30 #define OPTIONS "settings.ini" Priority for now: avoid magic numbers, constants in code 20157

8 2-dimensional array (matrix) In this case we have a square matrix (both dimensions are equal) int array[5][5] array[0][0]array[0][1]array[0][2]array[0][3]array[0][4] array[1][0]array[1][1]array[1][2]array[1][3]array[1][4] array[2][0]array[2][1]array[2][2]array[2][3]array[2][4] array[3][0]array[3][1]array[3][2]array[3][3]array[3][4] array[4][0]array[4][1]array[4][2]array[4][3]array[4][4] 20158

9 Going through all of the elements #include int main(void) { int i, j, numArray[5][5]; for (i = 0; i < 5; i++) { for (j = 0; j < 5; j++) { numArray[i][j] = 0; } return 0; } 20159

10 Lab task #2 Create a 2-dimensional array for 10x10 multiplication table Avoid using magic numbers when declaring the array and limiting the loops The multiplication table will be stored in the array created →Use for() loops for this! The multiplication table is displayed from the array you generated →Use while() loops for this! Format your output for better visuals →Put line breaks at the right time →Use formatted output when printing numbers - %4d 201510

11 Home task Modify your multiplication table, so that: The user can enter the size (width x length) Limit the size that can be entered based on what your program window can actually fit When the user inputs a size that’s out of bounds, ask again – use a do while() loop for this. You must be able to repeat the generation process without restarting the program. Create an algorithm with the state of lab task #2 (don’t include improvements from home task) 201511


Download ppt "Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)"

Similar presentations


Ads by Google