0% found this document useful (0 votes)
8 views19 pages

Struct and 2D Array

The document provides an overview of structures in programming, detailing their definition as user-defined data types that can hold heterogeneous data types. It includes examples of declaring structures, accessing their members, and performing operations such as passing structures to functions and returning them. Additionally, it covers typedefs and demonstrates matrix operations using 2-D arrays.

Uploaded by

bhachu3400
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views19 pages

Struct and 2D Array

The document provides an overview of structures in programming, detailing their definition as user-defined data types that can hold heterogeneous data types. It includes examples of declaring structures, accessing their members, and performing operations such as passing structures to functions and returning them. Additionally, it covers typedefs and demonstrates matrix operations using 2-D arrays.

Uploaded by

bhachu3400
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

STRUCTURE

Structure is a user defined, derived data type.


A structure can contain many different data types (int, string, bool, float etc.).
It is collection of variables of different types.
It contains Heterogeneous data that is data of different types.
Each variable in the structure is known as a member of the structure.
To create a structure, use the struct keyword and declare each of its members
inside curly braces.
After the declaration, specify the name of the structure variable.
To access members of a structure, use period operator (dot) with structure variable.

The compiler allocates contiguous/consecutive memory for the data members of


the structure. The size of allocated memory is at least the sum of sizes of all data
members. Structure members are stored sequentially in the order in which they are
declared:

1
2
Declaring Multiple structure variables: structure with 2 variables

#include<iostream>
using namespace std;
struct Student {
string name; // Stores the student's name
int roll; // Stores the roll number
float marks; // Stores the marks
};
int main()
{
Student s1,s2;

cout<<"enter name of student=";


cin>>[Link];
cout<<"enter roll number=";
cin>>[Link];
cout<<"enter marks of student=";
cin>>[Link];
cout<<endl;

cout<<"enter name of student=";


cin>>[Link];
cout<<"enter roll number=";
cin>>[Link];
cout<<"enter marks of student=";
cin>>[Link];
cout<<endl;

cout<<"Name="<<[Link]<<endl;
cout<<"RollNo.="<<[Link]<<endl;
cout<<"Marks="<<[Link]<<endl<<endl;

cout<<"Name="<<[Link]<<endl;
cout<<"RollNo.="<<[Link]<<endl;
cout<<"Marks="<<[Link]<<endl;
return 0;
}

3
Enter structure data from user

4
#include<iostream>
using namespace std;
struct Student {
string name; // Stores the student's name
int roll; // Stores the roll number
float marks; // Stores the marks
};
int main()
{
Student s;

cout<<"enter name of student=";


cin>>[Link];
cout<<"enter roll number=";
cin>>[Link];
cout<<"enter marks of student=";
cin>>[Link];

cout<<"Name="<<[Link]<<endl;
cout<<"RollNo.="<<[Link]<<endl;
cout<<"Marks="<<[Link]<<endl;
return 0;
}

5
Array of structure

#include<iostream>
using namespace std;
struct Student {
string name; // Stores the student's name
int roll; // Stores the roll number
float marks; // Stores the marks
};
int main()
{
struct Student s[3];
int i;

for(i=0;i<3;i++)
{
cout<<"enter name of student=";
cin>>s[i].name;
cout<<"enter roll number=";
cin>>s[i].roll;
cout<<"enter marks of student=";
cin>>s[i].marks;
cout<<endl;
}

for(i=0;i<3;i++)
{
cout<<"Name="<<s[i].name<<endl;
cout<<"RollNo.="<<s[i].roll<<endl;
cout<<"Marks="<<s[i].marks<<endl<<endl;
}
return 0;
}

6
Nested structure

#include<iostream>
using namespace std;
struct employee
{
string name;
int id;
struct doj
{
int day;
string m;
int y;
}d;
};
int main()
{
employee emp;

cout<<"enter name of employee=";


cin>>[Link];
cout<<"enter ID number=";
cin>>[Link];
cout<<"enter day of joining=";
cin>>[Link];
cout<<"enter month of joining=";
cin>>emp.d.m;
cout<<"enter year of joining=";
cin>>emp.d.y;
cout<<endl;

cout<<"Name="<<[Link]<<endl;
cout<<"ID Number.="<<[Link]<<endl;
cout<<"Date of Joining="<<[Link]<<"-"<<emp.d.m<<"-"<<emp.d.y<<endl;

return 0;
}

7
Nested Structure

8
Structure operations:
1 Passing structure to a function
#include<iostream>
using namespace std;
struct emp
{
string name;
int id;

};

int main()
{
emp e; //declaration of structure variable
void disp(emp a); //function prototype

cout<<"enter name of employee=";


cin>>[Link];
cout<<"enter ID number=";
cin>>[Link];
cout<<endl;

disp(e); //function call

return 0;
}

void disp(emp f) //function definition


{
cout<<"Name="<<[Link]<<endl;
cout<<"ID Number.="<<[Link]<<endl;
}

9
2 Function returning structure
#include<iostream>
using namespace std;

struct Student {
string name; // Stores the student's name
int roll; // Stores the roll number
float marks; // Stores the marks
};

int main()
{
Student s; //declaring structurevariable
Student input(); //function prototype/declaration

s=input(); //function call


cout<<"Name="<<[Link]<<endl;
cout<<"RollNo.="<<[Link]<<endl;
cout<<"Marks="<<[Link]<<endl;
return 0;
}

Student input() //function definition


{
Student d;
cout<<"enter name of student=";
cin>>[Link];
cout<<"enter roll number=";
cin>>[Link];
cout<<"enter marks of student=";
cin>>[Link];
return(d);
}

10
Typedef
#include <iostream>
using namespace std;
int main() {
typedef unsigned long int uli; //uli is the new name
uli a=10000000;
cout<<a<<endl;

typedef int integer; //integer is the new name of data type int
integer c=10;
cout<<c;

return 0;
}

#include<iostream>
using namespace std;
typedef struct {
string name;
int roll;

} mystruct;
int main()
{
mystruct s1;

cout<<"enter name of student=";


cin>>[Link];
cout<<"enter roll number=";
cin>>[Link];
cout<<endl;

cout<<"Name="<<[Link]<<endl;
cout<<"RollNo.="<<[Link]<<endl;

return 0;
}

11
Typedef structure

12
Structure typedef

13
Union

Structure Union

14
Subtraction of two matrices in 2-D Array

15
Addition of two matrices in 2-D Array

16
Addition of two matrices in 2-D Array entered by user

17
Multiplication of two matrices in 2-D Array

#include<iostream>
using namespace std;

int main()
{

int a1[3][3]={{1,2,3},
{2,2,2},
{4,1,2}
};

int a2[3][3]={{1,2,3},
{2,1,2},
{3,1,3}
};

int res[3][3], i, j, k, sum=0;

cout<<"1st Matrix: "<<endl;


for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{ cout<<a1[i][j]<<" "; }
cout<<endl;
}

cout<<"2nd Matrix: "<<endl;


for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{ cout<<a2[i][j]<<" "; }
cout<<endl;
}

18
// Multiplying two matrices...
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{ sum=0;
for(k=0; k<3; k++)
sum = sum + (a1[i][k] * a2[k][j]);
res[i][j] = sum;
}
}

cout<<"Multiplication Result:"<<endl;
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cout<<res[i][j]<<"\t";
cout<<endl;
}

return 0;
}

19

You might also like