Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computing Lecture 10 Arrays (Part 1)

Similar presentations


Presentation on theme: "Introduction to Computing Lecture 10 Arrays (Part 1)"— Presentation transcript:

1 Introduction to Computing Lecture 10 Arrays (Part 1)
Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

2 Topics Arrays Declaration Initialization Input/Output
Passing arrays to functions

3 Introduction to Arrays
Simple data types use a single memory cell to store a variable To solve many programming problems, it is more efficient to group data items together in main memory E.g., a program that processes exam scores for a class would be easier to write if all the scores were stored in one area of memory and were able to be accessed as a group

4 Arrays A group of contiguous memory locations used to store a series of related values All values have the same type Each storage location in an array is called an array element The array name is a pointer to the first element Individual elements of an array are accessed via an integer index: array[index] Element indices start at 0: array[0]is the first element

5 Example

6 Single Dimensional Arrays
Declaration type_name array_name[array_size] float expenses[12]; int rainfall[12]; Usage printf(“%.1f”, expenses[0]); expenses[0] = 10.40; expenses[3] = expenses[11]; int i=6; expenses[i] = 44.1;

7 Caution In an array of n elements, the allowable subscripts range from 0 to n-1 int array[10]; array[0] = …; array[9] = …; array[10] = …; OK! OK! Wrong!!

8 Array Subscripts A subscript is used to differentiate between the individual array elements and to specify which array element is to be manipulated We can use any expression of type int as an array subscript Important! The value of this subscript must lie between 0 and one less than the declared size of the array

9 Example i=5; printf(“%.1f”,x[4]); printf(“%.1f”,x[i]); 2.5
12.0 13.0 14.0 invalid -54.5

10 Initialization Arrays may be initialized with a list of suitable values No need to specify the number of elements for a 1D (1-dimensional) array E.g. int primes_lt_20[] = {2, 3, 5, 7, 11, 13, 17, 19};

11 Using for loops for sequential access
Generally, the elements of an array are processed in sequence, starting with element 0 E.g., scanning data into the array or printing its contents A for loop can be used whose loop control variable run from zero to one less than the array size Use the loop counter as an array index to access each array element in turn

12 Example: MonthlyRainfall
Problem: using Rainfall Table input month output mean rainfall for that month

13 Example (cont): MonthlyRainfall (v.1)
#include <stdio.h> int main() { int month; int table[12] = { 30, 40, 45, 95, 130, 220, 210, 185, 135, 80, 40, 45 }; printf("Enter month: "); scanf("%d", &month); printf("Average rainfall: %d mm.\n", table[month-1]); return 0; } rainfall1.c

14 Example (cont): MonthlyRainfall (v.1)
#include <stdio.h> int main() { int month; int table[12] = { 30, 40, 45, 95, 130, 220, 210, 185, 135, 80, 40, 45 }; printf("Enter month: "); scanf("%d", &month); printf("Average rainfall: %d mm.\n", table[month-1]); return 0; } rainfall1.c

15 Input / Output of Arrays
Library functions printf() and scanf() do not know about arrays So we have to do I/O ourselves

16 Example: IORainfall-1 rainio1.c #include <stdio.h>
#define NMONTHS 12 /* Store and print rainfall */ int main() { int table[NMONTHS]; int month; for ( month=0; month < NMONTHS; month++ ) printf("Enter data for the %d. month: ",month + 1); scanf("%d", &table[month]); } ... rainio1.c

17 Example (cont): IORainfall-1
#include <stdio.h> #define NMONTHS 12 /* Store and print rainfall */ int main() { int table[NMONTHS]; int month; for ( month=0; month < NMONTHS; month++ ) printf("Enter data for the %d. month: ",month + 1); scanf("%d", &table[month] ); } ... rainio1.c

18 Example (cont): IORainfall-2 (v.1)
#include <stdio.h> #define NMONTHS 12 ... /* Print from January to December */ for ( month=0; month < NMONTHS; month++ ) { printf( "%d ", table[month] ); } printf("\n"); /* Print from December to January */ for ( month = NMONTHS - 1; month >= 0; month-- ) return 0; rainio1.c

19 Example (cont): IORainfall-2 (v.1)
#include <stdio.h> #define NMONTHS 12 ... /* Print from January to December */ for ( month=0; month < NMONTHS; month++ ) { printf( "%d ", table[month] ); } printf("\n"); /* Print from December to January */ for ( month = NMONTHS - 1; month >= 0; month-- ) return 0; rainio1.c

20 Example (cont): IORainfall-2 (v.2)
#include <stdio.h> #define NMONTHS 12 ... /* Print from January to December */ for ( month=0; month < NMONTHS; month++ ) { printf( "%5d ” , table[month] ); } printf("\n"); /* Print from December to January */ for ( month = NMONTHS - 1; month >= 0; month-- ) return 0; rainio2.c

21 Handling Indices Arrays have a fixed size
There is no built-in way of checking if the supplied index is within range We must check for valid indices ourselves

22 Example (cont): MonthlyRainfall (v.2)
#include <stdio.h> #define MAXLEN 1024 int main() { int month; int table[12] = { 30, 40, 45, 95, 130, 220, 210, 185, 135, 80, 40, 45 }; while(1) printf("Enter month or ctrl-c to end: "); scanf("%d", &month); if (1 <= month && month <= 12) /* input in range? */ printf("Average rainfall for month %d is %d mm.\n",month,table[month-1]); } else printf("Month should be between 1 and 12. Try again.\n"); return 0; rainfall2.c

23 Example (cont): MonthlyRainfall-1 (v.3)
#include <stdio.h> int rainfall(int month); /* Main program to test rainfall() function. */ int main() { int month; while(1) printf("Enter month or ctrl-c to end: "); scanf("%d", &month); if (1 <= month && month <= 12) printf("Average rainfall for month %d is %d mm.\n",month,rainfall(month-1)); } else printf("Month should be between 1 and 12. Try again.\n"); return 0; rainfall3.c

24 Example (cont): MonthlyRainfall-2 (v.3)
/**********************************************************\ * NAME: * int rainfall(int month) * DESCRIPTION: * Returns the mean monthly rainfall (in millimeters) * in a given month * PRE: * The integer `month' must be between 0 and 11, where * 0 = January, 1 = February, etc. Otherwise, the behaviour * is undefined * The local array `table' should be initialized to contain * the average rainfall in a given month * POST: * It returns an integer value corresponding to the mean * rainfall (in millimeters) for the given `month' \**********************************************************/ int rainfall ( int month ) { int table[12] = { 30, 40, 45, 95, 130, 220, 210, 185, 135, 80, 40, 45 }; return (table[month]); } rainfall3.c

25 Example (cont): MonthlyRainfall-2 (v.3)
/**********************************************************\ * NAME: * int rainfall(int month) * DESCRIPTION: * Returns the mean monthly rainfall (in millimeters) * in a given month * PRE: * The integer `month' must be between 0 and 11, where * 0 = January, 1 = February, etc. Otherwise, the behaviour * is undefined * The local array `table' should be initialized to contain * the average rainfall in a given month * POST: * It returns an integer value corresponding to the mean * rainfall (in millimeters) for the given ‘month’ \**********************************************************/ int rainfall ( int month ) { int table[12] = { 30, 40, 45, 95, 130, 220, 210, 185, 135, 80, 40, 45 }; return (table[month]); } rainfall3.c

26 Passing Arrays to Functions
The array is passed as an array of unspecified size (int array[]) OR as a pointer (int *array) Changes to the array within the function AFFECT the “original” array If you want the use the array only as an input argument, you have to use the const keyword int get_max(const int list[], int n);

27 Example (cont): IORainfall-1 (v.3)
#include <stdio.h> #define NMONTHS 12 void loadRain ( int arrayPtr[] ) { int month; for (month=0; month < NMONTHS; month++) scanf("%d", &arrayPtr[month]); } rainio3.c

28 Example (cont): IORainfall-2 (v.3)
void printRain ( const int arrayPtr[] ) { int month; for (month=0; month < NMONTHS; month++) printf("%5d", arrayPtr[month]); } printf("\n"); rainio3.c

29 Example (cont): IORainfall-3 (v.3)
#include <stdio.h> #define NMONTHS 12 void loadRain ( int arrayPtr[] ); void printRain ( const int arrayPtr[] ); /* Store and print rainfall */ int main() { int data[NMONTHS]; loadRain(data); printRain(data); return 0; } rainio3.c

30 Example: IORainfall -- v.3 (cont)
#include <stdio.h> #define NMONTHS 12 void loadRain ( int arrayPtr[] ); void printRain ( const int arrayPtr[] ); /* Store and print rainfall */ int main() { int data[NMONTHS]; loadRain(data); printRain(data); return 0; } /* Read in rainfall for each month*/ void loadRain ( int arrayPtr[] ) int month; for (month=0; month < NMONTHS; month++) { printf(“Enter rainfall data for the %d. month:” ,month+1); scanf("%d", &arrayPtr[month]); } /* Print rainfall for each month*/ void printRain ( const int arrayPtr[] ) { printf("%5d", arrayPtr[month]); } printf("\n"); rainio3.c

31 Returning an Array Result
In C, it is NOT legal for a function’s return type to be an array An output parameter should be used to send the result array back to the caller

32 Example - Returning an Array Result
void add_arrays(const double ar1[], const double ar2[], double arrsum[], int n) { int i; for(i = 0 ; i < n; ++i) arsum[i] = ar1[i] + ar2[i]; }

33 Example - Returning an Array Result
void add_arrays(const double ar1[], const double ar2[], double arrsum[], int n) { int i; for(i = 0 ; i < n; ++i) arsum[i] = ar1[i] + ar2[i]; }


Download ppt "Introduction to Computing Lecture 10 Arrays (Part 1)"

Similar presentations


Ads by Google