Chapter 6 Arrays in C++ 2nd Semester King Saud University

Slides:



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

Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
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.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
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
Chapter 9: Arrays and Strings
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.
Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
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.
Chapter 8 Arrays and Strings
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
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.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
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.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
INC 161 , CPE 100 Computer Programming
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays Declarations CSCI N305
CSC 113: Computer Programming (Theory = 03, Lab = 01)
Chapter 2 part #3 C++ Input / Output
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
7 Arrays.
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Arrays Kingdom of Saudi Arabia
CS-161 Computer Programming Lecture 14: Arrays I
Introduction To Programming Information Technology , 1’st Semester
7 Arrays.
To refer to an element, specify
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
Arrays Arrays A few types Structures of related data items
Chapter 2 part #3 C++ Input / Output
Arrays.
Lecture 14: Problems with Lots of Similar Data
Programming Fundamental
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Chapter 6 Arrays in C++ 2nd Semester 1433 -1434 King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited By: Mashael almutlaq Noor Alhareqi Chapter 6 Arrays in C++ 2nd Semester 1433 -1434

Array - One Dimensional Array A one dimensional array is a list of related variables have the same data type and the same name . Represented as a group of consecutive memory locations . To refer to a particular location or element in the array, we specify the name of the array and the position number of the particular element in the array.

Defining Arrays size of the arrays . When defining arrays, specify : Array Name . Type of array . size of the arrays .

Defining Arrays The general form : Type array_Name [ size ]; Type declares the base type of the array that determines the data type of each element in the array . Size defines how many elements the array will hold. The Size must be an integer constant greater than zero.

Defining Arrays - Examples- int A[10]; An array of ten integers . Char str[20]; An array of twenty characters . int a[ 100 ], b[ 27 ] ; Defining multiple arrays of same type .

Defining Arrays - Examples- The Size must be an integer constant greater than zero.

Defining and Initializing an Array Define an array temperature of 5 elements contains float numbers , array size 5 : double temperature [5]; Initialize it with these numbers : 12.3 , 7.5 , 65 , 72.1, 87.5 . double temperature [5] = {12.3 , 7.5 , 65 , 72.1, 87.5 } ;

Defining and Initializing an Array double temperature [5] = {12.3 , 7.5 , 65 , 72.1, 87.5 }; 12.3 temperature [0] 7.5 temperature [1] 65.0 temperature [2] Elements 72.1 temperature [3] 87.5 temperature [ 4 ] Index

Initializing Arrays int N[ ] = { 1, 2, 3, 4, 5 }; int N[5] = { 0 } ; If size omitted, the size is determined from the 5 initializers . int N[5] = { 0 } ; int B[20] = {2, 4, 8, 16, 32}; Unspecified elements are guaranteed to be zero . If not enough initializers, rightmost elements become 0 . int C[4] = {2, 4, 8, 16, 32}; Error — compiler detects too many initial values . C++ arrays have no bounds checking . int D[5] = {2*n, 4*n, 8*n, 16*n, 32*n}; Automatically only ; array initialized to expressions .

Accessing Array Elements An individual element within array is accessed by use of a subscript (index) that describes the position of an element in the array , it must be an integer or integer expression . Array named c of size n: c[0],c[1],...,c[ n–1 ] int A[10]; A[0], A[1], …, A[9] In C++ , all array have zero as the index of first element .

Get values into array elements Initialize arrays with the variable declaration . double temperature [ 5 ] = { 12.3 , 7.5 , 65 , 72.1 , 87.5 } ; Use Assignment statements . Read input values into the array from the keyboard.

Accessing Array Elements Array elements are like normal variables : temperature [3] = 12.7 ; cin >> temperature [3] ; or N = 3; temperature [N] = 12.7; temperature [5-2] == temperature [3] == temperature [N]

Assigning Values You cannot assign one array to another : int a[10] , b[10] ; a=b ; //error … illegal Instead, we must assign each value individually . We can use them as values in assignment statements: char str[ ] =“Hello amal”; str[6] = ‘A’; scalar variable - a single data item vs. data structure - a data aggregate

Examples For example, if we assume that variable a is equal to 5 and that variable b is equal to 6, then the following statement adds 2 to array element c[11]. To print the sum of the values contained in the first three elements of array c, we’d write To divide the value of c[6] by 2 and assign the result to the variable x, we would write

Manipulating Arrays For loops are used to: Initializing array elements . Reading elements . Printing elements . Performing operations : Sum elements . Find largest/ smallest element . Search for an element . Sort elements .

Manipulating Arrays Initializing Array Elements : To access all elements of an array a for loop is used. It will start from index 0 to the last element in the array which has an index of array size-1. Define and initialize array a of size 10 with zeros int a[10],index; for(index =0; index <= 9; index++) a[index] = 0;

Manipulating Arrays for (sum = 0, j=0; j < size; j++) sum += a[j]; Reading Elements : for (index =0; index <= size-1; index++ ) { cout << “Enter value : ”; cin >> a[index]; } Printing Elements : for (index =0; index <= size-1; index++) cout << a[index]; Sum Array elements Elements : for (sum = 0, j=0; j < size; j++) sum += a[j];

Examples Note: index can be any integral expression. All of the following are valid: score[0] = 4 ; score[0] += 7 ; score[0] = x + y ; x = y – score[0] ; score[2] = score[1] + 5 * score[0] ; score[ j ] = score[ j + 1] ; Note: index can be any integral expression.

Example –sum of elements

Element Value 0 32 1 27 2 64 3 18 4 95 5 14 6 90 7 70 8 60 9 37

Element Value 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0