lecture 9 lecture 9
play

Lecture 9 Lecture 9 2. B inary R epresentation Arrays 3. H - PDF document

1. Introduction Lecture 9 Lecture 9 2. B inary R epresentation Arrays 3. H ardw are and S oftw are 4. H igh Level Languages 5. S tandard input and output For the last two lectures we have looked at 6. Operators, expression


  1. 1. Introduction Lecture 9 Lecture 9 2. B inary R epresentation Arrays 3. H ardw are and S oftw are 4. H igh Level Languages 5. S tandard input and output • For the last two lectures we have looked at 6. Operators, expression and statem ents algorithms. We shall now return to data structures. 7. M aking Decisions • So far we have only looked at simple variables, 8. Looping – integers 9. A rrays – characters 10. B asics of pointers – floating point numbers (real numbers) 11. S trings • But often we get data in sets of ‘like’ objects, e.g. 12. B asics of functions – images - 2D array of pixels 13. M ore about functions – strings - 1D array of characters 14. Files • The order of the data is important 14. D ata S tructures 16. C ase study: lottery num ber generator Declaring Arrays Declaring Arrays • We can assign initial values to any array, • C provides a simple data structure, applicable int a[3]={10,11,12}; to all data types: the array or int a[]={10,11,12}; – int teleph[100]; /* 100 telephone numbers*/ • C counts the number of items in the initialisation list – float marks[115]; /* 115 exam marks */ • The above declaration assigns values to the individual – char text[20]; /* a string 20 characters long */ array elements as follows: • We can use any constant integer expression a[0]=10, a[1]=11, a[2]=12 to declare the size of an array: • Each a[…] is an integer variable. They are identified by subscripts in square brackets [ ] #define CLASS_SIZE 108 /* number of students*/ • In C, subscripts always begin at Zero i.e. a[0] - - - - float marks[CLASS_SIZE]; – Well see why in the next lecture int thing[2*3+5]; Array Storage Subscripts and loops • It is very common to use a for loop to access • Array elements are stored sequentially in memory, each element of an array in turn. starting with the [0] element /* Example: arrays */ /* Example: arrays */ low memory address low memory address /* Example: arrays */ /* Example: arrays */ #include <stdio.h> array1.c #include <stdio.h> array2.c array2.c array1.c #include <stdio.h> #include <stdio.h> #define MAX 21 17 24 3 12 92 18 24 #define MAX 21 main() main() main() main() { { { int u; { X[0] X[1] X[2] X[3] X[4] X[5] X[6] int u; int u; int u; long p2[21] = { 1, 2, 4, 8, 16, 32, 64, 128, long p2[21] = { 1, 2, 4, 8, 16, 32, 64, 128, long p2[MAX]; /* powers of two */ 256, 512, 1024, 2048, 4096, long p2[MAX]; /* powers of two */ int x[7]={17,24,3,12,92,18,24}; 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 8192, 16384, 32768, 65536, /* initialise array: */ /* initialise array: */ 131072, 262144, 524288, • Note that although we declare x[7] (7 elements), the 131072, 262144, 524288, 1048576 1048576 p2[0] = 1; p2[0] = 1; }; highest subscript is 6. If we try to access x[7] the }; for (u = 1; u < MAX; u++) for (u = 1; u < MAX; u++) p2[u] = 2 * p2[u - 1]; computer will let us but the result will be rubbish or p2[u] = 2 * p2[u - 1]; for (u = 0; u < 21; u++) for (u = 0; u < 21; u++) printf("2 to the power %2i = %7li\n", u, p2[u]); printf("2 to the power %2i = %7li\n", u, p2[u]); /* print: */ may result in the programming crashing /* print: */ } } for (u = 0; u < MAX; u++) for (u = 0; u < MAX; u++) printf("2 to the power %2i = %7li\n", u, p2[u]); printf("2 to the power %2i = %7li\n", u, p2[u]); } } 1

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend