Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
LAB # 10: Structures in C++
CLO-4/PLO-5
Course Instructor: Dr Asim Khan
Lab Instructor: Engr. Ayesha Khanam
Sr. No. Student’s Name CMS ID
1 ABDUL WAHAB 544532
1
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
LAB #10: Structures in C++
Lab Tasks
1. Write a program in C++ where you define a structure called Employee that contains an
employee’s name, ID, and salary. Declare an array to store the information of five
employees. Use loops to accept the details of each employee from the user and then display
all the information using a second loop.
#include <iostream>
#include <string>
using namespace std;
struct employee {
string name;
int id;
double salary;
};
int main() {
employee list[5];
for (int i = 0; i < 5; i++) {
cout << "enter name for employee " << i + 1 << ": ";
cin >> list[i].name;
cout << "enter id: ";
cin >> list[i].id;
cout << "enter salary: ";
cin >> list[i].salary;
}
for (int i = 0; i < 5; i++) {
cout << "name: " << list[i].name << " | id: " << list[i].id << " | salary:
" << list[i].salary << endl;
}
2
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
return 0;
2. Create a program that defines a structure named Circle with two members: a floating-point
variable for the radius and a character array for the colour. Declare two variables of type
Circle, assign different values to each, and write logic to compare both their radius and
colour values. Display a message to the user indicating whether the two circles are the
same or different, based on the comparison.
#include <iostream>
#include <cstring>
using namespace std;
struct circle {
float radius;
char colour[10];
};
int main() {
circle c1, c2;
cout << "enter radius for circle 1: ";
cin >> [Link];
cout << "enter color for circle 1: ";
cin >> [Link];
cout << "enter radius for circle 2: ";
cin >> [Link];
cout << "enter color for circle 2: ";
cin >> [Link];
if ([Link] == [Link] && strcmp([Link], [Link]) == 0) {
cout << "circles are identical" << endl;
3
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
}
else {
cout << "they are not same" << endl;
}
return 0;
}
3. Write a program in C++ that defines a structure named Product to represent items in a
store. The structure should include the following members: an integer productID, a
character array name (up to 30 characters), a floating-point variable price, and an integer
quantity. In your program, create an array to store the details of five different products.
Then, write a function that takes this array and performs three tasks: first, it should
calculate and display the total value of each product (calculated as price × quantity);
second, it should identify and display the product with the lowest quantity in stock; and
third, it should prompt the user to enter a threshold value and then display all products
whose total value exceeds that threshold.
#include <iostream>
using namespace std;
struct product {
int productid;
char name[30];
float price;
int quantity;
4
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
};
void checkinventory(product list[5]) {
int i;
int lowest = 0;
for (i = 0; i < 5; i++) {
float total = list[i].price * list[i].quantity;
cout << "item: " << list[i].name << " total value: " <<
total << endl;
if (list[i].quantity < list[lowest].quantity) {
lowest = i;
}
}
cout << "item with lowest stock: " << list[lowest].name <<
endl;
float limit;
cout << "enter threshold number: ";
cin >> limit;
cout << "items above threshold:" << endl;
5
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
for (i = 0; i < 5; i++) {
float total = list[i].price * list[i].quantity;
if (total > limit) {
cout << list[i].name << endl;
}
}
}
int main() {
product items[5];
for (int i = 0; i < 5; i++) {
cout << "enter id: ";
cin >> items[i].productid;
cout << "enter name: ";
cin >> items[i].name;
cout << "enter price: ";
cin >> items[i].price;
cout << "enter quantity: ";
cin >> items[i].quantity;
}
checkinventory(items);
6
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
return 0;
}