1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Programming and Data Structure
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
O-1 University of Washington Computer Programming I Lecture 14: Arrays © 2000 UW CSE.
Kernighan/Ritchie: Kelley/Pohl:
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on Arrays Using array elements as function arguments  Examples Using arrays as function.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on One-dimensional Arrays Using array elements as function arguments  Examples Using.
Programming with Collections Collections in Java Using Arrays Week 9.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
O-1 University of Washington Computer Programming I Lecture 14: Arrays © 2000 UW CSE.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
1 TDBA66, VT-04, Lecture Ch7 One dimensional Arrays A data structure (vector) with many elements of the same type A common name where individual elements.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 8 Arrays and Strings
C ARRAYS -a collection of same type data, 1D, 2D- © 1/25.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
1 ICS103 Programming in C Ch7: Arrays. O BJECTIVES What is an Array? Declaring Arrays Visual representation of an Array Array Initialization Array Subscripts.
Array Lesson 1 CS1313 Spring Array Lesson 1 Outline 1.Array Lesson 1 Outline 2.Mean of a List of Numbers 3.mean: Declarations 4.mean: Greeting,
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
A First Book of ANSI C Fourth Edition
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
1. 1. Introduction to Array 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5. Operations on Array 6. Multidimensional Arrays 7. Index.
ICS103 Programming in C Lecture 11: Arrays I
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Computer Programming for Engineers
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
ICS103: Programming in C 7: Arrays Muhamed F. Mudawar.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays. Arrays are objects that help us organize large amounts of information.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
L what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s.
Introduction to programming in java Lecture 21 Arrays – Part 1.
26/06/ :14:35 CSC Alliance — 1 Kimera Richard Phone: INSTITUTE OF COMPUTER SCIENCE DEPARTMENT.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
ARRAYS.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
EKT120 : Computer Programming
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
Arrays and Records.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CNG 140 C Programming (Lecture set 8)
EKT150 : Computer Programming
ICS103 Programming in C Lecture 13: Arrays II
Declaration, assignment & accessing
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays I Handling lists of data.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
ICS103 Programming in C Lecture 12: Arrays I
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

1 ICS103 Programming in C Lecture 12: Arrays I

2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional Arrays Visual representation of a One-dimensional Array One-dimensional Array Initialization One-dimensional Array Subscripts Accessing One-dimensional Array elements Examples

3 Motivation for One-dimensional Arrays Suppose ICS 103 has 250 students and we want to process Exam01 grades for these students to:  find the difference between each grade and the average  arrange the grades in increasing order (i.e., to sort the grades in increasing order) One approach to solve the first problem is to put the grades in a text file, read the text file the first time to find the average, and then read it again to find the differences. What if we do not want to use a text file? Then we can use the following approach: double grade1, grade2, grade3,..., grade250; scanf(“%lf”, &grade1); scanf(“%lf”, &grade2);... ; scanf(“%lf”, &grade250); double average = (grade1 + grade grade250) / 250; printf(“%f ”, grade1 – average); printf(“%f ”, grade2 – average);... ; printf(“%f ”, grade250 – average); How do we solve the second problem of sorting the grades in increasing order? Clearly, the above approaches are not practical. Is there a better approach to solve the above problems and similar problems? The answer is YES; Use One-dimensional array to group a large number of variables, and then manipulate them as a single entity.

4 What is a One-dimensional Array? A scalar variable is one that can hold only one value at a time. For many problems you need to group data items together. A program that processes exam scores for a class, for example, 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. C allows a programmer to group such related data items together into a single composite data structure. A data structure is a grouping of related data items in memory. We now take a look at one such data structure: the Array. An array is a collection of two or more adjacent memory cells that:  Store the same type of data values.  Are referenced by the same name (i.e using one variable). These individual cells are called array elements.

5 Declaring One-dimensional Arrays To declare an array, we must declare its name, type of data values it will store and the number of cells associated with it. Example: double x[8]; This instructs C to associate eight memory cells with the name x; these memory cells will be adjacent to each other in memory. You can declare arrays along with scalar variables: double temperature[30], maxTemp, rainfall[30], maxRainfall; It is a good practice to define the array size as constant: #define ARRAY_SIZE 12 //... int myArray[ARRAY_SIZE];

6 Declaring One-dimensional Arrays (cont’d) Each element of the array x may contain a single value of type double, so a total of eight such numbers may be stored and referenced using the array name x. The elements are numbered starting with 0  An array with 8 elements has elements at 0,1,2,3,4,5,6, and 7 The subscripted variable x[0] (read as x sub zero) refers to the initial or 0 th element of the array x, x[1] is the next element in the array, and so on. The integer enclosed in brackets is the array subscript or index and its value must be in the range from 0 to arraySize - 1.

7 Visual representation of a One-dimensional Array ? ? 20 ? ? ? ? ? int x[8]; x[2] = 20; Memory Addresses Array Index/Subscript Array Element Note: Index starts with 0, not with 1

8 One-dimensional Array Initialization In C, when you declare a variable, its value is not initialized unless you specify so. int sum; // Does not initialize sum int product = 1; // Initializes product to 1 Arrays, like scalar variables, are not initialized by default int x[10]; //creates the array, but doesn’t set any of its values. You can declare and initialize an array at the same time, like: int x[10] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; The array elements are initialized in the order listed x[0] is initialized to 2 x[4] is initialized to 11

9 One-dimensional Array Initialization (cont’d) If the values in the initialization block are not enough to fill the array, all the elements in the array without values are initialized to 0 in the case of double or int, and NULL in the case of char. int scores[20] = {0}; // all 20 elements are initialized to 0 int scores[20] = {1, 2, 3}; // First 3 elements are initialized to 1, 2, // 3 and the rest are initialized to 0 If there are values in the initialization block, an explicit size for the array does not need to be specified. Only an empty array element is sufficient, C will count the size of the array for you. int scores[ ] = {20, 10, 25, 30, 40}; // size of the array score is // automatically calculated as 5

10 One-dimensional Array Subscripts We use subscripts/indices to differentiate between the individual array elements. We can use any expression of type int as an array subscript. However, to create a valid reference, the value of this subscript must lie between 0 and arraySize - 1. It is essential that we understand the distinction between an array subscript value and an array element value. int x[2], y = 1; x[y] = 5; The subscript is y (which is 1 in this case), and the array element value is 5 C compiler does not provide any array bound checking. As a programmer it is your job to make sure that an array index is valid (i.e. it falls within the boundary of the array).

11 Accessing One-dimensional Array elements An array element is accessed by using its index: point[1] // the 2 nd element of array point is accessed point[9] = *point[3]; // the 10 th element of array point is assigned //the value of the expression We usually use a loops to manipulate arrays: Example: Adding the values of all array elements Two alternative style for loops: int i; for(i = 0; i < ARRAY_SIZE; i++) sum += a[i]; for(i = 0; i <= ARRAY_SIZE -1; i++) sum += a[i]; Note : The array element is a single valued variable of the corresponding type and can be manipulated as a variable of that type.

12 Example 1 /* Reads five grades and prints them */ #include #define SIZE 5 int main(void) { double grades[SIZE] ; // array declaration int i ; printf("Enter five grades to store in array : \n"); printf("*********************************\n\n"); for (i = 0; i < SIZE; ++i) // loop to read the five grades into the array { printf ("Enter the %d element of array : ", i ) ; scanf ( "%lf", &grades[i] ) ; } printf("\n"); for (i = 0; i < SIZE; ++i) // loop to display five grades stored in the array printf ("The %d th element of array is %f\n", i, grades[i]) ; system("pause"); return 0; }

13 Example 2 /* Reads data into two arrays and subtracts their corresponding elements, storing the result in another array. */ #include #define SIZE 5 int main(void) { int first[SIZE], second[SIZE], diff[SIZE], i; printf("Enter %d data items for first array : ", SIZE); for(i=0;i<SIZE; i++) // input first array scanf("%d",&first[i]); printf("Enter %d data items for second array : ", SIZE); for(i=0;i<SIZE; i++) // input second array scanf("%d",&second[i]); for(i=0;i<SIZE; i++) // compute the differences diff[i]= second[i] - first[i]; printf("\n\nOutput of the arrays : \n"); for(i=0;i<SIZE; i++) // output the arrays printf("%5d %5d %5d\n", first[i], second[i], diff[i]); system("pause"); return 0; }

14 Example 3 /* Computes the mean and standard deviation of an array of data and displays the difference between each value and the mean. */ #include #define SIZE 8 /* maximum number of items in list of data*/ int main(void) { double x[SIZE], mean, st_dev, sum, sum_sqr; int i; /* Gets the data*/ printf("Enter %d numbers separated by blanks\n> ", SIZE); for (i = 0; i < SIZE; ++i) scanf("%lf", &x[i]); /* Computes the sum and the sum of the squares of all data*/ sum = 0; sum_sqr = 0; for (i = 0; i < SIZE; ++i) { sum += x[i]; sum_sqr += x[i] * x[i]; }

15 Example 3… /* Computes and prints the mean and standard deviation*/ mean = sum / SIZE; st_dev = sqrt(sum_sqr / SIZE - mean * mean); printf("The mean is %.2f.\n", mean); printf("The standard deviation is %.2f.\n", st_dev); /* Displays the difference between each item and the mean*/ printf("\nTable of differences between data values and mean\n"); printf("Index Item Difference\n"); for (i = 0; i < SIZE; ++i) printf("%3d%4c%9.2f%5c%9.2f\n", i, ' ', x[i], ' ', x[i] - mean); system("pause"); return (0); }