1 / 37

Introduction to Programming

This lecture covers the concepts of matrices, rows, columns, memory, output, two-dimensional arrays, and the need for loops to control a matrix.

wjanis
Télécharger la présentation

Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Matrix Rows Columns

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

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

  5. Column 0 Column 1 Column 2 Column 3 a[ 0 ][ 3 ] a[ 1 ][ 0 ] a[ 2 ][ 3 ] a[ 2 ][ 2 ] a[ 1 ][ 3 ] a[ 0 ][ 0 ] a[ 1 ][ 1 ] a[ 2 ][ 1 ] a[ 0 ][ 2 ] a[ 1 ][ 2 ] a[ 2 ][ 0 ] a[ 0 ][ 1 ] Row 0 Row 1 Row 2 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 Column subscript Array name Row subscript

  6. 1 2 3 4 1 0 3 4 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 } }; Row 0 Row 1

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

  8. 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 ] ; } }

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

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

  11. 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

  12. 2 D Array (Addition) Matrix 1Matrix 2 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

  13. 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];

  14. 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]; } }

  15. 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]; } }

  16. 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"; } }

  17. 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;

  18. 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]; } }

  19. 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; }

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

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

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

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

  24. 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]; } }

  25. 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"; } }

  26. 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]; } }

  27. 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; }

  28. 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

  29. 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];

  30. 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]; } }

  31. 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; }

  32. OutPut Enter The Sales Of District=1 ,Month1=100 Enter The Sales Of District=1 ,Month2=200 Enter The Sales Of District=1 ,Month3=300 Enter The Sales Of District=2 ,Month1=150 Enter The Sales Of District=2 ,Month2=1050 Enter The Sales Of District=2 ,Month3=2000 Enter The Sales Of District=3 ,Month1=111 Enter The Sales Of District=3 ,Month2=222 Enter The Sales Of District=3 ,Month3=333 Enter The Sales Of District=4 ,Month1=444 Enter The Sales Of District=4 ,Month2=555 Enter The Sales Of District=4 ,Month3=666 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

  33. 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.

  34. Agent ProgramOut 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

  35. 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";

  36. 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; }

  37. 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); }

More Related