Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
1 CIS 205 Practice Test George Lamperti A word that has a predefined meaning in a C++ program and cannot be used as a variable name is known as.
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.
Simple Arrays COMP104 Lecture 11 / Slide 2 Arrays * An array is a collection of data elements that are of the same type (e.g., a collection of integers,characters,
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 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.
Multiple-Subscripted Array
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
CSE202: Lecture 14The Ohio State University1 Arrays.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Simple Arrays Programming COMP104 Lecture 12 / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g., a collection.
Arrays.
Basic Elements of C++ Chapter 2.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Chapter 8 Arrays and Strings
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Computer Programming Lecture 8 Arrays. 2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
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.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
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.
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?
Objectives You should be able to describe: One-Dimensional 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.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++.
Programming fundamentals 2 Chapter 1:Array
C++ Arrays.
Basic Elements of C++ Chapter 2.
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Data type List Definition:
Programming Funamental slides
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
Arrays of Two-Dimensions
Array Data Structure Chapter 6
Arrays Arrays A few types Structures of related data items
Dr. Khizar Hayat Associate Prof. of Computer Science
Presentation transcript:

Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays

Computer Skills2 / Scientific Colleges 2 Arrays Data Types Data types are of two kinds: - simple data types (e.g. int, float, double, char) - Structured data type: (e.g. arrays) An array is a collection of two or more adjacent memory cells, called array elements, that are associated with a particular symbolic name. Arrays are of two kinds: - Arrays of one-dimension - Arrays of two-dimension

Computer Skills2 / Scientific Colleges 3 One-Dimensional Arrays Declaration of one-dimension array Syntax: atype aname [ size ] ; // uninitialized array atype aname [ size ] = { initialization list } ; where atype is any data type; aname is the name given to the array; size represents the number of elements in the array. initialization list is the list of initial values given to the array.

Computer Skills2 / Scientific Colleges 4 One-Dimensional Arrays Examples EX1: int x [ 3 ]; - This tells the compiler to associate 3 memory cells with name x. - These cells will be adjacent to each other in memory. - Each element of array x contains a value of integer type EX2: int Y [ 4 ] = { 2, 4, 6, 8 } ; This initializes array Y to have 4 elements which contain 2, 4, 6, 8.

Computer Skills2 / Scientific Colleges Accessing One-Dimensional Array int x [3] ; Array x in memory: How to process the data stored in an array? Syntax: aname [ index ] - index is the subscript that is used to reference the desired element. The array indexing starts from 0 until the fixed size -1.

Computer Skills2 / Scientific Colleges 6 Accessing One-Dimensional Array Array x in memory: Index Values Accessing the array: x [0] to access the first element of x x [1] to access the second element of x x [2] to access the third element of x

Computer Skills2 / Scientific Colleges 7 Examples on One-Dimensional Arrays Example 1: Write a C++ program that stores the first 5 integers that are multiples of 5 into array A and reads data into array B;computes the sum of the two arrays and stores the result in array C. # include void main ( ) { int A [5] ; //declaring array A of 5 integers int B [5], C [5]; //declaring arrays B and C of 5 integers for ( int i = 0; i <5 ; i++ ) { A[ i ] = ( i + 1 ) * 5; cout << “enter new element in array B”; cin >> B[ i ] ; C [ i ] = A[ i ] + B [ i ] ; cout << C [ i ] << “ “ ; } }

Computer Skills2 / Scientific Colleges 8 Example1 (Cont.) The trace of the previous example shows the arrays as follows if B elements are : A B C

Computer Skills2 / Scientific Colleges 9 Example 2 Example 2: Write a C++ program to read 10 integers and store them in array A. Then it finds the even numbers to store them in array B and the odd numbers to store them in array C.

Computer Skills2 / Scientific Colleges 10 Example 2 (Cont.) // File arrays.cpp #include void main ( ) { int i; int A [10], B[10], C[10] ; cout << “ Enter 10 integers : “ ; for (i = 0 ; i <10; i++) { cin >> A[i] ; if (A[i] % 2 == 0) B[i] = A[i] ; else C[i] = A[i]; } cout << “B element = “ << “ C element = “ << endl; for (i = 0; i < 10; i++) { cout << B[i] << “ “ << C[i] << endl ; }

Computer Skills2 / Scientific Colleges 11 Examples on One-Dimensional Arrays Example 3: The resultant arrays B and C in example 2 contain data stored in non consecutive locations in the arrays. Modify the program of example 2 so that the data are stored in consecutive locations.

Computer Skills2 / Scientific Colleges 12 Example 3 (Cont.) / File arrays.cpp #include void main ( ) { int i, j = 0, k = 0 ; int A [10], B[10], C[10] ; for ( i= 0 ; i <10; i++) { cin >> A[i] ; if (A[i] % 2 == 0) { B [ j ] = A [ i ] ; j ++ ; } else { C [k] = A [ i ]; k ++ ; } } cout << “B element = “ << “ C element = “ << endl ; for (i=0; i<10; i++) { cout << “B[i] << “ “ << “ “ << C[i] << endl ; } }

Computer Skills2 / Scientific Colleges 13 Example 4 The problem: Write C++ program that searches for an integer in array of 10 integers. A proper message should be printed out if the number is found or not. The Analysis: A given array of integer numbers is going to be searched in order to find a given number. Requirements: Input: an integer number n, an array A of 10 integers Output: a message “yes found” or “no not found” according to weather the number is found or not.

Computer Skills2 / Scientific Colleges 14 The C++ Program for Example 4 //File search.cpp #include void main ( ) { int n, i ; int A [ 10 ] ; bool flag = false; cout << “ Enter the number that you ant to search for: “ ; cin >> n ; cout << “ Enter 10 integers: “ ; for ( i = 0 ; i < 10 ; i++ ) { cin >> A[i] ; if ( A[i] == n ) { flag = true ; break ; } } if ( flag == true ) cout << “ Yes, the number is found \n” ; else cout << “ No, the number is not found \n” ; }

Computer Skills2 / Scientific Colleges 15 Arrays of Two-Dimensions Syntax ( in C++ ): atype aname [nrows] [ncolumns] ; Example: int B [2] [3] ; // declaring array B of 2 rows and 3 columns Array in diagram: Reading array of two-dimensions: int i, j ; for ( i = 0 ; i < 2 ; i++ ) for ( j = 0 ; j < 3 ; j++ ) cin >> B[ i ] [ j ] ;

Computer Skills2 / Scientific Colleges 16 Example1 of Two-Dimensional Array Write a C++ program that reads array A of size (2 x 3) and finds the sum of the elements in each column. //File arrays2d.cpp #include void main ( ) { int i, j, clmsum = 0 ; int B[2][3]; cout << "Enter 6 array elements: " ; for ( i = 0 ; i < 2 ; i++ ) for ( j = 0 ; j < 3 ; j++) cin >> B[i][j] ; // Process the array now for ( j = 0 ; j < 3 ; j++) { for ( i = 0 ; i < 2 ; i++ ) clmsum = clmsum + B[i][j] ; cout << " sum of column no. " << j << " is " << clmsum<<endl; clmsum = 0; }}

Computer Skills2 / Scientific Colleges 17 Example2 of Two-Dimensional Array Write a C++ program that reads an array of size (3 x 3) and finds the product of the diagonal elements. //File diagonal.cpp #include void main ( ) { int i, j, product = 1 ; int B[3][3]; cout << "Enter the 9 array elements: " ; for ( i = 0 ; i <3 ; i++ ) for ( j = 0 ; j < 3 ; j++) cin >> B[i][j] ; // Process the array now for ( i = 0 ; i < 3 ; i++) for ( j = 0 ; j < 3 ; j++ ) if ( i == j ) product = product * B[i][j] ; cout << " The product of the diagonal elements = " << product << endl; }