Array & Matrix Selected topics. 1. It will compare two adjacent elements, if second element is smaller than the first then it will swap them, if we wanted.

Slides:



Advertisements
Similar presentations
Standard Algorithms Find the highest number. ! Your name and today’s date ! Find the maximum Dim numbers(20) As Integer.
Advertisements

Starting Out with C++, 3 rd Edition 1 Chapter 8 – Searching and Sorting Arrays.
Programming Searching Arrays. COMP102 Prog. Fundamentals: Searching Arrays/ Slide 2 Copyright © 2000 by Broks/Cole Publishing Company A division of International.
If Statements & Relational Operators Programming.
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.,
Engineering Problem Solving With C++ An Object Based Approach Chapter 6 One-Dimensional Arrays.
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
Array, Pointer and Reference ( V ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Overview Sort – placing data in an array in some order (usually decreasing or increasing order) Bubble Sort More efficient bubble sort.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Sorting and Searching. Problem Read in a parameter value n, then read in a set of n numbers. Print the numbers in their original order. Sort the numbers.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
Exercise 5.
Programming Sorting Arrays. COMP104 Lecture 25 / Slide 2 Sorting l To arrange a set of items in sequence. l It was estimated that 25~50% of all computing.
1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Arrays.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
CS161 Topic #14 1 Today in CS161 Lecture #14 Practicing! Writing Programs to Practice Write a program that counts the number of vowels in a sentence, ended.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.
Chapter 6 One-Dimensional Arrays ELEC 206 Computer Tools for Electrical Engineering.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
February 4, 2005 Searching and Sorting Arrays. Searching.
Lecture 16: Searching and Sorting Arrays Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 6 Arrays.
STARTING OUT WITH STARTING OUT WITH Class 3 Honors.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
CS 162 Intro to Programming II Bubble Sort 1. Compare adjacent elements. If the first is greater than the second, swap them. Do this for each pair of.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
String as Arrays, Array Sorting C++ Programming Technologies.
Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types.
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.
SORTING Sorting is storage of data in some order, it can be in ascending or descending order. The term Sorting comes along-with the term Searching. There.
Starting Out with C++, 3 rd Edition 1 Sorting Arrays.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
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.
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Engineering Problem Solving with C++, Etter
Sorting Algorithms.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Engineering Problem Solving with C++, Etter
CS1201: Programming Language 2
Arrays Lecture 11.
Example 1 Ask the user to type10 integers of an array T1 and 10 integers of an array T2. Put into T3, an array with 20 integers : the sum of the elements.
CS-161 Computer Programming Lecture 15 & 16: Arrays II
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.
Programming Fundamental
Presentation transcript:

Array & Matrix Selected topics

1. It will compare two adjacent elements, if second element is smaller than the first then it will swap them, if we wanted to sort an array in an ascending order. 2. It will continue the above process of comparing pares, from the first pare to the last pair, at its first iteration the greatest element is placed at the last index of an array. 3. Then it will again repeat the above steps for all the elements by excluding the greatest one. 4. It will continue to repeat until the array becomes sorted. Let’s take a look towards the following example which will illustrate and completely describe the use, working and operations of bubble sort.

// sort array #include using namespace std; main() { int hold; int array[5]; cout<<"Enter 5 numbers: "<<endl; for(int i=0; i<5; i++) { cin>>array[i]; } cout<<endl; cout<<"Orignally entered array by the user is: "<<endl; for(int j=0; j<5; j++) { cout<<array[j]; cout<<endl; } cout<<endl; for(int i=0; i<4; i++) { for(int j=0; j<4; j++) { if(array[j]>array[j+1]) { hold=array[j]; array[j]=array[j+1]; array[j+1]=hold; } } } cout<<"Sorted Array is: "<<endl; for(int i=0; i<5; i++) { cout<<array[i]<<endl; } getch(); }

// getting number in array #include main() { int abb[4]; int r; int initial=0; int final=4; int mid; int location=-5; cout<<"Enter 5 numbers to store in array: "<<endl; for(int i=0; i<5; i++) { cin>>abb[i]; } cout<<endl; cout<<"Enter the number you want to found :"; cin>>r; cout<<endl; while(initial<=final) { mid= (initial+final)/2; if(abb[mid]==r) { location=mid; break; } if(r<abb[mid]) final=mid-1; if(r>abb[mid]) initial=mid+1; } if(location==-5) cout<<" Required number not found "<<endl; else cout<<" Required number is found at index "<<location<<endl; getch(); }

// prime number #include using namespace std; bool prime(int); main() { int a; cout<<"Enter an interger greater then 1: "; cin>>a; if(prime(a)) cout<<a<< " is a prime Number "<< endl; else cout<<a<<" is not a prime Number"; getch(); } bool prime(int z) { for(int i=2; i<z; i++) { if(z%i==0) return false; } return true; }

//Two-Dimensional Integer Array #include main() { int matrix [2][3]; // For taking integer inputs in a matrix // for (int m1=0 ; m1<2 ; m1++) { for (int m2=0 ; m2<3 ; m2++) { cout<<"Enter Integer :"; cin>>matrix [m1][m2]; } } cout<<endl; // For displaying elements of a matrix on a screen // for (int m1=0 ; m1<2 ; m1++) { for (int m2=0 ; m2<3 ; m2++) { cout<<"Your Entered Integer are :"; cout<<matrix [m1][m2]; cout<<endl; } getch(); }

//Removing element from array #include using namespace std; void removeAt(int [], int, int); int main() { const int len = 12; int ylist[len] = {4, 23, 65, 34, 82, 37, 12, 17, 24, 36, 82, 51}; int index = 1; removeAt(ylist, len, index); system("pause"); return 0; } void removeAt(int ylist[], int len, int index) { cout << "Please pick an array index from 0 to " << (len - 1) << ": "; cin >> index; for (int loc = 0; loc < len; loc++) if (ylist[loc] == ylist[index]) { cout << endl << "The array element that you are removing is " << ylist[loc] << endl; }

// counted for (int i = index; i < len; i++) { ylist[index] = ylist[index+1]; ylist[len-1] = 0; } cout << "Final Array Elements are: "; for (int x = 0; x < len; x++) { cout << ylist[x] << " "; } cout << endl; }

// largest number in array #include const int SIZE = 15; main() { // Puts some numbers in the array. int ara[SIZE]={5,2,7,8,36,4,2,86,11,43,22,12,45,6,585}; int high_val, ctr; high_val = ara[0]; // Initializes with first // array element. for (ctr=1; ctr<SIZE; ctr++) { // Stores current value if it is // the higher than the highest. if (ara[ctr] > high_val) { high_val = ara[ctr]; } } cout << "The highest number in the list is " << high_val << "\n"; system("pause"); return 0 ; }

// Finds the highest and the lowest value in the array. #include const int SIZE = 15; main() { int ara[SIZE]; int high_val, low_val, ctr; // Fills array with random numbers from 0 to 99. for (ctr=0; ctr<SIZE; ctr++) { ara[ctr] = rand() % 100; } // Prints the array to the screen. cout << "“Here are the “ << SIZE << “ random numbers:\n"; for (ctr=0; ctr<SIZE; ctr++) { cout << ara[ctr] << "\n"; } cout << "\n\n"; // Prints a blank line. high_val = ara[0]; // Initializes first element to // both high and low. low_val = ara[0]; for (ctr=1; ctr<SIZE; ctr++) { // Stores current value if it is // higher than the highest. if (ara[ctr] > high_val) { high_val = ara[ctr]; } if (ara[ctr] < low_val) { low_val = ara[ctr]; } } cout << "The highest number in the list is " <<high_val << "\n"; cout << "The lowest number in the list is " <<low_val << "\n"; system("pause"); return 0; }

// sort number in array #include using namespace std; main() { int hold; int array[5]; cout<<"Enter 5 numbers: "<<endl; for(int i=0; i<5; i++) { cin>>array[i]; } cout<<endl; cout<<"Orignally entered array by the user is: "<<endl; for(int j=0; j<5; j++) { cout<<array[j]; cout<<endl; } cout<<endl; for(int i=0; i<4; i++) { for(int j=0; j<4; j++) { if(array[j]>array[j+1]) { hold=array[j]; array[j]=array[j+1]; array[j+1]=hold; } cout<<"Sorted Array is: "<<endl; for(int i=0; i<5; i++) { cout<<array[i]<<endl; } getch(); }

#include // Reverse Contents in Array #include void reverse(int [], int); void printarray(int [], int ); int main () { const int SIZE = 10; int arr [SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; cout<<"Before reverse\n"; printarray(arr, SIZE); reverse(arr, SIZE); cout<<"After reverse\n"; printarray(arr, SIZE); getch(); return 0; } void printarray(int arr[], int count) { for(int i = 0; i < count; ++i) cout<<arr[i]<<' '; cout<<'\n'; } void reverse(int arr[], int count) { int temp; for (int i = 0; i < count/2; ++i) { temp = arr[i]; arr[i] = arr[count-i-1]; arr[count-i-1] = temp; }