ARRAYS.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Introduction to C Programming
Kernighan/Ritchie: Kelley/Pohl:
Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University.
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 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Nested LOOPS.
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
ICS103 Programming in C Lecture 11: Arrays I
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
310201: Fundamental Programming Fundamental Programming Introduction to Arrays.
Repetition statements
Decision making If.. else statement.
Arrays Low level collections.
An Introduction to Programming with C++ Sixth Edition
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
multi-dimensional arrays
© 2016 Pearson Education, Ltd. All rights reserved.
Array, Strings and Vectors
EKT120 : Computer Programming
Arrays Declarations CSCI N305
Lecture-5 Arrays.
REPETITION STATEMENTS
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
New Structure Recall “average.cpp” program
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
Lecture2.
C++ Arrays.
Arrays and Records.
Control Statement Examples
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, For loop While loop Do while loop
Visual Basic .NET BASICS
PROGRAM STRUCTURE CSC 111.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
One-Dimensional Array Introduction Lesson xx
INPUT & OUTPUT scanf & printf.
CNG 140 C Programming (Lecture set 8)
Functions.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
EKT150 : Computer Programming
Declaration, assignment & accessing
1) C program development 2) Selection structure
EKT120: Computer Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Functions with arrays.
Decision making If statement.
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
REPETITION STATEMENTS
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays I Handling lists of data.
Functions Extra Examples.
C++ Array 1.
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
Dr. Khizar Hayat Associate Prof. of Computer Science
Presentation transcript:

ARRAYS

For such type of problems, it is more convenient to use arrays. 1. DATA TYPES Simple data types (int, double, char) use a single memory cell to store a variable. However, it is sometimes more efficient to solve problems by grouping data items together for storage in main memory. For such type of problems, it is more convenient to use arrays. In fact, a string is an array of characters. Dr. Soha S. Zaghloul 2

2. DATA TYPES in memory The following declarations of simple data types are reflected in memory as shown in figure 1: int sum = 0; double average = 0.0; char choice = ‘X’; Sum Choice Average X 0.0 Dr. Soha S. Zaghloul 3

3. Declaration of arrays An array is a collection of two or more adjacent memory cells, called “array elements”. Elements of the same array should be ALL of the same type: this represents the type of the array. Array elements are associated with a particular symbolic name (identifier name) that obeys all rules previously mentioned about identifiers. Therefore, to set up an array in memory, we must declare the following: The type of the array: which is the type of its elements, The name of the array, The number of elements of the array: which is the number of adjacent cells in memory associated with the name of the array. Dr. Soha S. Zaghloul 4

4. Declaration of arrays – example (1) Consider the following array declaration: double x[8]; The above declaration instructs the compiler to allocate 8 adjacent memory cells with the name x. All cells are of type double. The memory layout is shown in the figure below: x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] Dr. Soha S. Zaghloul 5

Array subscripts are always of type integer. To process the data stored in an array, we refer to each individual element by specifying: The array name, The order of the element in the array (or subscript) Array subscripts always start with zero, and end with the array size minus one. For example, if the array contains 8 elements, then the subscript ranges from 0 to 7. Array subscripts are always of type integer. Array subscripts should be constant values. Array subscripts are enclosed between square brackets [ ]. Dr. Soha S. Zaghloul 6

6. Array subscripts – example (1) cnt’d In the previously declared array x (Refer to slide #5), assume we give values to the array elements using the following statements: x[0] = 16.0; x[1] = 12.0; x[2] = 6.0; x[3] = 8.0 x[4] = 2.5; x[5] = 12.0; x[6] = 14.0; x[7]= -54.5; This is reflected in memory as follows: x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] 16.0 12.0 6.0 8.0 2.5 14.0 -54.5 Dr. Soha S. Zaghloul 7

7. Array manipulation The following are some statements that manipulate the array x, and their explanation: x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] 16.0 12.0 6.0 8.0 2.5 14.0 -54.5 sum 28.0 28.0 28.0 26.0 25.0 Statement Explanation Result printf (“%.1f”, x[0]); Display the value of x[0] 16.0 x[3] = 25.0; Changes the value of x[3] to 25.0 double sum; sum = x[0] + x[1]; Store the sum of x[0] and x[1] (28.0) in sum sum += x[2]; Add x[2] to sum x[3] += 1.0; Add 1 to x[3] x[2] = x[0] + x[1]; Stores the sum of x[0] and x[1] (28.0) in x[2] Dr. Soha S. Zaghloul 8

Consider the following example: 8. More about subscripts Array subscripts should be constant. However, these constants may be declared with the directive #define. Consider the following example: #include <stdio.h> #define NUM_STUDENTS 50 Int main (void) { int id[NUM_STUDENTS]; double gpa[NUM_STUDENTS]; //the rest of the program } // end main Dr. Soha S. Zaghloul 9

9. A programming hint – parallel arrays #include <stdio.h> #define NUM_STUDENTS 50 Int main (void) { int id[NUM_STUDENTS]; double gpa[NUM_STUDENTS]; //the rest of the program } // end main In the shown example, we have two arrays of the same size (NUM_STUDENTS) This can be useful in problem solving by associating the two arrays using the subscript. In other words, each subscript in both arrays represent the same student. For example, gpa [0] is the gpa of the student of ID = id [0]; gpa [15] is the gpa of the student of ID = id [15], and so on. Dr. Soha S. Zaghloul 10

9. A programming hint – parallel arrays (cnt’d) id[0] 433122 id[1] 433200 id[2] 433333 … id[30] 433818 id[31] 432900 id[49] 433777 gpa[0] 2.71 gpa[1] 3.09 gpa[2] 2.98 … gpa[30] 4.50 gpa[31] 4.03 gpa[49] 4.8 Because the data stored in id[i] and gpa[i] relate to the ith student, the two arrays are called parallel arrays. Dr. Soha S. Zaghloul 11

An array can be initialized when we declare it as follows: 10. Array initialization An array can be initialized when we declare it as follows: int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; In the previous declaration, the array size is omitted. The array size is therefore specified by the number of elements listed between braces. In the above example, the array size is 10. The subscripts go from 0 to 9. Another example: char vowels[] = {‘A’, ‘E’, ‘I’, ‘O’, ‘U’}; This is an array of characters of size 5. The subscripts go from 0 to 4. Dr. Soha S. Zaghloul 12

What is the difference in meaning between x3 and x[3]? 11. Quick questions What is the difference in meaning between x3 and x[3]? For the declaration char grades[5]; How many memory cells are allocated for data storage? What type of data can be stored? How does one refer to the initial array element? How does one refer to the final array element? Dr. Soha S. Zaghloul 13

12. Example (2) Declare one array for storing the numbers from 0 to 9; another array to store their square, and a third array for storing the cubes of the same integers. Note that the subscripts of the arrays square and cube may be used to recognize the integer. For example, square[2] = 4, square[9] = 81, and the same for the cube array. int numbers[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} int square[] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100}; int cube[] = {0, 1, 8, 27, 64, 125, 216, 343, 512, 1000}; Dr. Soha S. Zaghloul 14

13. Array initialization using loops The following example declares an array of integers; then initializes all its elements to zero. Very often, loops are used with arrays to process its elements in sequence. In this case, the loop counter (i) is equal to the array subscript. int i, numbers[100]; for (i = 0; i< 100; i++) numbers [i] = 0; Dr. Soha S. Zaghloul 15

14. Example (3) The array square and cube in example 2 can also be initialized as follows: int i, square[10]; for (i = 0; i< 10; i++) { square[i] = i * i; cube[i] = i * i * i; } // end for Dr. Soha S. Zaghloul 16

15. Array initialization using scanf We may also get the values of the array elements from the user as follows: int i, number[10]; for (i = 0; i< 10; i++) { printf (“Enter an integer> “); scanf (“%d”, &number[i]; } // end for Dr. Soha S. Zaghloul 17

16. self-check exercises Write a complete programs that calculates the product of the integer of an array of size 100. The array elements are entered by the user. Write a complete programs that accepts 50 array elements of type double, and produces another array that represents the corresponding absolute values of the first array. Dr. Soha S. Zaghloul 18