Introduction to Programming

Slides:



Advertisements
Similar presentations
1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays.
Advertisements

 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 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
Arrays.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
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.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
POINTERS.
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
FP201 - PROGRAMMING FUNDAMENTALS Unit Understand the use of array PREPARED BY: MAZNAH AHMAD, JTMK PSIS.
Arrays.
Functions BICSE-6A Mr. Naeem Khalid Lecturer, Dept. of Computing.
Module 1: Array ITEI222 - Advance Programming Language.
C++ Programming Lecture 16 Arrays – Part III The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Arrays float Scores[9]; ? index: element // one dimensional array 1.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
EXAMPLE. Dr. Soha S. Zaghloul2 Write a complete program that searches for all the elements that are multiple of 7 in array X of type int and size 100.
CSC 113: C OMPUTER P ROGRAMMING (T HEORY = 03, L AB = 01) Computer Science Department Bahria University, Islamabad.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
1 Two-Dimensional Arrays. 2 Terminology Two-dimensional arrays represent matrices A matrix contains a number of values of the same data type The values.
Arrays float Scores[9]; ? index: element // one dimensional array 2.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Introduction to Programming
2-D Array.
Introduction to Programming
Computer Programming BCT 1113
Two Dimensional Array Mr. Jacobs.
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
Arrays Arrays exist in almost every computer language.
Dynamic Array Multidimensional Array Matric Operation with Array
Programming Fundamental
Chapter 7: Array.
CS 1430: Programming in C++.
Multi-dimensional Array
Multidimensional Arrays
Multidimensional Arrays Vectors of Vectors
7 Arrays.
Arrays Kingdom of Saudi Arabia
CS-161 Computer Programming Lecture 14: Arrays I
1020: Introduction to Programming Mohamed Shehata November 7, 2016
Arrays Outline Introduction Arrays Declaring Arrays
CISC181 Introduction to Computer Science Dr
C++ Programming Lecture 16 Arrays – Part III
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
4.9 Multiple-Subscripted Arrays
Lecture 12 Oct 16, 02.
Multidimensional array
7 Arrays.
CHAPTER 2 Arrays and Vectors.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays Arrays A few types Structures of related data items
EECE.2160 ECE Application Programming
CHAPTER 2 Arrays and Vectors.
EECE.2160 ECE Application Programming
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
CS-161 Computer Programming Lecture 15 & 16: Arrays II
Dale Roberts, Lecturer IUPUI
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.
Vectors.
4.9 Multiple-Subscripted Arrays
Chapter 6 - Arrays Outline Multiple-Subscripted Arrays.
Presentation transcript:

Introduction to Programming Lecture # 17 saqib.rasheed@mail.au.edu.pk Air University

Matrix Rows Columns

Matrix 1 9 8 7 6 5 4 3 2 1 Row 1 Row 2 Row 3 9 8 7 6 5 4 3 2 1 Row 1 Row 2 Row 3 Memory 3 2 1 6 5 4 9 8 7 Row 1 Row 2 Row 3 Output

x [rowIndex ] [ columnIndex ] Two Dimensional Array Need 2 loop to controll a matrix int x [ i ] [j ] ; x [rowIndex ] [ columnIndex ]

Multiple-Subscripted Arrays Multiple subscripts a[ i ][ j ] Tables with rows and columns Specify row, then column “Array of arrays” a[0][0] is the first element of that array Row 0 Row 1 Row 2 Column 0 Column 1 Column 2 Column 3 a[ 0 ][ 0 ] a[ 1 ][ 0 ] a[ 2 ][ 0 ] a[ 0 ][ 1 ] a[ 1 ][ 1 ] a[ 2 ][ 1 ] a[ 0 ][ 2 ] a[ 1 ][ 2 ] a[ 2 ][ 2 ] a[ 0 ][ 3 ] a[ 1 ][ 3 ] a[ 2 ][ 3 ] Column subscript Array name Row subscript

Multiple-Subscripted Arrays To initialize Initializers grouped by row in braces int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; 1 2 3 4 Row 0 Row 1 1 0 3 4

Multiple-Subscripted Arrays Referenced like normal cout << b[ 0 ][ 1 ]; Outputs 0 Cannot reference using commas cout << b[ 0, 1 ]; Syntax error Function prototypes Must specify sizes of subscripts First subscript not necessary, as with single-scripted arrays void printArray( int [][ 3 ] ); 1 0 3 4

2 D Array E.g int maxRows = 2; int maxCols = 3 ; int matrix [ 2] [ 3 ]; int row , col ; for ( row = 0 ; row < maxRows ; row ++ ) { for ( col = 0 ; col < maxCols ; col ++ ) cout << “Please enter value of ”<< row << “ “ << col; cin >> matrix [ row ] [ col ] ; }

After second outer loop 2 D Array E.g After first outer loop Input [0] 5 2 9 After second outer loop Input [0] 5 2 9 7 4 [1]

Three Dimensional Arrays int x [ ] [ ] [ ] ;

2 D Array (Addition) Write a program in C++ that take two matrices and then Add them After inserting two matrices first display the both matrices and then add them and show the result. i.e

2 D Array (Addition) Matrix 1 Matrix 2 1 2 3 1 2 3 4 5 6 4 5 6 1 2 3 1 2 3 4 5 6 4 5 6 7 8 9 7 8 9 . Result = Matrix 1 + Matrix2 2 4 6 8 10 12 14 16 18

2 D Array (Addition) (code) #include<iostream.h> # define size 3 int main () //Starts Main { int ar1[size][size],ar2[size][size], ar3[size][size];

2 D Array (Addition) (code) cout<<"***Enter First Matrics*** \n"; for(int a=0; a<size; a++) { for(int a1=0; a1<size; a1++) cout<<"First Matrics ElementNo.["<<a<<":"<<a1<<"]="; cin>>ar1[a][a1]; }

2 D Array (Addition) (code) cout<<"***Enter Second Matrics***\n"; for(int b=0; b<size; b++) { for(int b1=0; b1<size; b1++) cout<<"Second Matrics Element No.["<<b<<":"<<b1<<"]="; cin>>ar2[b][b1]; }

2 D Array (Addition) (code) //DISPLAY OF FIRST MATRICS cout<<"\n***You Entered First Matrics***"; for (int a2=0; a2<size; a2++) { cout<<"\n"; for (int a3=0; a3<size; a3++) cout<<ar1[a2][a3]<<"\t"; }

2 D Array (Addition) (code) //DISPLAY OF SECOND MATRICS cout<<"\n***You Entered Second Matrics***"; for (int b2=0; b2<size; b2++) { cout<<"\n"; for (int b3=0; b3<size; b3++) cout<<ar2[b2][b3]<<"\t"; } cout<<endl;

2 D Array (Addition) (code) //Adding two Matrices for (int d=0; d<size; d++) { for (int d1=0; d1<size; d1++) ar3[d][d1] = ar1[d][d1] + ar2[d][d1]; }

2 D Array (Addition) (code) //RESULT OUTPUT cout<<"\nThe Result Of Two Matrices is as Follow\n"; for(int c=0; c<size; c++) { cout<<"\n"; for (int c1=0; c1<size; c1++) cout<<ar3[c][c1]<<"\t"; } } cout<<endl;return 0; }

Enter the values in a matrix and print it in reverse Column order More Exercise Enter the values in a matrix and print it in reverse Column order 9 8 7 6 5 4 3 2 1 [0] [1] [2] 7 8 9 4 5 6 1 2 3 [2] [1] [0]

Transpose E.g 1 2 3 4 5 6 7 8 9

Transpose E.g Write a Program in C++ that Display the Transpose of Matrix. Matrix 1 Transpose 1 2 3 1 4 7 4 5 6 2 5 8 7 8 9 3 6 9

Transpose E.g (code) #include<iostream.h> # define size 3 int main () // Starts Main { int ar2[size][size],ar2t[size][size];

Transpose E.g (code) cout<<"***Enter The Matrics***\n"; for(int b=0; b<size; b++) { for(int b1=0; b1<size; b1++) cout<<" Matrics Element No.["<<b<<":"<<b1<<"]="; cin>>ar2[b][b1]; }

Transpose E.g (code) //DISPLAY OF MATRICS cout<<"\n***You Entered Matrics***"; for (int b2=0; b2<size; b2++) { cout<<"\n"; for (int b3=0; b3<size; b3++) cout<<ar2[b2][b3]<<"\t"; }

Transpose E.g (code) //TRANSPOSE OF MATRICS cout<<"\n***The Transpose Of Matrics Is as Follow***\n"; for(int x=0; x<size; x++) { for (int x1=0; x1<size; x1++) ar2t[x][x1] = ar2[x1][x]; }

Transpose E.g (code) // DISPLAY OF TRANSPOSE OF MATRICS for( int y=0; y<size; y++) { cout<<"\n"; for(int y1=0; y1<size; y1++) cout<<ar2t[y][y1]<<"\t"; } cout<<endl; return 0;

2 D Array (Sale) A Distributor of a Pharmaceutical Company has 4 Districts, for supply the medicine. He requires a program that can display the sales of all his Districts. Write a Program in C++ Using two Dimensional Array that shows the Following Out put. The Program should display the Sale, Districts wise and up to 3 Months i.e Month1 Month2 Month3 Sale of Districts # 1 = 599 865 900 Sale of Districts # 2 = 400 200 365 Sale of Districts # 3 = 900 950 899 Sale of Districts # 4 = 250 320 340

2 D Array (Sale) (code) #include<iostream.h> int main() { const int districts=4; const int months=3; int d,m; double sales[districts][months];

2 D Array (Sale) (code) //Taking Sale of three Months District Wise for(d=0; d<districts; d++) { for(m=0; m<months; m++) cout<<"Enter The Sales Of District="<<d+1<<" "; cout<<",Month"<<m+1<<"="; cin>>sales[d][m]; }

2 D Array (Sale) (code) //Displaying the Sale District Wise cout<<"\n\n\n"; cout<<"\n\nThe Sale Of Districts Is \n"; for(d=0; d<districts; d++) { cout<<"\n District # "<<d+1<<" sales :"; for(m=0; m<months; m++) cout<<sales[d][m]<<"\t"; } cout<<endl; return 0;

OutPut Enter The Sales Of District=1 ,Month1=100 The Sale Of Districts Is District # 1 sales : 100 200 300 District # 2 sales : 150 1050 2000 District # 3 sales : 111 222 333 District # 4 sales : 444 555 666 Press any key to continue

Agent Program Write a program in C++, which take Agent code (123,258,..) and traveling expense (Rs = 5000, 6000,…) of the agent. Find the agent who had spent most money in all, Display the agent code and amount after searching in 2 D Array.

Agent Program Out put Enter 3 digit agent number = 123 then enter traveling expenses Rs=5000 Enter 0 0 to quit Enter agent number & Expense =123 5000 Enter agent number & Expense =258 6587 Enter agent number & Expense =963 65478 Enter agent number & Expense =701 10000 Enter agent number & Expense =0 0 Agents with higest Expense : Agent Code = 963 Amount = 65478 Press any key to continue

Agent Program(CODE) #include<iostream.h> #define ROWS 10 #define COLUMNS 2 int maxex (int [][COLUMNS], int ); void main () { int agents[ROWS][COLUMNS]; int number,expenses; int index = 0; cout<<"\nEnter 3 digit agent number = 123\n" <<"then enter traveling expenses Rs=5000 \nEnter 0 0 to quit\n";

Agent Program(CODE) do { cout<<"\nEnter agent number & Expense ="; cin>>number>>expenses; agents[index][0] = number; agents[index][1] = expenses; } while(agents[index++][0] !=0.0); index--; index= maxex(agents,index); cout<<"\nAgents with higest Expense :\nAgent Code = "<<agents[index][0]; cout<<"\nAmount = "<<agents[index][1]<<endl;

Agent Program(CODE) int maxex (int list[][COLUMNS], int size) { int dex,maxdex; int max; max = list[0][1]; maxdex = 0; for(dex=1; dex<size; dex++) if(max < list[dex][1]) max = list[dex][1]; maxdex = dex; } return (maxdex);