SlideShare a Scribd company logo
1 of 11
Lab # 13
DE-32 (A)
    DME
◦ 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
   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];
            }
   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}};
   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];
#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;
    }
}
   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
    }
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;
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"};
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;
   }

}

More Related Content

What's hot

Array in c language
Array in c languageArray in c language
Array in c language
home
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
Deepak Singh
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
웅식 전
 
Dynamics allocation
Dynamics allocationDynamics allocation
Dynamics allocation
Kumar
 

What's hot (20)

Litebox
LiteboxLitebox
Litebox
 
Ooprc4 b
Ooprc4 bOoprc4 b
Ooprc4 b
 
Program: Inheritance in Class - to find topper out of 10 students
Program: Inheritance in Class - to find topper out of 10 studentsProgram: Inheritance in Class - to find topper out of 10 students
Program: Inheritance in Class - to find topper out of 10 students
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Machine Learning Game Changer for IT - Maartens Lourens
Machine Learning Game Changer for IT - Maartens LourensMachine Learning Game Changer for IT - Maartens Lourens
Machine Learning Game Changer for IT - Maartens Lourens
 
Hash tables
Hash tablesHash tables
Hash tables
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
Array in c language
Array in c languageArray in c language
Array in c language
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
 
2013 11 CSharp Tutorial Struct and Class
2013 11 CSharp Tutorial Struct and Class2013 11 CSharp Tutorial Struct and Class
2013 11 CSharp Tutorial Struct and Class
 
Array in c language
Array in c language Array in c language
Array in c language
 
2D arrays
2D arrays2D arrays
2D arrays
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
COW
COWCOW
COW
 
Array BPK 2
Array BPK 2Array BPK 2
Array BPK 2
 
Structure in c sharp
Structure in c sharpStructure in c sharp
Structure in c sharp
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
 
Dynamics allocation
Dynamics allocationDynamics allocation
Dynamics allocation
 
Funcd
FuncdFuncd
Funcd
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 

Similar to Lab 13

Cs1123 12 structures
Cs1123 12 structuresCs1123 12 structures
Cs1123 12 structures
TAlha MAlik
 
Oop lec 3(structures)
Oop lec 3(structures)Oop lec 3(structures)
Oop lec 3(structures)
Asfand Hassan
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
kavitham66441
 
C++ Course - Lesson 3
C++ Course - Lesson 3C++ Course - Lesson 3
C++ Course - Lesson 3
Mohamed Ahmed
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
GkhanGirgin3
 

Similar to Lab 13 (20)

C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
12Structures.pptx
12Structures.pptx12Structures.pptx
12Structures.pptx
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
 
Cs1123 12 structures
Cs1123 12 structuresCs1123 12 structures
Cs1123 12 structures
 
The STL
The STLThe STL
The STL
 
OOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxOOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptx
 
Oop lec 3(structures)
Oop lec 3(structures)Oop lec 3(structures)
Oop lec 3(structures)
 
CHAPTER -4-class and structure.pptx
CHAPTER -4-class and structure.pptxCHAPTER -4-class and structure.pptx
CHAPTER -4-class and structure.pptx
 
Computer Programming -II (Lec. 10).pptx
Computer Programming -II (Lec. 10).pptxComputer Programming -II (Lec. 10).pptx
Computer Programming -II (Lec. 10).pptx
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
Lecture 9_Classes.pptx
Lecture 9_Classes.pptxLecture 9_Classes.pptx
Lecture 9_Classes.pptx
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
C++ Course - Lesson 3
C++ Course - Lesson 3C++ Course - Lesson 3
C++ Course - Lesson 3
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structure
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
Introduction to c part 2
Introduction to c   part  2Introduction to c   part  2
Introduction to c part 2
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 

Recently uploaded

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Recently uploaded (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

Lab 13

  • 1. Lab # 13 DE-32 (A) DME
  • 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. 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. 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. 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.
  • 7. #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; } }
  • 8. 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 }
  • 9. 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;
  • 10. 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"};
  • 11. 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; } }