0% found this document useful (0 votes)
20 views11 pages

Data structure sh1

The document contains a series of C++ programming exercises involving structures, arrays, and arrays of structures. It includes tasks such as calculating room volume, managing employee data, handling 2D points, and processing sales data. Each exercise is accompanied by code snippets demonstrating the required functionality.
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)
20 views11 pages

Data structure sh1

The document contains a series of C++ programming exercises involving structures, arrays, and arrays of structures. It includes tasks such as calculating room volume, managing employee data, handling 2D points, and processing sales data. Each exercise is accompanied by code snippets demonstrating the required functionality.
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/ 11

[A] STRUCTURES

1. Create a structure called volume that represents a room dimension: length, width, and high.
Define a variable of type volume and initialize its members to the room dimensions: 4.65, 4.2 and
3.0 Then calculate the volume of the room and print out the result on the screen.
#include <iostream>
using namespace std;
struct volume
{
float length;
float width;
float high;
};
int main()
{
struct volume VoL;
float RoomVolume;
VoL.length=4.65;
VoL.width=4.2;
VoL.high=3.0;
RoomVolume= VoL.length*VoL.width*VoL.high;
cout<<RoomVolume;
return 0;}
2. Create a structure called employee that contains two members: an employee ID of type int and
an employee salary of type float. Ask the user to fill data for three employees and store these data
in three variables E1, E2 and E3. Then display the information of the three employees on the screen.

#include <iostream>
using namespace std;
struct employee{
int ID;
float salary;
};
int main(){
struct employee E1, E2, E3;
cin>>E1.ID;
cin>>E1.salary;
cin>>E2.ID;
cin>>E2.salary;
cin>>E3.ID;
cin>>E3.salary;

cout<<"Data of First Employee is:",


cout<<E1.ID<<" "<<E1.salary<<endl;
cout<<"Data of Second Employee is:",
cout<<E2.ID<<" "<<E2.salary<<endl;
cout<<"Data of Third Employee is:",
cout<<E3.ID<<" "<<E3.salary<<endl;
return 0;}
3. A point on two-dimensional plan can be represented by two coordinates X and Y. Write a C++
program that uses a structure called Point to model a point in 2D. Define three points P1, P2 and P3
and allow a user to input coordinates of two points P1 and P2. Then set the third point P3 equals the
sum of the other two points and display the coordinates of the third point P3.
#include <iostream>
using namespace std;
struct Point{
float x;
float y;
};
int main(){
struct Point P1, P2, P3;
cin>>P1.x;
cin>>P1.y;
cin>>P2.x;
cin>>P2.y;

P3.x= P1.x + P2.x;


P3.y= P1.y + P2.y;

cout<<P3.x<<endl;
cout<<P3.y<<endl;
return 0;}
4. Write a C++ program to create a product defined by product name of type character, product model of type
integer and product price of type float. Allow a user to input the data of two products P1 and P2 from the
keyboard and then print these data on the screen.

#include <iostream>
using namespace std;
struct Product{
char name [15];
int model;
float price;};
int main(){
struct Product P1, P2;
cin>>P1.name;
cin>>P1.model;
cin>>P1.price;

cin>>P2.name;
cin>>P2.model;
cin>>P2.price;

cout<<"Data of First Product"<<endl;


cout<<P1.name<<" "<<P1.model<<" "<<P1.price<<endl;

cout<<"Data of Second Product"<<endl;


cout<<P2.name<<" "<<P2.model<<" "<<P2.price<<endl;
return 0;}
5. Write a C++ program to create a student data defined by student ID of type integer, student name
of type character, and student price of type float. Allow a user to input the data of three students S1,
S2 and S3 from the keyboard and then print these data of the students on the screen.

#include <iostream>
using namespace std;
struct student{
char name [15];
int ID;
float price;
};
int main(){
struct student S1, S2,S3;
cin>>S1.name;
cin>>S1.ID;
cin>>S1.price;

cin>>S2.name;
cin>>S2.ID;
cin>>S2.price;

cin>>S3.name;
cin>>S3.ID;
cin>>S3.price;

cout<<"Data of First Student"<<endl;


cout<<S1.name<<" "<<S1.ID<<" "<<S1.price<<endl;

cout<<"Data of Second Student"<<endl;


cout<<S2.name<<" "<<S2.ID<<" "<<S2.price<<endl;

cout<<"Data of Third Student"<<endl;


cout<<S3.name<<" "<<S3.ID<<" "<<S3.price<<endl;
return 0;}
[B] ARRAYS

6. Write a C++ program to create an array of 10 float numbers. Enter the contents of the array from
the keyboard and then print the sum and the average on the screen.

#include <iostream>
using namespace std;
int main ()
{
float arr[10], sum=0,avg=0;

for (int i=0; i<10; i++){


cin>> arr[i];
sum=sum + arr[i];
}
avg= sum/10;
cout<<sum<<endl;
cout<<avg;
return 0;
}
7. Write a program to input 20 integer numbers into 1-dimension array and then print on the screen
the contents of the array, the sum and the average of even values in the array.

#include <iostream>
using namespace std;
int main ()
{
int arr[20], sum=0, count=0;
float avg=0;
for (int i=0; i<20; i++){
cin>> arr[i];
if (arr[i]%2==0){
count ++;
sum=sum + arr[i];}
}
avg= sum/count;
for (int i=0; i<20; i++){
cout<< arr[i];
}
cout<<endl;
cout<<sum<<endl;
cout<<avg;
return 0;
}
8. Write a program to input 30 integer numbers into 1D array and then print on the screen the
contents of the array, the sum and the average of the values divisible by 5 in the array.

#include <iostream>
using namespace std;
int main ()
{
int arr[30], sum=0, count=0;
float avg=0;
for (int i=0; i<30; i++){
cin>> arr[i];
if (arr[i]%5==0){
count ++;
sum= sum + arr[i];}
}
avg= sum/count;
for (int i=0; i<30; i++){
cout<< arr[i]<<" ";
}
cout<<endl;
cout<<sum<<endl;
cout<<avg;
return 0;
}

9. Write a C++ program to create an array representing sales of a week. Enter the sales values of
each day of the week and then calculate the sum and the average of the sales in the week and print
the results on the screen.

#include <iostream>
using namespace std;
int main ()
{
float arr[7], sum=0,avg=0;

for (int i=0; i<7; i++){


cin>> arr[i];
sum= sum + arr[i];
}
avg=sum/7;
cout<<sum<<endl;
cout<<avg;
return 0;
}
10. Write a program to allow the user to input 20 integer numbers into an array from the keyboard,
and then goes through the array elements, element by element, looking for the largest value.
Finally, print the largest number on the screen.

#include <iostream>
using namespace std;
int main (){
int arr[20], Max=0;

for (int i=0; i<20; i++){


cin>> arr[i];
}
Max= arr[0];
for (int i=1; i<20; i++){
if (Max<arr[i])
{ Max=arr[i];}
}
cout<<Max;
return 0;
}

11. Write a program to allow the user to input 30 integer numbers into an array from the keyboard,
and then goes through the array elements, element by element, looking for the minimum value.
Finally, print the minimum value on the screen.

#include <iostream>
using namespace std;
int main (){
int arr[30], Min=0;

for (int i=0; i<30; i++){


cin>> arr[i];
}
Min= arr[0];
for (int i=1; i<30; i++){
if (Min>arr[i])
{ Min=arr[i];}
}
cout<<Min;
return 0;
}
12. Write a program to input 10 integer numbers into Array Y. Then call a function called Min() to find
and return the min value and another function called Max() to find and return the max value. Print
the results on the screen.

#include <iostream>
using namespace std;

int MIN(int Y[]){


int Min=Y[0];
for (int i=1 ; i<10; i++){
if (Min>Y[i]){
Min= Y[i];}}
return Min;
}
int MAX(int Y[]){
int Max=Y[0];
for (int i=1 ; i<10; i++){
if (Max<Y[i]){
Max= Y[i];}}
return Max;
}
int main (){
int min,max;
int Y[10];
for (int i=0 ; i<10; i++){
cin>>Y[i];}

min=MIN(Y);
max=MAX(Y);
cout<<min<<endl;
cout<<max;
return 0;}
13. Write a program to input data into 5x3 2D array A of type float. Then compute the values of
another array B such that each element in B represent 5 times of the corresponding element in A.
Finally, print both the arrays A and B on the screen.
#include <iostream>
using namespace std;
int main() {
float A[5][3], B[5][3];

for (int i = 0; i<5; i++) {


for (int j = 0; j <3; j++) {
cin >> A[i][j];
B[i][j] = 5 * A[i][j];}
}
cout << "Matrix A:" << endl;
for (int i = 0; i<5; i++) {
for (int j = 0; j<3; j++) {
cout << A[i][j]<<" ";}
cout << endl;
}
cout << "Matrix B:" << endl;
for (int i = 0; i<5; i++) {
for (int j = 0; j<3; j++) {
cout << B[i][j]<<" ";}
cout << endl;}
return 0;}

14. Write a program to input data into 5x3 2D array X of type float. Then, display the contents of the
array as well as the summation and average of array contents.
#include <iostream>
using namespace std;
int main() {
float X[5][3];
float sum=0,avg=0;
for (int i = 0; i<5; i++) {
for (int j = 0; j <3; j++) {
cin >> X[i][j];}
}
for (int i = 0; i<5; i++) {
for (int j = 0; j <3; j++) {
sum= sum + X[i][j];}
}
for (int i = 0; i<5; i++) {
for (int j = 0; j<3; j++) {
cout << X[i][j]<<" ";}
cout << endl;
}
avg= sum /(5*3);
cout<<sum<<" "<<avg;
return 0;}
15. Write a program to input the sales of a company in one month in an array, and then find the
average of the sales in the week and in the month as well as the sum of the sales and finally print
out both the average and the sum.

#include <iostream>
using namespace std;

int main() {
int n;
cin >> n;
int* arr = new int[n];

int Summonth = 0, SremainingSales = 0;


for (int i = 0; i < n; i++) {
cin >> arr[i];
Summonth += arr[i];}

int Sumweek[4] = {0};


for (int i = 0; i < 28; i++)
Sumweek[i / 7] += arr[i];
for (int i = 28; i < n; i++)
SremainingSales += arr[i];

cout << Summonth << "<" << Summonth / n << endl;

for (int i = 0; i < 4; i++)


cout << Sumweek[i] << "<" << Sumweek[i] / 7 << endl;

if (n > 28)
cout << SremainingSales << "<" << SremainingSales / (n - 28) << endl;

delete[] arr;
return 0;}
[C] ARRAY OF STRUCTURES

16. Write a program to create a database of products where each product is defined by model,
name, and price. The program allows a user to input data of 50 products. Finally, print the data of
the products on the screen.

#include <iostream>
using namespace std;
struct product {
int model;
char name [15];
float price;
};

int main() {
struct product pdt [50];
for (int i=0; i<50; i++){
cin>>pdt [i].model;
cin>>pdt [i].name;
cin>>pdt [i].price;}
for (int i=0; i<50; i++){
cout<<pdt [i].model<<" ";
cout<<pdt [i].name<<" ";
cout<<pdt [i].price<<endl;}
return 0;}
17. Write a program to create a database for 100 students, where each student is defined by stno,
stname, staddress, and stage. The program allows a user to input data of 50 students. Finally, print
the data of first 10 students on the screen.

#include <iostream>
#include <string>
using namespace std;
struct student {
int stno;
string stname;
string staddress;
int stage;
};
int main() {
struct student std [100];
for (int i=0; i<50; i++){
cin>>std [i].stno;
cin>>std [i].stname;
cin>>std [i].staddress;
cin>>std [i].stage;}

for (int i=0; i<10; i++){


cout<<std [i].stno;
cout<<std [i].stname;
cout<<std [i].staddress;
cout<<std [i].stage;}
return 0;}

====================================================================================

"‫علّ ْمتَنَاَ ِإنّكَََأ َ ْنتَََا ْلعَ ِلي ُمََا ْل َح ِكي َُم‬


َ َ‫س ْب َحانَكَََالَ ِع ْل ََمَلَنَاَ ِإ َّالَ َما‬
ُ "
َ‫َوزدناَعلما‬،‫َوانفعناَبماَعلمتنا‬،‫اللهمَعلمناَماَينفعنا‬
by Kareem Elhafi
LinkedIn

You might also like