Sunteți pe pagina 1din 13

Lab # 11 – Programming Fundamentals (CPE-121)

Lab Manual # 11

Title: C++ Structures

CLO: CLO-1

Student Name: M.usama saghar Class Roll: CPE-2019-27

Subject Teacher : Engr. Muhammad Baqer


Lab Engineer : Engr. Abdul Rehman
Lab Performed In : Networking Lab (CPED)

LAB POINTS SCORE


Lab Performance [10] 0 1 2 3 4 5
Lab Participation
Lab Activity Completion on the Same Day

Lab Submission[10] 0 1 2 3 4 5
Completeness & Correctness
Required Conclusion & Results

No of Checks
SUB TOTAL
TOTAL SCORE

______________________
Course Instructor / Lab Engineer

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 11 – Programming Fundamentals (CPE-121)

Structure is the collection of variables of different types under a single name for better visualization of
problem. Arrays is also collection of data but arrays can hold data of only one type whereas structure can
hold data of one or more types.

How to define a structure in C++ programming?

The struct keyword defines a structure type followed by an identifier(name of the structure). Then inside
the curly braces, you can declare one or more members (declare variables inside curly braces) of that
structure. For example:

struct person {

char name[50];

int age;

float salary;

};

Here a structure person is defined which has three members: name, age and salary.

When a structure is created, no memory is allocated. The structure definition is only the blueprint for the
creating of variables. You can imagine it as a datatype. When you define an integer as below:

intA;

The int specifies that, variable A can hold integer element only. Similarly, structure definition only
specifies that, what property a structure variable holds when it is defined.

How to define a structure variable?

Once you declare a structure person as above. You can define a structure variable as:

personB;
Here, a structure variable B is defined which is of type structure person. When structure variable is defined, then
only the required memory is allocated by the compiler. Considering you have either 32-bit or 64-bit system, the
memory of float is 4 bytes, memory of int is 4 bytes and memory of char is 1 byte. Hence, 58 bytes of memory is
allocated for structure variable B.

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 11 – Programming Fundamentals (CPE-121)
How to access members of a structure?

The members of structure variable are accessed using dot operator. Suppose, you want to access  age of
structure variable B and assign it 50 to it. You can perform this task by using following code below:

B.age = 50;

Example: C++ Structure

Program to assign data to members of a structure variable and display it.

#include<iostream>

usingnamespacestd;

struct person{

char name[50];

int age;

float salary;

};

int main(){

person p1;

cout<<"Enter Full name: ";

cin.get(p1.name,50);

cout<<"Enter age: ";

cin>> p1.age;

cout<<"Enter salary: ";

cin>> p1.salary;

cout<<"\nDisplaying Information."<<endl;

cout<<"Name: "<< p1.name <<endl;

cout<<"Age: "<< p1.age <<endl;

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 11 – Programming Fundamentals (CPE-121)

cout<<"Salary: "<< p1.salary;

return0;

} Here a structure person is declared which has three members. Inside main() function, a structure variable p1 is
defined. Then, the user is asked to enter information and data entered by user is displayed.

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 11 – Programming Fundamentals (CPE-121)
P-1: Write a C++ Program to Store Information of Students Using Structure. At least 4 students.

Your Code:

// C Program to Store Information


// of Students Using Structure

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Create the student structure


struct Student {
char* name;
int roll_number;
int age;
double total_marks;
};

// Driver code
int main()
{
int i = 0, n = 5;

// Create the student's structure variable


// with n Student's records
struct Student student[n];

// Get the students data


student[0].roll_number = 1;
student[0].name = "Haalim";
student[0].age = 12;
student[0].total_marks = 78.50;

student[1].roll_number = 5;
student[1].name = "Gabrial";
student[1].age = 10;
student[1].total_marks = 56.84;

student[2].roll_number = 2;
student[2].name = "Haan";
student[2].age = 11;
student[2].total_marks = 87.94;

student[3].roll_number = 4;

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 11 – Programming Fundamentals (CPE-121)
student[3].name = "saghar";
student[3].age = 12;
student[3].total_marks = 89.78;

student[4].roll_number = 3;
student[4].name = "Usama";
student[4].age = 13;
student[4].total_marks = 78.55;

// Print the Students information


printf("Student Records:\n\n");
for (i = 0; i < n; i++) {
printf("\tName = %s\n", student[i].name);
printf("\tRoll Number = %d\n", student[i].roll_number);
printf("\tAge = %d\n", student[i].age);
printf("\tTotal Marks = %0.2f\n\n", student[i].total_marks);
}

return 0;
}

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 11 – Programming Fundamentals (CPE-121)

Paste your output here

P-2: Write a program to define structure with four members. The first member is book Title, the
other be author name, the book id and the subject name. Assign values to the members during
their declaration and display them.

Your Code:

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 11 – Programming Fundamentals (CPE-121)
#include <iostream>
using namespace std;

struct members
{
char title[50];
char name[50];
int id;
char subject[100];
};

int main()
{
members s;
cout << "Enter Book title," << endl;
cin>> s.title;
cout << "Enter Author name: ";
cin >> s.name;
cout << "Enter id: ";
cin >> s.id;
cout << "Enter the subject name: ";
cin >> s.subject;

cout << "\nDisplaying Information," << endl;


cout << "Name: " << s.name << endl;
cout << "id: " << s.id << endl;
cout << "Subject name: " << s.subject << endl;
return 0;
}

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 11 – Programming Fundamentals (CPE-121)

Paste your output here

P-3: Write a C++ Program to Add Two Distances (in inch-feet) System Using Structures If inch is
greater than 12, changing it to feet.

Your Code:
#include <iostream>
using namespace std;

struct Distance{
int feet;
float inch;
}d1 , d2, sum;

int main()

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 11 – Programming Fundamentals (CPE-121)
{
cout << "Enter 1st distance," << endl;
cout << "Enter feet: ";
cin >> d1.feet;
cout << "Enter inch: ";
cin >> d1.inch;

cout << "\nEnter information for 2nd distance" << endl;


cout << "Enter feet: ";
cin >> d2.feet;
cout << "Enter inch: ";
cin >> d2.inch;

sum.feet = d1.feet+d2.feet;
sum.inch = d1.inch+d2.inch;

// changing to feet if inch is greater than 12


if(sum.inch > 12)
{
++ sum.feet;
sum.inch -= 12;
}

cout << endl << "Sum of distances = " << sum.feet << " feet " << sum.inch << " inches";
return 0;
}

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 11 – Programming Fundamentals (CPE-121)

Paste your output here

P-4: Write a program to define structure with five members . The first member be student
name and the other be marks obtained in subjects. Assign values to the members during their
declaration. Add the marks of the subjects to calculate total marks and then prints these
numbers and the total marks of the students.

Your Code:

#include <iostream>
using namespace std;

int main(){
int subjects, i;

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 11 – Programming Fundamentals (CPE-121)
float marks, total=0.0f, averageMarks, percentage;

// Input number of subjects

cout << "Enter number of subjects\n";


cin >> subjects;

// Take marks of subjects as input


cout << "Enter marks of subjects\n";

for(i = 0; i < subjects; i++){


cin >> marks;
total += marks;
}

// Calculate Average
averageMarks = total / subjects;

// Each subject is of 100 Marks


percentage = (total/(subjects * 100)) * 100;

cout << "Total Marks = " << total;


cout << "\nAverage Marks = " << averageMarks;
cout << "\nPercentage = " << percentage;

return 0;
}

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 11 – Programming Fundamentals (CPE-121)

Paste your output here

Comments:
In this lab we studied about how to define a structure in C++ programming
how to access members of a structure?

1. Array elements are accessed using the Subscript variable , Similarly Structure


members are accessed using dot [.] operator.
2. (.) is called as “Structure member Operator”.
3. Use this Operator in between “Structure name” & “member name”.

Version 1.0 Department of Computer Engineering UCE&T BZU Multan

S-ar putea să vă placă și