Exercise 5
Transcription
Exercise 5
Exercise 5 Arrays and C-Strings Informatik I für D-MAVT (FS 2016) © M. Gross, ETH Zürich, 2016 Recap last exercise • Question 1: • int i; long int li; float f; double d; long double ld; char c; bool b; • int x = f; • short int x = li; • long int x = f; • float x = li; • double x = c; • int x = b; • short int x = ld; • char x = i; • bool x = i; 2 © M. Gross, ETH Zürich, 2016 Recap last exercise • Question 1: • int i; long int li; float f; double d; long double ld; char c; bool b; • int x = f; • short int x = li; • long int x = f; • float x = li; • double x = c; • int x = b; • short int x = ld; • char x = i; • bool x = i; 2 -> abschneiden © M. Gross, ETH Zürich, 2016 Recap last exercise • Question 1: • int i; long int li; float f; double d; long double ld; char c; bool b; • int x = f; -> abschneiden • short int x = li; -> bits werden abgeschnitten! • long int x = f; • float x = li; • double x = c; • int x = b; • short int x = ld; • char x = i; • bool x = i; 2 © M. Gross, ETH Zürich, 2016 Recap last exercise • Question 1: • int i; long int li; float f; double d; long double ld; char c; bool b; • int x = f; -> abschneiden • short int x = li; -> bits werden abgeschnitten! • long int x = f; -> abschneiden • float x = li; • double x = c; • int x = b; • short int x = ld; • char x = i; • bool x = i; 2 © M. Gross, ETH Zürich, 2016 Recap last exercise • Question 1: • int i; long int li; float f; double d; long double ld; char c; bool b; • int x = f; -> abschneiden • short int x = li; -> bits werden abgeschnitten! • long int x = f; -> abschneiden • float x = li; -> Info-verlust wenn li zu gross • double x = c; • int x = b; • short int x = ld; • char x = i; • bool x = i; 2 © M. Gross, ETH Zürich, 2016 Recap last exercise • Question 1: • int i; long int li; float f; double d; long double ld; char c; bool b; • int x = f; -> abschneiden • short int x = li; -> bits werden abgeschnitten! • long int x = f; -> abschneiden • float x = li; -> Info-verlust wenn li zu gross • double x = c; -> ASCII Code von c • int x = b; • short int x = ld; • char x = i; • bool x = i; 2 © M. Gross, ETH Zürich, 2016 Recap last exercise • Question 1: • int i; long int li; float f; double d; long double ld; char c; bool b; • int x = f; -> abschneiden • short int x = li; -> bits werden abgeschnitten! • long int x = f; -> abschneiden • float x = li; -> Info-verlust wenn li zu gross • double x = c; -> ASCII Code von c • int x = b; -> false->0, true ->1 • short int x = ld; • char x = i; • bool x = i; 2 © M. Gross, ETH Zürich, 2016 Recap last exercise • Question 1: • int i; long int li; float f; double d; long double ld; char c; bool b; • int x = f; -> abschneiden • short int x = li; -> bits werden abgeschnitten! • long int x = f; -> abschneiden • float x = li; -> Info-verlust wenn li zu gross • double x = c; -> ASCII Code von c • int x = b; -> false->0, true ->1 • short int x = ld; -> abschneiden • char x = i; • bool x = i; 2 © M. Gross, ETH Zürich, 2016 Recap last exercise • Question 1: • int i; long int li; float f; double d; long double ld; char c; bool b; • int x = f; -> abschneiden • short int x = li; -> bits werden abgeschnitten! • long int x = f; -> abschneiden • float x = li; -> Info-verlust wenn li zu gross • double x = c; -> ASCII Code von c • int x = b; -> false->0, true ->1 • short int x = ld; -> abschneiden • char x = i; -> bits werden abgeschnitten! • bool x = i; 2 © M. Gross, ETH Zürich, 2016 Recap last exercise • Question 1: • int i; long int li; float f; double d; long double ld; char c; bool b; • int x = f; -> abschneiden • short int x = li; -> bits werden abgeschnitten! • long int x = f; -> abschneiden • float x = li; -> Info-verlust wenn li zu gross • double x = c; -> ASCII Code von c • int x = b; -> false->0, true ->1 • short int x = ld; -> abschneiden • char x = i; -> bits werden abgeschnitten! • bool x = i; -> x = (i != 0) 2 © M. Gross, ETH Zürich, 2016 Recap last exercise • Question 4: 3 © M. Gross, ETH Zürich, 2016 Recap last exercise • Question 4: value = 2; 3 © M. Gross, ETH Zürich, 2016 Recap last exercise • Question 4: value = 2; result = 7; 3 © M. Gross, ETH Zürich, 2016 Recap last exercise • Question 4: value = 2; result = 7; value = 3; 3 © M. Gross, ETH Zürich, 2016 Recap last exercise • Question 4: value = 2; result = 7; value = 3; return 7; 3 © M. Gross, ETH Zürich, 2016 Recap last exercise • Question 4: value = 2; result = 7; value = 3; return 7; returnedValue = 7; value = 2; 3 © M. Gross, ETH Zürich, 2016 Agenda • Arrays – 1-dimensional – 2-dimensional • C-Style strings 4 © M. Gross, ETH Zürich, 2016 Arrays • A datatype, storing multiple values of the same type • Definition consists of three elements: – Data type of elements 1 – Name of array 2 – Number of elements 3 1 2 3 int myArray[100]; // Allocate int array of size 100 5 © M. Gross, ETH Zürich, 2016 Arrays: Definition • Number of elements must be constant! int i=10; float myFloats[i]; // Invalid; must be sure about size of // array at compile time. const int j=10; float myFloats2[j]; // This is okay. 6 © M. Gross, ETH Zürich, 2016 Arrays: Indexing • Array elements are zero-indexed! – Range is from 0 to (number of elements) – 1 int myArray[7]; myArray 7 © M. Gross, ETH Zürich, 2016 Arrays: Initialisation and Assignment • Initialisation in two ways: – At definition: int a[5] = {0, 1, 2, 3, 4}; – Explicitly: int b[3]; b[0] = 7; b[1] = 3; b[2] = 3; 8 © M. Gross, ETH Zürich, 2016 Arrays: Initialisation and Assignment • Partial initialisation int c[5] = {1, 2}; // Equals {1,2,0,0,0} – Remaining elements are set to default value (0 for int) • Let compiler determine array size int d[] = {1, 2, 3}; // Equals int d[3]=… – Array takes minimum size to store given values. 9 © M. Gross, ETH Zürich, 2016 Arrays: Out of Bounds • Caution! Compiler doesn’t verify your indices. • Exceeding array bounds results in runtime errors. int e[3] = {1, 2, 3}; cout << e[0]; // Fine. cout << e[3]; // Out of bounds, but cout << e[9]; // compiler doesn’t complain • Accessing uninitialised memory – Results are undefined 10 © M. Gross, ETH Zürich, 2016 Arrays: Example • Finding the biggest value • Better: use for/while loops – See next exercise const int dim = 3; double values[dim] = {1.0, 8.0, -5.0}; double maxValue = values[0]; // Initialise if (values[1] > maxValue) maxValue = values[1]; if (values[2] > maxValue) maxValue = values[2]; cout << "Maximum value: " << maxValue << endl; // 8.0 11 © M. Gross, ETH Zürich, 2016 Arrays: Example II 12 © M. Gross, ETH Zürich, 2016 Arrays: Example II • int a [ ] = {3, 5, 8, 1, 0}; 12 © M. Gross, ETH Zürich, 2016 Arrays: Example II • int a [ ] = {3, 5, 8, 1, 0}; 12 3 © M. Gross, ETH Zürich, 2016 5 8 1 0 Arrays: Example II • int a [ ] = {3, 5, 8, 1, 0}; 3 • a[ 2 ] = 2; 12 © M. Gross, ETH Zürich, 2016 5 8 1 0 Arrays: Example II • int a [ ] = {3, 5, 8, 1, 0}; 3 5 8 1 0 • a[ 2 ] = 2; 3 5 2 1 0 12 © M. Gross, ETH Zürich, 2016 Arrays: Example II • int a [ ] = {3, 5, 8, 1, 0}; 3 5 8 1 0 • a[ 2 ] = 2; 3 5 2 1 0 • a [ a[3] ] = a [ 3 ] + 2; 12 © M. Gross, ETH Zürich, 2016 Arrays: Example II • int a [ ] = {3, 5, 8, 1, 0}; 3 5 8 1 0 • a[ 2 ] = 2; 3 5 2 1 0 • a [ a[3] ] = a [ 3 ] + 2; 3 3 2 1 0 12 © M. Gross, ETH Zürich, 2016 Arrays: Example II • int a [ ] = {3, 5, 8, 1, 0}; 3 5 8 1 0 • a[ 2 ] = 2; 3 5 2 1 0 • a [ a[3] ] = a [ 3 ] + 2; 3 3 2 1 0 • a[ a[4] ] = a[ a[3] + a[1] ]; 12 © M. Gross, ETH Zürich, 2016 Arrays: Example II • int a [ ] = {3, 5, 8, 1, 0}; 3 5 8 1 0 • a[ 2 ] = 2; 3 5 2 1 0 • a [ a[3] ] = a [ 3 ] + 2; 3 3 2 1 0 0 3 2 1 0 • a[ a[4] ] = a[ a[3] + a[1] ]; 12 © M. Gross, ETH Zürich, 2016 Multidimensional Arrays • Arrays of arbitrary size can be allocated • 2D array syntax: typename variableName [rows][columns]; 13 © M. Gross, ETH Zürich, 2016 Multidimensional Arrays • Definition and initialisation: int b[2][3] = {{1,2,3},{4,5,6}}; 14 © M. Gross, ETH Zürich, 2016 Multidimensional Arrays • Definition and initialisation: int b[2][3] = {{1,2,3},{4,5,6}}; • Assignment: b[1][2] = 12; int c = b[0][1]; 15 © M. Gross, ETH Zürich, 2016 c == 2 12 Multidimensional Arrays: Example int myArray[][4] = { {236, 189, 189, 0}, {236, 80, 189, 189}, {236, 0, 189, 80}, {236, 189, 189, 80}}; myArray • Representation of a simple greyscale image – Array elements as brightness values 16 © M. Gross, ETH Zürich, 2016 C-Strings • A string is an array of characters. – Represents a word, a sentence, etc. • cstring library provides many useful functions. #include <cstring> 17 © M. Gross, ETH Zürich, 2016 C-Strings • C-style strings must end with ’\0’ (zero character) – Valid: char name[5] = {'H','a','n','s','\0'}; – Invalid: char name[4] = {'H','a','n','s'}; This is a character array, but not a string! • Initialisation: char name[5] = {'H','a','n','s','\0'}; char name2[] = "Hans"; // Much nicer! 18 © M. Gross, ETH Zürich, 2016 C-Strings • Beware your quotes – data type mismatch: char a = 'x'; // Good. Single quotes denote a char char a = "x"; // Bad! Double quotes denote a string • Reading in a string: char name[100]; cin >> name; • Reading in a line including spaces: char sentence[200]; cin.getline(sentence, 200); 19 © M. Gross, ETH Zürich, 2016 C-Strings: Useful Functions • toupper (from library <ctype.h>): char str[] = "abc"; str[0] = toupper(str[0]); // str == Abc • strcat and strcpy: char str2[80]; strcpy(str2,str); strcat(str2,"def"); // str2 == Abc // str2 == Abcdef • strlen: int n; n = strlen(str2); 20 // n == 6 © M. Gross, ETH Zürich, 2016 Exercise 5 • • • • • Practice array declarations Programming: create, use, and modify arrays Programming: use C-style strings Experiment with UNIX user rights Hand in: – Solutions to tasks 1 (arrays) and 5 (user rights) – Source code for means2, temperature, names 21 © M. Gross, ETH Zürich, 2016