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);