Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming

Similar presentations


Presentation on theme: "Introduction to Programming"— Presentation transcript:

1 Introduction to Programming
Lecture # 17 Air University

2 Matrix Rows Columns

3 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

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

5 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

6 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

7 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

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 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]

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 1 Matrix 2 1 2 3 1 2 3 4 5 6 4 5 6
Result = Matrix 1 + Matrix2 2 4 6

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 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]

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 1 Transpose

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 = Sale of Districts # 2 = Sale of Districts # 3 = Sale of Districts # 4 =

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
The Sale Of Districts Is District # 1 sales : District # 2 sales : District # 3 sales : District # 4 sales : 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 Program Out put Enter 3 digit agent number = 123
then enter traveling expenses Rs=5000 Enter 0 0 to quit Enter agent number & Expense = Enter agent number & Expense = Enter agent number & Expense = Enter agent number & Expense = 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);


Download ppt "Introduction to Programming"

Similar presentations


Ads by Google