Upcoming SlideShare
×

# Lab 13

494 views

Published on

Published in: Technology, Business
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

• Be the first to like this

Views
Total views
494
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
12
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Lab 13

1. 1. Lab # 13DE-32 (A) DME
2. 2. ◦ To learn how to declare multidimensional array◦ To learn how to declare 2D dynamic array using pointers◦ To learn the use and syntax of the structures
3. 3.  Arrays can have two or more dimensions. The two dimensional array is called a matrix. It can be declared as int std[no. of rows][no. of columns]; Two loops are used to input values to the array elements. E.g. for(i=0; i<no. of rows; i++) for(j=0; j<no. of columns; j++) { cin>>std[i][j]; }
4. 4.  Like input, two loops are used to display values of the array elements. E.g. for(i=0; i<no. of rows; i++) for(j=0; j<no. of columns; j++) { cout<<std[i][j]; } Two dimensional arrays can be initialized as; int std[3][2]={{1234,56}, {2356, 99}, {1586, 90}}; int std[][2]={{1234,56}, {2356, 99}, {1586, 90}};
5. 5.  Pointers in C++ can also be used to declare a 2D dynamic array in which you can specify the number of rows and columns at the runtime. First you have to declare a double pointer mean a pointer that point to a variable pointer then we allocate memory for the variable pointers of any size we want and at last we will go to each location of the pointers and allocate memory for variables of any size. This can be done as int **m; m = new int* [size]; //array of pointers for ( int i=0; i<size; i++) m[i] = new int [size];
6. 6. #include<iostream>using namespace std;void main(){ int size; cin>>size; int **num; num= new int*[size]; for(int i=0;i<size;i++) num[i]=new int[size]; for(int i=0;i<size;i++) for(int j=0;j<size;j++) cin>>num[i][j]; for(int i=0;i<size;i++) { for(int j=0;j<size;j++) { cout<<num[i][j]<<t; } cout<<endl; }}
7. 7.  A structure is a collection of simple variables that can be of different types and functions. The data items in a structure are called members of a structure. Structures are defined using ‘struct’ keyword followed by the user defined name for the structure e.g. struct name { int a; //data member float b; //data member string c; //data member void function(); //member function }; //definition of structure ends with a semi-colon After the definition of the structure you can declare objects of the structure just like you declare a variable. To access the data and function member of a structure dot operator is used e.g. void main() { name n1; n1.a = 10; n1.b = 2.3; n1.c = “Hello “; //storing value in a data member n1.function(); //calling member function }
8. 8. struct name{ int a; //data member float b; //data member string c; //data member void function() { cout<<"HELLO WORLD"; }//member function}n;struct name{ int a; //data member float b; //data member string c; //data member void function() { cout<<"HELLO WORLD"; }//member function};struct name n;
9. 9. struct name{ int a; //data member float b; //data member string c; //data member void function() { cout<<"HELLO WORLD"; }//member function};struct name n={10,20.0,"Hello"};
10. 10. struct book{string name;float price;int pages;};struct book b[100];void main(){ int i; for(i=0;i<100;i++) { cin>>b[i].name>>b[i].price>>b[i].pages; } for(i=0;i<100;i++) { cout<<b[i].name<<b[i].price<<b[i].pages<<endl; }}