[go: up one dir, main page]

0% found this document useful (0 votes)
51 views8 pages

Lec 4

The document discusses data structures in C++. It defines a structure as a collection of simple variables that can be of different types. Structures allow grouping related data together under one name. The document provides examples of declaring structure variables, defining structures that contain arrays, using pointers to structures, and passing structures to functions. It demonstrates how to define, declare, read from, and print structure data in C++ programs.

Uploaded by

MUSTAFA Bsh
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)
51 views8 pages

Lec 4

The document discusses data structures in C++. It defines a structure as a collection of simple variables that can be of different types. Structures allow grouping related data together under one name. The document provides examples of declaring structure variables, defining structures that contain arrays, using pointers to structures, and passing structures to functions. It demonstrates how to define, declare, read from, and print structure data in C++ programs.

Uploaded by

MUSTAFA Bsh
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
You are on page 1/ 8

Data Structure Lecture-4

Structures:
A structure is a collection of simple variables. The variables in a structure can be
of different types: int, float, and so on. (This is unlike the array, in which all the
variables must be the same type.) The data items in a structure are called the
members of the structure.

struct Name

{
type Element1;
type Element2;
type Element3;
.
.
} Object_Name;

Examples:

1
Data Structure Lecture-4

There are two ways to declare objects of type struct:


The first one by declare variables of type struct in the main function as you can see in the
example:

struct student
{
char name [20];
int age;
};

void main()
{
student st1, st2;
}

The second way the declaration is after the end of the structure body as shown in the
example:

struct student
{
char name [20];
int age;
} st1, st2;

Example: Design a structure named (student) contains three elements (name, stage and degree):

Write a program to execute the followings:

1) Read all three information for 2 students.

2) Print all information for 2 students.

2
Data Structure Lecture-4

// Lec3.cpp : Defines the entry point for the console application.


//

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;

struct student
{
string name;
int stage;
int mark;
};
int _tmain(int argc, _TCHAR* argv[])
{
student std1, std2;
cout << "Enter the information of the first student: "<<"\n \n";
cin >> std1.name;
cin >> std1.stage;
cin >> std1.mark;
cout << "Enter the name of the second student" << "\n\n";
cin >> std2.name;
cin >> std2.stage;
cin >> std2.mark;

cout << "The information of first student is: " << std1.name << " " << std1.stage << " " << std1.mark << "\n\n";

cout << "The information of second student is: " << std2.name << " " << std2.stage << " " << std2.mark << "\n\n";

return 0;
}

3
Data Structure Lecture-4

Structures and Array:


Any structure member can be an array for example the marks in the student structure can be an
array of integer:
struct student
{
string name;
int stage;
int mark[10];
}std1;

The access of that structure member is like:

std1.mark[i]=55; or cin>>std1.mark[i]; and so on.

Example: Design a structure named (student) contains three elements (string name, integer stage and
integer mark [3]):
Write a program to execute the followings:
1- Read all three information for 2 students. 2- Print all information for 2 students.

struct student
{
string name;
int stage;
int mark[3];
};

int _tmain(int argc, _TCHAR* argv[])


{ student std1, std2;
int i;

cout << "Enter the information of the first student: "<<"\n \n";
cin >> std1.name;
cin >> std1.stage;

for (i = 0; i < 3; i++)


cin >> std1.mark[i];

cout << "Enter the name of the second student" << "\n\n";
cin >> std2.name;
cin >> std2.stage;

for (i = 0; i < 3; i++)


cin >> std2.mark[i];

cout << "The information of first student is: " << std1.name << " " << std1.stage << " ";
for (i = 0; i < 3; i++)
cout<< std1.mark[i] << " ";
cout << endl;
cout << "The information of second student is:" << std2.name << " " << std2.stage << " ";
for (i = 0; i < 3; i++)
cout << std2.mark[i] << " ";
return 0;
}

4
Data Structure Lecture-4

The objects of structures can be arrays of that structure in our


example of the student structure if there are more than two
students it will be very difficult to define them as separated
variables, but an array of students can be defined instead of using
more than two variables as shown below:

struct student
{
string name;
int stage;
int mark[2];
}stdnt[3];

#include<string>
using namespace std;

struct student
{
string name;
int stage;
int mark[2];
} stdnt[3];

int _tmain(int argc, _TCHAR* argv[])


{
int i, j;
for (j = 0; j < 3; j++)
{

cout << "Enter the information of a new student: " << "\n \n";
cin >> stdnt[j].name;
cin >> stdnt[j].stage;
for (i = 0; i < 2; i++)
cin >> stdnt[j].mark[i];
}

for (j = 0; j < 3; j++)


{

cout<<"\nThe info of student " << j+1<< "is:" << stdnt[j].name


cout<<"Stag"<<stdnt[j].stage<<"Marks";

for (i = 0; i < 2; i++)


cout << stdnt[j].mark[i] << " ";
cout << endl<<endl;
}

return 0;
}

5
Data Structure Lecture-4

Pointer of structure can be used, in this case the operator will be


(->) instead of the dot (.),the -> is called the arrow operator. It is formed
by using the minus sign followed by a greater than sign.

Simply saying: To access members of a structure, use the dot operator. To access
members of a structure through a pointer, use the arrow operator.

struct student
{
string name;
int stage;
int mark;

}*stdptr;

stdptr->name; // to access the student name via stdptr pointer


stdptr->stage; // to access the student stage via stdptr pointer
stdptr->mark; // to access the student mark via stdptr pointer

- To see how to access structure members, we can use a pointer to the student structure and
do the same instructions on the previous example.
- The first step after the declaration of the object and the pointer is to initialize the pointer with
the memory address of the object:
stdptr = &std1;

- After that we can access the structure members directly or via pointer.
- The output of the program will be:

6
Data Structure Lecture-4

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;

struct student
{
string name;
int stage;
int mark;
};

int _tmain(int argc, _TCHAR* argv[])


{
student *stdptr, std1;

stdptr = &std1;

cout << "Enter the information of a new student: " << "\n \n";

cin >> stdptr->name;


cin >> stdptr->stage;
cin >> stdptr->mark;

cout << "\n\nThe information of student is: " << stdptr->name;


cout << " Stage " << stdptr->stage << " Mark " << stdptr->mark<<"\n\n";

stdptr->stage = stdptr->stage + 1;
stdptr->mark = 88;

cout << "\n\nThe information of student is: " << stdptr->name;


cout << " Stage " << stdptr->stage << " Mark " << stdptr->mark;

return 0;
}

7
Data Structure Lecture-4

Passing the structure to a function:


Structure can be passed to a function using pointer, this is by pass the address of the structure to
a function.
Example: Write a program to read the information (name, stage, maks[3]) of a student, then use a
function to print the average of the marks.

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;

struct student
{
string name;
int stage;
int mark[3];
};

void checkfun(student *stdpointer);

int _tmain(int argc, _TCHAR* argv[])


{
int i, j;
student std1;

cout << "Enter the information of a new student: " << "\n \n";
cin >> std1.name;
cin >> std1.stage;
for (i = 0; i < 3; i++)
cin >> std1.mark[i];

checkfun(&std1);

return 0;
}

void checkfun(student *stdpointer)


{
int sum = 0, i;
float avg;

for (i = 0; i < 3; i++)


{

sum +=stdpointer->mark[i];
}

avg = sum / 3;

cout << "The average of the student is : " << avg<<"\n";


}

You might also like