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.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

The simple built-in data types of the C language such as int, float, - are not sufficient to represent complex data such as lists, tables, vectors, and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
An Introduction to Programming with C++ Fifth Edition
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 12: Arrays.
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Arrays--data structure. Arrays 7 zConsider how you would store five integer values in the memory of the computer. zOne way to do this is to create five.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Chapter 7 Arrays. Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3 Programming with Arrays 7.4 Multidimensional Arrays.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
chap8 Chapter 8 Arrays (Hanly) chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 7 Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
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.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Module 1: Array ITEI222 - Advance Programming Language.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
26/06/ :14:35 CSC Alliance — 1 Kimera Richard Phone: INSTITUTE OF COMPUTER SCIENCE DEPARTMENT.
An Introduction to Programming with C++ Sixth Edition
Arrays 2.
Computer Programming BCT 1113
Chapter 7 Arrays.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
Chapter 7 Arrays PROGRAMMING IN ANSI C.
Data Structures (CS212D) Week # 2: Arrays.
Arrays.
Presentation transcript:

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 data items. An array in C is a collection of data items of the same type, associated with a single name. Any valid variable name can bused as the name of the array. The individual elements of an array are referenced by appending a subscript to the array name. For example, we can create an array x[] consisting of six elements. The individual elements of the array are x[0], x[1], x[2], x[3], x[4], x[5]. The array subscript, which is the number enclosed in square brackets after the array name, defines the position of the individual element in the array.

7.2 Array Declaration Before we can use an array in a C program we must first declare an array. An array declaration specifies the type of the array and the number of elements in the array. All array in C must be explicitly declared so that the compiler can allocate the necessary memory for the array. The general form of the declaration for a one dimensional array is: type_specifier array_name [ size ] in which type_specifier is the data type of each element in the array, array_name is the name of the array and the size in square brackets is the number of elements in the array. Array anmes use the same naming convention as variable names.

Examples of array declaration are as follows: int x [8]; float alpha [50]; double beta [100]; char buffer [ ]; 7.3 Array Initialization The general form of array initialization for a one dimensional array is as follows: type_specifier array_name[size] = {list_of_values}

In which list_of_values is a comma separated list of constants having the same data type as the array. When the array is initialized, the first constant in the list is assigned to the first element of the array, the second constant is assigned to the second element, and so on. For example, the statement int x [5] = { 1,2,3,4,5} assigns the following values to the elements of the array x[ ]: Element ==> x[0] x[1] x[2] x[3] x[4] Value ==>

Example: Average of Test Scores problem statement: write a program to read test scores in an array and compute the average test score. solution: the program first reads in the number of test scores. It then reads in the individual test scores. The scores are stored in an array scores[], which is defined as float scores [ MAX_SCORES ]; The symbolic constant MAX_SCORES contains the size of the array and is defined to be equal to 100 in the preprocessor statement #define MAX_SCORES 100;

#include #define MAX_SCORES 100; main() { float scores [MAX_SCORES]; float average, sum = 0.0; int n, i = 0; printf(“\n Enter number of students in class: “); scanf(“%d”, &n); while (i < n) { printf(“\n Enter score # %d : “, i+1); scanf(“%f”, &scores[i]); sum += scores[i]; } average = sum/n; printf(“\n The average score is: %.2f”, average); }

7.4 Arrays as Function Arguments An entire array can be passed to a function as an argument. To pass an array to a function, we simply state the array’s name without the subscript in the function call, For example, to pass an array defined as int scores[10] = {1,2,3,4,5,6,7,8,9,10}; to a function called score_list() that sorts the elements of the array we could use the following call sort_list ( scores ); For a function to receive an array, the array has to specified in the formal parameter list. The parameter list for a one-dimensional array includes the type of the array followed by empty square brackets. For example, the function header for the function sort_list() would be void sort_list ( int a[ ] );

7.5 Sorting Sorting is the process of arranging data according to some specified order. Examples of sorting include arranging a list of numbers in numerical order and arranging a list of names in alphabetical order. Computers are extremely useful for performing such operations. Sorting is and extremely important task and has therefore received considerable attention by software developers. Many sorting procedures have been developed, and many books have been written on the subject. Since the procedures can be extremely time consuming, even when performed on a computer, the objective of most sorting algorithms is to make the task as efficient as possible.

7.5.1 Selection Sort One of the simplest algorithms for sorting a list of elements is the selection sort. The selection sort algorithm is similar to the commonsense approach that you would use to sort a list. It consists of the following steps: 1. Find the smallest element in the list of n elements 2. Place this element at the top of the list 3. Find the smallest element in the remaining list of n-1 elements. 4. Place this element in the second position in the list 5. Repeat until the remaining list contains only one element.

As an example of the selection sort procedure, consider the array x[ ] containing six elements that has the following initial configuration: x1 x2x3x4x5 x

Selection Sort

7.5.2 The Bubble Sort The bubble sort is the simplest sorting algorithm. The procedure consists of comparing adjacent pairs of elements in the array to be sorted. The first two numbers are compared, and if the second is smaller than the first, the numbers are exchanged. Then the values of the next adjacent pair of elements are compared and exchanged if necessary. This sequence of comparisons is continued until the last two elements of the array have been compared.

As an example of the bubble sort procedure, consider the array x[ ] containing six elements that has the following initial configuration: x1 x2x3x4x5 x

Pass1: From the figure shows all the comparisons and exchanges that take place during the first pass. The following steps are performed 1.The first two numbers are compared and exchanged since x2 is smaller than x1 2. The second and third numbers are compared. No exchange is made since the third is larger than the second. 3. x3 and x4 are compared and interchanged since x4 is smaller than x3. 4. x4 and x5 are compared and swapped. 5. Finally, the last two elements x5 and x6 are compared and exchanged. Note that the largest number has now “sunk” to the bottom of the list. Bubble sort, pass 1.

Pass2: From the figure shows all the exchanges made during the second pass. At the end of the second pass, the second largest number in the array is moved in x5. Bubble sort, pass 2.

Pass 3, 4,and 5: From the figure shows the contents of the array at the end of passes 3, 4, and 5. At the end of the third pass the third largest number is in the third element from the bottom (that is, x4). The array is sorted at the end of pass 4, and no exchanges are made during pass 5. Bubble sort, passes 3,4 and 5

Any Questions