Friday, 28 October 2016
Structure in c++:-
Structure:-
Structure is a collection variables of different data items under a common name .Some can be int, some can be float, some can be char and so on. In this case the data item of structure is called member of that structure.
The difference between array and structure is that array is a collection of same type elements that are referred by a common name but in case of structure element of structure can be of different type.
Declaration of Structure:-
Syntax:-
struct structure name
{
data- type 1 member1;
data- type 1 member2;
data- type 1 member3;
-------------------
-------------------
data-type n member n;
};
example:-
struct student
{
char name[10];
int rollno;
char branch[10];
int sem;
};
Creating structure variable:-
If you need to access the data member of Structure , so you have to declare a structure variable. That can access data member of structure. There are various ways to access these member functions.We can create more then one structure variable.
I) We can create structure variable at the time of declaration. Structure variable(s) can be declared after defining the structure type.
struct structure name
{
data- type 1 member1;
data- type 1 member2;
data- type 1 member3;
-------------------
-------------------
data-type n member n;
} structure variable(s);
Example:-
struct student
{
char name[10];
int rollno;
char branch[10];
int sem;
}var ;
Program:-
II) We can also create a structure variable with in a main function.
Example:-
struct student
{
char name[10];
int rollno;
char branch[10];
int sem;
} ;
main()
{
struct student var;
----
-------
}
Program:-
Accessing Structure Elements:-
To access member of structure we have to use the . (dot) operator.
syntax:-
structure_variable_name.member
- If you need to read a statement from keyboard then we have to write down the following statement:-
cin>>var.rollno;
In which var is a structure variable and rollno is a member of structure as we declare above. Remember the .(dot)operator between structure variable and member of structure.
- If you need to print a statement then write down the following statement :-
cout<<var.rollno;
In which you can see that it will print rollno that you entered before.
Structure is a collection variables of different data items under a common name .Some can be int, some can be float, some can be char and so on. In this case the data item of structure is called member of that structure.
The difference between array and structure is that array is a collection of same type elements that are referred by a common name but in case of structure element of structure can be of different type.
Declaration of Structure:-
Syntax:-
struct structure name
{
data- type 1 member1;
data- type 1 member2;
data- type 1 member3;
-------------------
-------------------
data-type n member n;
};
example:-
struct student
{
char name[10];
int rollno;
char branch[10];
int sem;
};
Creating structure variable:-
If you need to access the data member of Structure , so you have to declare a structure variable. That can access data member of structure. There are various ways to access these member functions.We can create more then one structure variable.
I) We can create structure variable at the time of declaration. Structure variable(s) can be declared after defining the structure type.
struct structure name
{
data- type 1 member1;
data- type 1 member2;
data- type 1 member3;
-------------------
-------------------
data-type n member n;
} structure variable(s);
Example:-
struct student
{
char name[10];
int rollno;
char branch[10];
int sem;
}var ;
Program:-
Write a program in c++ to read and write in structure
II) We can also create a structure variable with in a main function.
Example:-
struct student
{
char name[10];
int rollno;
char branch[10];
int sem;
} ;
main()
{
struct student var;
----
-------
}
Program:-
Write a program in c++ to read and write in structure
Accessing Structure Elements:-
To access member of structure we have to use the . (dot) operator.
syntax:-
structure_variable_name.member
- If you need to read a statement from keyboard then we have to write down the following statement:-
cin>>var.rollno;
In which var is a structure variable and rollno is a member of structure as we declare above. Remember the .(dot)operator between structure variable and member of structure.
- If you need to print a statement then write down the following statement :-
cout<<var.rollno;
In which you can see that it will print rollno that you entered before.
Monday, 18 May 2015
Write a program in c++ which read two matrix and then print a matrix which is addition of these two matrix.
Coding:-
#include<iostream.h>
void main()
{
int a[2][3],b[2][3],c[2][3];
int i,j;
cout<<"enter values in first matrix \n";
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
cin>>a[i][j];
}
cout<<"the matrix is : \n";
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
cout<<" "<<a[i][j];
cout<<"\n";
}
cout<<"enter values in 2nd matrix \n";
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
cin>>b[i][j];
}
cout<<"the matrix is : \n";
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
cout<<" "<<b[i][j];
cout<<"\n";
}
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
c[i][j]=a[i][j]+b[i][j];
cout<<"\n";
}
cout<<"the sum of two matrix is: = \n" ;
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
cout<<" "<<c[i][j];
cout<<"\n";
} }
Output:-
#include<iostream.h>
void main()
{
int a[2][3],b[2][3],c[2][3];
int i,j;
cout<<"enter values in first matrix \n";
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
cin>>a[i][j];
}
cout<<"the matrix is : \n";
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
cout<<" "<<a[i][j];
cout<<"\n";
}
cout<<"enter values in 2nd matrix \n";
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
cin>>b[i][j];
}
cout<<"the matrix is : \n";
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
cout<<" "<<b[i][j];
cout<<"\n";
}
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
c[i][j]=a[i][j]+b[i][j];
cout<<"\n";
}
cout<<"the sum of two matrix is: = \n" ;
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
cout<<" "<<c[i][j];
cout<<"\n";
} }
Output:-
Write a Program in c++ which read 6 no. from keyboard and store these no into array and then calculate sum of these no. using function.
Coding:-
#include<iostream.h>
int sum (int a[])
{
int n=0;
for(int i=0;i<=5;i++)
n=n+a[i];
return (n);
}
int output(int a[])
{
cout<<"the elements of array are : "<< endl ;
for(int i=0;i<=5;i++)
cout<<a[i]<<endl;
}
int main()
{
int a[6],s,i;
cout<<"enter 6 elements : ";
for(i=0;i<=5;i++)
cin>>a[i];
output(a);
cout<<"sum of array element is : ="<<sum(a)<<endl;
}
Output:-
#include<iostream.h>
int sum (int a[])
{
int n=0;
for(int i=0;i<=5;i++)
n=n+a[i];
return (n);
}
int output(int a[])
{
cout<<"the elements of array are : "<< endl ;
for(int i=0;i<=5;i++)
cout<<a[i]<<endl;
}
int main()
{
int a[6],s,i;
cout<<"enter 6 elements : ";
for(i=0;i<=5;i++)
cin>>a[i];
output(a);
cout<<"sum of array element is : ="<<sum(a)<<endl;
}
Output:-
Subscribe to:
Posts (Atom)





