lecture08_4on1 - CS1010E Lecture 8 Arrays Henry Chia...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS1010E Lecture 8 Arrays Henry Chia [email protected] Semester 1 2011 / 2012 Department of Computer Science School Of Computing National University Of Singapore CS1010E Lecture 8 – p.1/34 Lecture Outline One-Dimensional Arrays Declaration and initialization Array element access Arrays and pointers Arrays as function arguments File Processing File Input File Output CS1010E Lecture 8 – p.2/34 Problem Statement #1 Read 100 values and find the average. Requires one pass through the values. #include <stdio.h> #define MAX 100 int main( void ) { int i, val, sum=0; double ave; for (i = 1; i <= MAX; i++) { scanf( "%d" , &val); sum += val; } ave = 1.0 * sum / MAX; printf( "The average is %lf\n" , ave); return 0; } CS1010E Lecture 8 – p.3/34 Problem Statement #2 Read 100 values from the user and find the number of values greater than the average. Requires two passes through the values. Pass #1: Find the average. Pass #2: Count values greater than the average. Program design: Declare 100 variables for input values. Read and compute the sum of all values. Compute the average. Determine how many variables have values greater than the average using 100 if statements. CS1010E Lecture 8 – p.4/34 One-Dimensional Arrays Work with a set of data values of the same type without giving each value a separate name. An array is given an identifier name. Subscripts distinguish between array values. Subscripts start with and increment by 1 . Memory assignment for an array is guaranteed to be a sequential group of memory locations. CS1010E Lecture 8 – p.5/34 One-Dimensional Arrays @100 @101 @102 @103 @104 @105 s: 5-1 2 15 2 s[0] s[1] s[2] s[3] s[4] s[5] First value in array s is s[0] ; last (sixth) value is s[5] . s is a symbolic address to the first value; it is not a variable. In the above, s is @100 . Memory location for s[1] will immediately follow the memory location for s[0] , etc. Actual addresses of adjacent elements might not differ by one, but by a fixed offset. CS1010E Lecture 8 – p.6/34 Array Declaration An array identifier is followed by an integer expression in brackets that specifies the maximum number of elements in the array. Elements in an array must be the same data type, eg. int s[6]; double list[100]; struct Fraction fracArray[50]; int * ptr[10]; int * ptr[10] is a declaration of an array of ten pointers to integers. This is different from int ( * ptr)[10] which is a pointer to an array of ten integers. CS1010E Lecture 8 – p.7/34 Declaration and Initialization An array can be initialized during declaration. To initialize the array, values are specified (comma-separated) in an initializer list. int s[6]={5,0,-1,2,15,2}; Initializer lists may be shorter than the array size. The following are equivalent....
View Full Document

This note was uploaded on 12/13/2011 for the course ELECTRICAL CS1010E taught by Professor Henrytan during the Spring '11 term at National University of Singapore.

Page1 / 9

lecture08_4on1 - CS1010E Lecture 8 Arrays Henry Chia...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online