Articles by "Structure"
Showing posts with label Structure. Show all posts
no image
A phone number, such as (212) 767-8900, can be thought of as having three parts: the area code (212), the exchange (767) and the number (8900). Write a program that uses a structure to store these three parts of a phone number separately. Call the structure phone. Create two structure variables of type phone. Initialize one, and have the user input a number for the other one. Then display both numbers. The interchange might look like this: 
Enter your area code, exchange, and number: 415 555 1212 
My number is (212) 767-8900 
Your number is (415) 555-1212




Implementation of the above problem:
#include<iostream.h>
#include<conio.h>
struct phone
{
char area[10];
char exchange[10];
char number[10];
};
int main()
{
phone ph1={"212","767","8900"};
phone ph2;
clrscr();
cout<<"\nenter your area code,exchange and number:";



cin>>ph2.area>>ph2.exchange>>ph2.number;
cout<<"\nmy number is ("<<ph1.area<<")"<<ph1.exchange<<"-"<<ph1.number;
cout<<"\nyour number is ("<<ph2.area<<")"<<ph2.exchange<<"-"<<ph2.number;
getch();
return 0;
}


Thanks
Mukesh Rajput
no image
A point on the two dimensional plane can be represented by two numbers: an X coordinate and a Y coordinate. For example, (4,5) represents a point 4 units to the right of the origin along the X axis and 5 units up the Y axis. The sum of two points can be defined as a new point whose X coordinate is the sum of the X coordinates of the point and whose Y coordinate is the sum of their Y coordinates. Write a program that uses a structure called point to model a point. Define three points, and have the user input values to two of them. Than set the third point equal to the sum of the other two, and display the value of the new point. Interaction with the program might look like this:
Enter coordinates for P1: 3 4
Enter coordinates for P2: 5 7
Coordinates of P1 + P2 are : 8, 11




Implementation of the above problem:
#include<iostream.h>
#include<conio.h>
#define N 2
struct point
{
int x;
int y;
}
p[N],pt={0,0};
int main()
{
int i;
clrscr();
for(i=0;i<=N-1;i++)
{
cout<<"enter coordinates x"<<i+1<<" & y"<<i+1<<":";
cin>>p[i].x>>p[i].y;
}
for(i=0;i<=N-1;i++)
{
pt.x=pt.x+p[i].x;
pt.y=pt.y+p[i].y;
}
cout<<"sum of "<<N<<" points is:"<<pt.x<<","<<pt.y;



getch();
return 0;
}


Thanks
Mukesh Rajput
no image
Create a Union called student with the following details as variables within it.
1. Name of the student
2. Age
3. Year of study
4. Semester
5. Five different subject marks in array




Write a C++ program to create an object for the union to access these and print the Name, age,
year, semester and grade according to their percentage of marks scored.
90 % and above – S grade
80% to 89% -- A grade
70% to 79% -- B grade
60% to 69% -- C grade
50% to 59% -- D grade
<50% -- F grade


Implementation of the above problem:
#include <iostream.h>
union Student
{
char name[25],grade;
int age,year,semester;
float m[5];
}s1;
int main()
{
int i,j;
int total=0,average;
char grade;
cout<<"\nEnter the student's name : ";
cin>>s1.name;
cout<<"\nName : "<<s1.name;
cout<<"\n----------------------\n";
cout<<"\nEnter the age";
cin>>s1.age;
cout<<"\nAge : "<<s1.age;
cout<<"\n----------------------\n";
cout<<"\nEnter the year";
cin>>s1.year;
cout<<"\nYear : "<<s1.year;
cout<<"\n----------------------\n";
cout<<"Enter semester ";
cin>>s1.semester;
cout<<"\nSemester : "<<s1.semester;
cout<<"\n----------------------\n";
cout<<"Enter the five different marks for the student :";



for(i=0;i<5;i++)
{
cin>>s1.m[i];
if(s1.m[i]>=50)
total=total+s1.m[i];
else
j++;
}
average=total/5;
if(average>=90)
{ grade='S';}
else if(average>=80 && average<90)
{ grade='A';}
else if(average>=70 && average<80)
{ grade='B';}
else if(average>=60 && average<70)
{ grade='C';}
else if(average>=50 && average<60)
{ grade='D';}
else if(j>=1)
{grade='F';}
cout<<"\nGrade : "<<grade<<"\n";
cout<<"Size of the union is :"<<sizeof(s1);



return 0;
}


Thanks
Mukesh Rajput
no image
Create a Structure called employee with the following details as variables within it.
1. Name of the employee
2. Age
3. Designation
4. Salary
Write a C++ program to create array of objects for the structure to access these and print the name, age, designation and salary.




Implementation of the above problem:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct bio
{
char name[20];
int age;
float sal;
char designation[20];
};
void main()
{
struct bio info[2];
for(int i=0;i<2;i++)
{
cout<<"Enter the data : name, age, salary, designation"<<endl;
cin>>info[i].name>>info[i].age>>info[i].sal>>info[i].designation;
}
for(i=0;i<2;i++)
{
cout<<"Given Data for obj["<<i<<"]:- "<<endl;
cout<<"+++++++++++++++++++++++++++\n";
cout<<"Name : "<<info[i].name<<endl;
cout<<"Age : "<<info[i].age<<endl;
cout<<"Salary : "<<info[i].sal<<endl;
cout<<"Address : "<<info[i].designation<<endl;
}
cout<<"Size of the structure Student is :"<<sizeof(info[0]);



getch();
}


Thanks
Mukesh Rajput
no image
Explain Structures with the help of an example.

A structure is a group of data of different data types. A structure must be defined before it is declared. A structure template is created during structure definition. Once the definition is given, structure may be declared. Memory is allotted to a structure only after declaration.


Syntax for structure definition :
struct structure_name
{
datatype member_1;
datatype member_2;
………
datatype member_n;
};

Example for structure definition:
Struct student
{
int roll_no;
int age;
char name[20];
};

Syntax for structure declaration:
struct <structure_name>    v1, v2, v3,…….., vn;
Example : 
struct student    s1, s2, s3;


To access the members of a structure .(dot) operator is used.
Example:
scanf(“%d%d%s”, &s1.roll_no, s1.age, s1.name);
scanf(“%d%d%s”, &s2.roll_no, s2.age, s2.name);
scanf(“%d%d%s”, &s3.roll_no, s3.age, s3.name);


Thanks
Mukesh Rajput
no image
How Structures are initialized? 

A structure can be initialized at design time as follows:
Example :
struct student
{
int roll_no;

int age;
char name[20];
};
struct student stu = {1,17,"Mukesh"};

What are nested structures?
A structure built within another structure is called as nested structure.
Example : 
struct dob
{
int day;
int month;
int year;
};
struct student
{
int roll_no;
char name[20];
struct dob date; // structure with in a structure
}stu;

What is meant by an array of structures?



An array of structures contains data elements of every structure variable stored in to an array.
Example : 
struct student
{
int reg no;
int age;
char name[20];
}stu[10];


Thanks
Mukesh Rajput
no image
Introduction to STRUCTURE in C++

A structure is a collection of variables under a single name. Variables can be of any type like int, float, char etc. The main difference between structures and array is that arrays are collections of the same data types and structure is a collection of variables of different data type under a single name or they store heterogeneous data in it. 
A structure declaration forms a template that may be used to create structure objects. The variables that make up the structure are called members. All of the members of the structure are logically related.

Definition of STRUCTURE: A structure is a user-defined datatype which is created by a collection of heterogeneous elements represented by the same name.

Declaration of STRUCTURE:
The structure is declared by using the keyword struct followed by the structure name, also called a tag. Then the structure members are defined with their type and variable names inside the open and closing braces "{" and "}". Finally, the closed braces end with a semicolon denoted as ";" following the statement. The above structure declaration is also called a structure specifier.

Syntax of STRUCTURE:
struct structure_name
{
data_type  structure_members;
......
......
};

Example of structure:
struct employee
{
int imp_id;
int salary;
float commission;
};




In the above example, the variables of different types such as int and float are grouped in a single structure name Employee. Arrays behave in the same way, declaring structures does not mean that memory is allocated. Structure declaration gives a skeleton for the structure.


Thanks
Mukesh Rajput
no image
Write a program to do the following task in C++ language:

(i) ‘Student’ is a base class, having two data members: entryno and name; entryno is integer and name of 20 characters long. The value of entryno is 1 for Science student and 2 for Arts student, otherwise it is an error.
(ii) ‘Science’ and ‘Arts’ are two derived classes, having respectively data items marks for Physics, Chemistry, Mathematics and marks for English, History, Economics.
(iii) Read appropriate data from the screen for 3 science and 3 arts students.
(iv) Display entryno, name, marks for science students first and then for arts students.

Program Code:
#include<iostream>
using namespace std;
//base class name student having some data members and member functions 
class student
{
protected:
int entryno;
char name[20];
public:
void getdata()
{
cout<<"Enter name of the student : " ;



cout<<endl;
cin>>name;
cout<<endl;
}
void display()
{
cout<<"Name of the student is : "<<name;
cout<<endl;
}
};
// derived class name science which is derived from student class
class science : public student
{
int pcm[3];
public:
void getdata()
{
student :: getdata();
cout<<"Enter marks for Physics,Chemistry and Mathematics : ";
cout<<endl;
for(int j=0; j<3; j++)
{
cin>>pcm[j];
}
}
void display()
{
entryno=1;
cout<<"Entry no for Science student is : "<<entryno;
cout<<endl;
student :: display();
cout<<"Marks in Physics,Chemistry and Mathematics are : ";
cout<<endl;
for(int j=0; j<3; j++)
{
cout<<pcm[j];
cout<<endl;;
}
}
};
// derived class name arts which is derived from student class
class arts : public student
{
int ehe[3];
public:
void getdata()
{
student :: getdata();
cout<<"Enter marks for English,History and Economics : ";
cout<<endl;
for(int j=0; j<3; j++)
{
cout<<ehe[j];
cout<<endl;;
}
}
};
//main function which is used to call all functions of base and derived class
int main()
{
science s1[3]; // array of object of science class 
arts a1[3]; //array of object of science class 
int i,j,k,l;
cout<<"Entry for Science students : ";
cout<<endl;
for(i=0; i<3; i++)
{
s1[i].getdata();
}
cout<<"Details of three Science students are : ";
cout<<endl;
for(j=0; j<3; j++)
{
s1[j].display();
}
cout<<"Entry for Arts students : ";
cout<<endl;
for(k=0; k<3; k++)
{
a1[k].getdata();
}
cout<<"Details of three Arts students are : ";



cout<<endl;
for(l=0; l<3; l++)
{
a1[l].display();
}
return 0;
}

// copyright to this program is reserved with the author.
Thanks
Mukesh Rajput