0% found this document useful (0 votes)
14 views10 pages

S Structure Questions

Uploaded by

dilawarazad986
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)
14 views10 pages

S Structure Questions

Uploaded by

dilawarazad986
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/ 10

Javeria Hamdani

BSCS51F24S111
SS2 Semester#1

Structure
Question 1: Write a program that declares a structure of MyStructure that store no with integer data type. The
program should define structure variables, inputs the value and then display the value.

#include <iostream>
using namespace std;
struct MyStructure
{
int no;
};

int main()
{
MyStructure my;
cout << "Enter an integer value: ";
cin >> my.no;
cout << "The value stored in the structure is: " << myStruct.no << endl;

return 0;
}

Question 2: Write a program that declares a structure of Person that store Person id, name and age with
appropriate data types. The program should define structure variables, inputs the values and then displays their
values.

#include <iostream>
using namespace std;
struct Person
{
int id;
string name;
int age;
};

int main()
{
Person person;
cout << "Enter Person ID: ";
cin >> person.id;
cin.ignore();
cout << "Enter Person Name: ";
getline(cin, person.name);
cout << "Enter Person Age: ";
cin >> person.age;
cout << "\nPerson Details:" << endl;
cout << "ID: " << person.id << endl;
cout << "Name: " << person.name << endl;
cout << "Age: " << person.age << endl;

return 0;
}

Question 3: Write a program that declares a structure Student to store Roll No, English, physics, computer subject
Marks, average marks of a student. The program should define a structure variable, inputs the values, finds the
average and then displays the student values on the screen.

#include <iostream>
using namespace std;
struct Student
{
int rollNo;
int englishMarks;
int physicsMarks;
int computerMarks;
float averageMarks;
};
void calculateAverage(Student &student)
{
student.averageMarks = (student.englishMarks + student.physicsMarks + student.computerMarks) / 3.0;
}

int main()
{
Student student;
cout << "Enter Roll No: ";
cin >> student.rollNo;

cout << "Enter Marks in English: ";


cin >> student.englishMarks;

cout << "Enter Marks in Physics: ";


cin >> student.physicsMarks;

cout << "Enter Marks in Computer: ";


cin >> student.computerMarks;
calculateAverage(student);
cout << "\nStudent Details:" << endl;
cout << "Roll No: " << student.rollNo << endl;
cout << "Marks in English: " << student.englishMarks << endl;
cout << "Marks in Physics: " << student.physicsMarks << endl;
cout << "Marks in Computer: " << student.computerMarks << endl;
cout << "Average Marks: " << student.averageMarks << endl;

return 0;
}

Question 4: Write a program that declares a structure to store Computer id (int), brand name(string) and
price(float) of a computer. It defines two structure variables and inputs values. It displays the record of costliest
computer on the screen.

#include <iostream>
using namespace std;
struct Computer
{
int id;
char brand[50];
float price;
};

int main()
{
Computer comp1, comp2;
cout << "Enter Computer 1 details:" << endl;
cout << "ID: ";
cin >> comp1.id;
cout << "Brand: ";
cin >> comp1.brand;
cout << "Price: ";
cin >> comp1.price;
cout << "\nEnter Computer 2 details:" << endl;
cout << "ID: ";
cin >> comp2.id;
cout << "Brand: ";
cin >> comp2.brand;
cout << "Price: ";
cin >> comp2.price;
if (comp1.price > comp2.price)
{
cout << "\nCostliest Computer: " << endl;
cout << "ID: " << comp1.id << ", Brand: " << comp1.brand << ", Price: " << comp1.price << endl;
} else {
cout << "\nCostliest Computer: " << endl;
cout << "ID: " << comp2.id << ", Brand: " << comp2.brand << ", Price: " << comp2.price << endl;
}

return 0;
}

Question 5: Write a program that uses a structure to store employee number, name, hours worked, hourly rate
and gross pay. The program inputs employee number, name, hours worked and hourly rate the user, calculates
gross pay and then displays all employee data on screen.

#include <iostream>
using namespace std;
struct Employee
{
int employeeNumber;
char name[50];
float hoursWorked;
float hourlyRate;
float grossPay;
};
void calculateGrossPay(Employee &emp)
{
emp.grossPay = emp.hoursWorked * emp.hourlyRate;
}
int main()
{
Employee emp;
cout << "Enter Employee Number: ";
cin >> emp.employeeNumber;

cout << "Enter Employee Name (one word only): ";


cin >> emp.name;

cout << "Enter Hours Worked: ";


cin >> emp.hoursWorked;

cout << "Enter Hourly Rate: ";


cin >> emp.hourlyRate;
calculateGrossPay(emp);
cout << "\nEmployee Details:" << endl;
cout << "Employee Number: " << emp.employeeNumber << endl;
cout << "Name: " << emp.name << endl;
cout << "Hours Worked: " << emp.hoursWorked << endl;
cout << "Hourly Rate: " << emp.hourlyRate << endl;
cout << "Gross Pay: $" << emp.grossPay << endl;

return 0;
}

Question 6 Write a program that uses a structure to store employee number, name, hours worked, hourly rate and
gross pay. The program inputs employee number, name, hours worked and hourly rate the user, calculates gross
pay and then displays all employee data on screen.

#include <iostream>
using namespace std;
struct Employee
{
int employeeNumber;
char name[50];
float hoursWorked;
float hourlyRate;
float grossPay;
};
void calculateGrossPay(Employee &emp)
{
emp.grossPay = emp.hoursWorked * emp.hourlyRate;
}

int main()
{
Employee emp;
cout << "Enter Employee Number: ";
cin >> emp.employeeNumber;

cout << "Enter Employee Name (one word only): ";


cin >> emp.name;

cout << "Enter Hours Worked: ";


cin >> emp.hoursWorked;

cout << "Enter Hourly Rate: ";


cin >> emp.hourlyRate;
calculateGrossPay(emp);
cout << "\nEmployee Details:" << endl;
cout << "Employee Number: " << emp.employeeNumber << endl;
cout << "Name: " << emp.name << endl;
cout << "Hours Worked: " << emp.hoursWorked << endl;
cout << "Hourly Rate: " << emp.hourlyRate << endl;
cout << "Gross Pay: $" << emp.grossPay << endl;

return 0;
}

Question 7: Write a program that creates a structure Complex which represents fields by integer real and
imaginary. Program allows the user to enter the real and imaginary parts complex number and then print the
complex number on the screen.
For example, if user entered two complex numbers as
Enter Real part = 12
Enter imaginary Part = 4
Then output of program will be
Complex no = 12 + 4i

#include <iostream>
using namespace std;
struct Complex
{
int real;
int imaginary;
};

int main()
{
Complex c;
cout << "Enter Real part: ";
cin >> c.real;

cout << "Enter Imaginary part: ";


cin >> c.imaginary;
cout << "Complex number = " << c.real;

if (c.imaginary >= 0)
{
cout << " + " << c.imaginary << "i" << endl;
}
else
{
cout << " - " << -c.imaginary << "i" << endl;
}

return 0;
}

Question 8: #include <iostream>


using namespace std;
struct Complex
{
int real;
int imaginary;
};

int main()
{
Complex c;
cout << "Enter Real part: ";
cin >> c.real;

cout << "Enter Imaginary part: ";


cin >> c.imaginary;
cout << "Complex number = " << c.real;

if (c.imaginary >= 0)
{
cout << " + " << c.imaginary << "i" << endl;
}
else
{
cout << " - " << -c.imaginary << "i" << endl;
}

return 0;
}

Question 9: Write a program that creates a structure Player that store distance covered by the player along with
the minutes and seconds taken to cover the distance. The program should input the record of two players and then
display the record of the winner player.
Perform all above question with function
structure variable as arguments, returning structure variable from the function.

#include <iostream>
using namespace std;
struct Player
{
float distance;
int minutes;
int seconds;
};
Player inputPlayerRecord()
{
Player p;
cout << "Enter distance covered by player (in meters): ";
cin >> p.distance;
cout << "Enter time taken (minutes): ";
cin >> p.minutes;
cout << "Enter time taken (seconds): ";
cin >> p.seconds;

return p;
}
int calculateTotalTimeInSeconds(const Player& p)
{
return p.minutes * 60 + p.seconds;
}
Player getWinner(Player p1, Player p2)
{
int time1 = calculateTotalTimeInSeconds(p1);
int time2 = calculateTotalTimeInSeconds(p2);

if (time1 < time2)


{
return p1;
} else {
return p2;
}
}
void displayPlayerRecord(const Player& p)
{
cout << "Distance covered: " << p.distance << " meters\n";
cout << "Time taken: " << p.minutes << " minutes " << p.seconds << " seconds\n";
}

int main()
{
Player player1, player2;
cout << "Enter record for Player 1:\n";
player1 = inputPlayerRecord();

cout << "\nEnter record for Player 2:\n";


player2 = inputPlayerRecord();
Player winner = getWinner(player1, player2);
cout << "\nWinner's Record:\n";
displayPlayerRecord(winner);

return 0;
}

Question 10: Create a structure called TIME. It three members, all type int, should be called hours, minutes, and
seconds. Write a program that create one structure variable in main () and prompts the user to enter a TIME value
in hours, minutes, and seconds. This can be in 12:59:59 format, or each number can be entered at a separate
prompt (“Enter hours:” and so forth). The program should then store the time in a variable of type struct TIME,
then pass structure variable to user define function. The function prints the total number of seconds represented
by this time variable.

#include <iostream>
using namespace std;
struct TIME
{
int hours;
int minutes;
int seconds;
};
int calculateTotalSeconds(const TIME& t)
{
return (t.hours * 3600) + (t.minutes * 60) + t.seconds;
}

int main()
{
TIME time;
cout << "Enter hours: ";
cin >> time.hours;

cout << "Enter minutes: ";


cin >> time.minutes;

cout << "Enter seconds: ";


cin >> time.seconds;
int totalSeconds = calculateTotalSeconds(time);
cout << "Total time in seconds: " << totalSeconds << " seconds" << endl;

return 0;
}
Question 11: Write a program that declares a structure to store the code number, salary and grade of an employee.
The program defines two structure variables, inputs records of two employees and then displays the record of the
employee with more salary.

#include <iostream>
using namespace std;
struct Employee
{
int code;
float salary;
char grade;
};
Employee inputEmployeeRecord()
{
Employee emp;

cout << "Enter Employee Code: ";


cin >> emp.code;

cout << "Enter Employee Salary: ";


cin >> emp.salary;

cout << "Enter Employee Grade: ";


cin >> emp.grade;

return emp;
}
void displayEmployeeRecord(const Employee& emp)
{
cout << "Employee Code: " << emp.code << endl;
cout << "Employee Salary: $" << emp.salary << endl;
cout << "Employee Grade: " << emp.grade << endl;
}

int main()
{
Employee emp1, emp2;
cout << "Enter record for Employee 1:" << endl;
emp1 = inputEmployeeRecord();

cout << "\nEnter record for Employee 2:" << endl;


emp2 = inputEmployeeRecord();
if (emp1.salary > emp2.salary)
{
cout << "\nEmployee with Higher Salary:" << endl;
displayEmployeeRecord(emp1);
} else {
cout << "\nEmployee with Higher Salary:" << endl;
displayEmployeeRecord(emp2);
}
return 0;
}

You might also like