Consultation Hours. Mubashir: – Tuesday from 12:30 to 1:30 with ease of Students. Zohaib – Wednesday b/w 9:30 -10:30 Location: TA Room (next to HOD Office)

Slides:



Advertisements
Similar presentations
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.
Advertisements

Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT5: Array (1D and 2D) CS2311 Computer Programming.
Maths for Computer Graphics
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Arrays A variable: a named position of memory For example: x1, x2, x3……..x10 An array: a collection of variables of the same type that are referenced by.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
 2007 Pearson Education, Inc. All rights reserved C Arrays.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
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.
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
EENG212 Algorithms and Data Structures
Array.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Matrices Jordi Cortadella Department of Computer Science.
A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.
Chapter 8 Arrays and Strings
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
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.
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.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 3.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
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.
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Introduction to Programming (in C++) Multi-dimensional vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.
CSE 251 Dr. Charles B. Owen Programming in C1 Intro to Arrays Storing List of Data.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Dr. Sajib Datta Feb 11,  Example of declaring and initializing an array. ◦ double someData[3]; /* declare the array someData that will.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Arrays. Arrays are objects that help us organize large amounts of information.
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.
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.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Consultation Hours. Mubashir: – Tuesday from 12:30 to 1:30 with ease of Students. Zohaib – Wedneday b/w 9:30 -10:30 Location: TA Room (next to HOD Office)
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Dr. Sajib Datta Sep 10,  #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b ) ◦ printf("Test1\n");  else.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
Prof. Amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Data Structures I (CPCS-204)
Arrays in C The c language provides a capability that enables the user to design a set of similar data types called array. Pointers and arrays are.
Computer Programming BCT 1113
Array 9/8/2018.
Chapter 5: Arrays: Lists and Tables
Lecture 10 Arrays.
Review of Arrays and Pointers
Data Structures (CS212D) Week # 2: Arrays.
Multidimensional array
Dr Tripty Singh Arrays.
Intro to Arrays Storing List of Data.
Presentation transcript:

Consultation Hours

Mubashir: – Tuesday from 12:30 to 1:30 with ease of Students. Zohaib – Wednesday b/w 9:30 -10:30 Location: TA Room (next to HOD Office)

Arrays

There are situations in which we would want to store more than one value at a time in a single variable. For example, suppose we wish to arrange the percentage marks obtained by 100 students in ascending order. – Construct 100 variables to store percentage marks obtained by 100 different students, i.e. each variable containing one student’s marks. – Construct one variable (called array or subscripted variable) capable of storing or holding all the hundred values.

it would be much easier to handle one variable than handling 100 different variables. Moreover, there are certain logics that cannot be dealt with, without the use of an array.

Array Declaration and Initialization in C int num[6]={2,4,12,5,45,5}; int n[]={2,4,12,5,45,5}; float p[]={1.5,2.5,3}; int k[3]; k[0]=1; k[1]=2; k[2]=3; type variable_name[lengthofarray]; double height[10]; float width[20]; int min[9]; char name[20]; All elements of the array should be of the same type In C Language, arrays starts at position 0.

Array Declaration and Initialization The elements of the array occupy adjacent locations in memory.

9 Arrays - No bounds checking // An incorrect program. Do Not Execute! int main() { int crash[10], i; for(i=0; i<100; i++) crash[i]=i; return 1; }

Linear/Sequential Search int a[]={1,2,5,4,7}; int key=4,n=5,flag=0; for(i=0; i<= n-1; i++){ if(a[i] == key){ flag=1; break; } if(flag == 0) printf("\nThe number is not in the list"); else printf("\nThe number is found");

Finding Min and Max Number // start min and max off as equal to the first number min = nums[0]; max = nums[0]; // iterate through nums int i; for(i = 1; i < nums_length; ++i) { // update max, if necessary if( nums[i] > max ) { max = nums[i]; } // update min, if necessary if(nums[i] < min) { min = nums[i]; }

Bubble Sort

Third Iteration

Bubble Sort for(i=0; i<n-1; i++){ // number of iterations for(j=0; j<(n-i-1); j++){ // number of pair wise comparisons if(array[j]>array[j+1]){ temp = array[j+1]; array[j+1] = array[j]; array[j] = temp; }

Bubble Sort 1. for(i=0; i<n-1; i++){ // number of iterations 2. for(j=0; j<(n-i-1); j++){ // number of pair wise comparisons 3. if(array[j]>array[j+1]){ 4. temp = array[j+1]; 5. array[j+1] = array[j]; 6. array[j] = temp; }

Stepij a[j]> a[j+1]a[0]a[1]a[2]a[3]a[4] T ,5, T T T for(i=0; i<n-1; i++){ 2. for(j=0; j<(n-i-1); j++){ 3. if(array[j]>array[j+1]){ 4. temp = array[j+1]; 5. array[j+1] = array[j]; 6. array[j] = temp; }

1. for(i=0; i<n-1; i++){ 2. for(j=0; j<(n-i-1); j++){ 3. if(array[j]>array[j+1]){ 4. temp = array[j+1]; 5. array[j+1] = array[j]; 6. array[j] = temp; } Stepij a[j]> a[j+1]a[0]a[1]a[2]a[3]a[4] T ,5, T T

1. for(i=0; i<n-1; i++){ 2. for(j=0; j<(n-i-1); j++){ 3. if(array[j]>array[j+1]){ 4. temp = array[j+1]; 5. array[j+1] = array[j]; 6. array[j] = temp; } Stepij a[j]> a[j+1]a[0]a[1]a[2]a[3]a[4] T ,5, T T

Homework Trace Bubble Sort algorithm for the following input – Directly (show comparisons and swaps) – Using tracing table (show variables also) Input: {2,6,12,5,9} Submission – Handwritten – Start of next Class

2-D Array

Initializing two dimensional array

Two Dimensional Array

Matrix Addition int i, j; for (i = 0; i < NRows; i++) { for (j = 0; j < NCols; j++) { C [i][j] = A [i][j] + B [i][j]; }

Appendix

Practice Questions Addition of Matrices Matrix Multiplication Average and Statistics

Matrix Multiplication It is defined between two matrices only if the number of columns of the first matrix is the same as the number of rows of the second matrix. If A is an m-by-n matrix and B is an n-by-p matrix, then their product A×B is an m-by-p matrix given by

Matrix Multiplication

int i = 0; int j = 0; int k = 0; for(i = 0; i < 2; i++) // rows of matrix 1 for( j = 0; j < 4; j++) // columns of matrix 2 for( k = 0; k < 3; k++) // col of m1,row of m2 a3[i][j] += a1[i][k] * a2[k][j];

35 Computing Mean, Median and Mode Using Arrays Mean – Average (sum/number of elements) Median – Number in middle of sorted list – 1, 2, 3, 4, 5 (3 is median) – If even number of elements, take average of middle two Mode – Number that occurs most often – 1, 1, 1, 2, 3, 3, 4, 5 (1 is mode)

Bubble Sort for(x=0; x<n; x++){ // number of iterations for(y=x+1; y<n; y++){ // number of pair wise comparisons if(array[y]>array[y+1]){ temp = array[y+1]; array[y+1] = array[y]; array[y] = temp; }

Matrix Multiplication int i, j, k; int sum; for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { sum = 0; for (k = 0; k < N; k++) { sum += A[i][k] * B[k][j]; } C[i][j] = sum; }