Data structure sh1
Data structure sh1
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<<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;
#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;
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;
#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;
#include <iostream>
using namespace std;
int main (){
int arr[20], Max=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;
#include <iostream>
using namespace std;
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];
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];
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;}
====================================================================================