Programming fundamentals 2 Chapter 1:Array

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.,
How Create a C++ Program. #include using namespace std; void main() { cout
Arrays.
Basic Elements of C++ Chapter 2.
Programming is instructing a computer to perform a task for you with the help of a programming language.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
 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.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
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.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
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.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Bill Tucker Austin Community College COSC 1315
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Bill Tucker Austin Community College COSC 1315
The Ohio State University
Arrays Arrays exist in almost every computer language.
Arrays Declarations CSCI N305
Basic Elements of C++.
CSC 113: Computer Programming (Theory = 03, Lab = 01)
Introduction to C++ October 2, 2017.
Multi-dimensional Array
C++ Arrays.
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Programming Funamental slides
Data type List Definition:
CS150 Introduction to Computer Science 1
Programming Funamental slides
Lecture 12 Oct 16, 02.
Summary Two basic concepts: variables and assignments Basic types:
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
Engineering Problem Solving with C++, Etter
Arrays Lecture 11.
Arrays of Two-Dimensions
CS150 Introduction to Computer Science 1
Chapter 9: Data Structures: Arrays
Engineering Problem Solving with C++ An Object Based Approach
CHAPTER 2 Arrays and Vectors.
Array Data Structure Chapter 6
CS150 Introduction to Computer Science 1
Arrays Arrays A few types Structures of related data items
Fundamental Programming
CHAPTER 2 Arrays and Vectors.
CS150 Introduction to Computer Science 1
COMS 261 Computer Science I
CS31 Discussion 1D Winter19: week 4
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.
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
Dr. Khizar Hayat Associate Prof. of Computer Science
Presentation transcript:

Programming fundamentals 2 Chapter 1:Array Miss: Hanan Hardan

Data type List Definition: The data type List (or set or array) allows:  To group together a limited number of elements. These elements have the same type (integer, real, character, string,). To fix the possible operations on these elements (input, output, sum, search, min, max…)  

List general processing model The elements of a list, being of the same type, undergo, generally, the same processing. We start by processing the first element, the second, the third, and so on, up to the last. The general model of this processing is as it follows:

Algorithm General-array-Processing Begin / / Data definition: declarations … / / Data Initialization Obtain the first element to process; While (the last element to process is not yet processed) do / / There is at least one element to process Process the element / / Current element processing Obtain the next element to process / / next element obtaining End End General-array-Processing;

Data definition(declarations) The definition of the elements to be processed differs from a problem to other. These elements may be elementary (integer, character, …) or complex (word, sequence, sentence, paragraph,..). array array_name [array size] of type_name EX: array L [5] of type integer array word [10] of type character array sentence[4] of type string

Data Initialization EX1: array L [5] of type integer L {2,4,7,8,9} array word [6] of type character word {‘A’,’t’,’t’,’e’,’n’,’d’} EX3: array sentence[4] of type string Sentence {“The” ,”book” ,”is” ,”good”}

Processing order array word [6] of type character word {‘A’,’t’,’t’,’e’,’n’,’d’} 1 2 3 4 5 6 processing order (index) Word[1] = A Word[2] = t Word[3] = t Word[4] = e Word[5] = n Word[6] = d

Array Operations   Several operations are allowed on the arry: ReadArray, WriteArray, SearchArray, SumArray, MinArray, MaxArray, SortArray, etc..

Example: WriteArray Algorithm Print-characters Begin array Word [6] of type character //Data definition: declarations word {‘A’,’t’,’t’,’e’,’n’,’d’} //Data Initialization CurChar  1 //Obtain the first Char to process //the last character to process is not yet processed While (CurChar  6) do Output Word [CurChar] //Current character processing CurChar  CurChar + 1 //next character obtaining End while End Print-characters

Example: ReadArray Algorithm ReadListOfIntegers; Begin Const ListSize = 10 Array L [ListSize] of integer CurElem is Integer CurElem  1 While (CurElem  ListSize ) do Input L[CurElem] CurElem  CurElem + 1; End While End ReadListOfIntegers;

Arrays in C++ 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

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.

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.

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. E.g. A [ 2 ] // refers to second element of A. A [ 2 ] is called a subscripted variable. 24 20 10

Accessing One-Dimensional Array Array x in memory: Index 0 1 2 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 24 20 10

Example: WriteListOfIntegers #include <iostream> using namespace std; void main () { int L [5]={1,2,3,4,5}; int CurElem=0; while (CurElem < 5 ) cout<<L[CurElem] ; CurElem = CurElem + 1; }

Example: ReadListOfIntegers #include <iostream> using namespace std; void main () { const int ListSize = 10; int L [ListSize]; int CurElem=0; cout<<"please enter 10 integer number"; while (CurElem < ListSize ) cin >> L[CurElem] ; CurElem = CurElem + 1; }

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 <iostream.h> void main ( ) { int A [5] ; //declaring array A of 10 integers int B [5] , C [5]; //declaring arrays B and C of 10 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 ] << “ “ ; } }

Example1..Cont. The trace of the previous example shows the arrays as follows if B elements are 7 6 10 13 23: 0 1 2 3 4 A 0 1 2 3 4 B C 5 10 15 20 25 7 6 10 13 23 12 16 25 33 48

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.

Example 2 .. Cont. #include <iostream.h> 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 ;

Examples on One-Dimensional Arrays 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.

#include <iostream.h> 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 = “ ; for (i=0; i<j; i++) cout << B[i] <<“ “ ; cout<<endl; cout << “C element = “ ; for (i=0; i<k; i++) cout << C[i] << “ “ ; }

Example 4 The problem: Write an algorithm then a C++ program that searches for an integer in array of 10 integers. A proper message should be printed out. 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.

Algorithm Search Begin Array L [10] of integer Integer CurElem, SearchVal Boolean Found // ------------------------ Reading L ------------------------------ CurElem  1 While (CurElem  10 ) do Input L[CurElem] CurElem  CurElem + 1; End While //------------------------------ End Reading L ------------------

Input SearchVal Found  False; CurElem  1; While (CurElem  10 and not Found ) do If (L[CurElem] = SearchVal) then Found = True Else CurElem  CurElem + 1 End If End While IF ( Found = true) then Output “ Yes, the number is found “ Output “ No, the number is not found “ End Search

The C++ Program #include <iostream.h> void main ( ) { int L [10] ; int CurElem, SearchVal; bool Found; // ------------------------ Reading L ------------------------------ cout<<"Enter 10 integers:"; CurElem = 0 ; while (CurElem < 10 ) cin>> L[CurElem] ; CurElem = CurElem + 1; } //------------------------------ End Reading L ------------------

cout<< "Enter the number that you ant to search for: "; cin>>SearchVal; Found = false; CurElem = 0; while (CurElem < 10 && !Found ) { if (L[CurElem] == SearchVal) Found = true; else CurElem =CurElem + 1; } if ( Found == true) cout<<" Yes, the number is found "; cout<<" No, the number is not found" ;