0% found this document useful (0 votes)
7 views

Assignment 1 Solution

This is Related to OOP concepts

Uploaded by

mabdullah1mughal
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)
7 views

Assignment 1 Solution

This is Related to OOP concepts

Uploaded by

mabdullah1mughal
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/ 6

Question 1:

#include<iostream>
using namespace std;
struct Distance{
int feet;
float inch;
};
struct Volume{
Distance length, width, height;
};
int main(){
float len, wid, hei;
Volume room1 = {{13, 2.5}, {11, 5.5}, {5,2.5}};
len = room1.length.feet+ room1.length.inch/12;
wid = room1.width.feet+ room1.width.inch/12;
hei = room1.height.feet+ room1.height.inch/12;
cout << "Volume = " << len * wid * hei << " cubic feet" << endl;
return 0;
}

Question 2:
#include<iostream>
using namespace std;
struct employee{
int emp_num;
float emp_comp;
};
int main(){
employee e1, e2, e3;
cout << "Enter employee details: " << endl;
cout << endl << "Employee 1\nEmployee number: ";
cin >> e1.emp_num;
cout << "Employee's compensation: ";
cin >> e1.emp_comp;
cout << endl << "Employee 2\nEmployee number: ";
cin >> e2.emp_num;
cout << "Employee's compensation: ";
cin >> e2.emp_comp;
cout << endl << "Employee 3\nEmployee number: ";
cin >> e3.emp_num;
cout << "Employee's compensation: ";
cin >> e3.emp_comp;
cout << endl << "Employee Details: ";
cout << endl<< "Employee 1\nEmployee number: " << e1.emp_num << "\nEmployee's compensation:
" << e1.emp_comp << endl;
cout << endl<< "Employee 2\nEmployee number: " << e2.emp_num << "\nEmployee's compensation:
" << e2.emp_comp << endl;
cout << endl<< "Employee 3\nEmployee number: " << e3.emp_num << "\nEmployee's compensation:
" << e3.emp_comp << endl;
return 0;
}

Question 3:
Step 1
1 of 2

To declare an array alpha of integers we do:

int alpha[50];
Result
2 of 2
Declare a static array of 50 elements.

Step 1
1 of 2

We should use a for loop to initialize components like this:


for (int i = 0; i < 50; i++) {
alpha[i] = -1;
}
Result
2 of 2
Use a for loop to set all of the components to -1.

Step 1
1 of 2

To output the value of the first component we do:

cout << alpha[0];


Result
2 of 2
Output the value of alpha[0].
Step 1
1 of 2
To set the value of the 25th component we use: alpha[24] = 62; Keep in mind that
counting in arrays starts from 0, so when we want to get the 25th element, we should
look at position 24
Result
2 of 2
Set the alpha[24] to 62.

Step 1
1 of 2

Here, we just do set the value of alpha[9] like this:

alpha[9] = 3 * alpha[49] + 10;


Result
2 of 2
Set the value of alpha[9] to the answer of the given mathematical notation.

Step 1
1 of 2

To output the last value of an array of 50 components we can use:

cout << alpha[49];


Result
2 of 2
Output the component on position alpha[49].
Question 4:

Step 1
1 of 2

To initialize the heights array of 10 elements we do:

double heights[10] = { 5.2, 6.3, 5.8, 4.9, 5.2, 5.7, 6.7, 7.1, 5.10, 6.0 };
Result
2 of 2
Initialize the heights array as soon as declaring the array.

Step 1
1 of 2

We initialize the weights array the same way as the heights array from 15a:

int weights[7] = {120, 125, 137, 140, 150, 180, 210};


Result
2 of 2
Initialize the weights array the same way as the heights array from 15a
Step 1
1 of 2

We again do this the same way as in 15a:

char specialSimbols[] = { '$', '#', '%', '@', '&', '!', '^' };


Result
2 of 2
We again do this the same way as in 15a but now with the char type.
Step 1
1 of 2

We keep doing the same things as in the last 3, but with a different type of variables and
values.

string seasons[4] = { "fall", "winter", "spring", "summer" };


Result
2 of 2
We keep doing the same things as in the last 3, but with a different type of variables and
values.
Question 5:
Step 1
1 of 3

We will write a program that will:

• Declare an array of doubles alpha with 50 elements


• Using a for-loop traverse the first 25 elements and calculate them as the square
of the index.
• Using a for-loop traverse the next 25 elements and calculate them as the index
multiplied by 3.
• Print each of the elements. If the index is divisible by 10 we will end the line. We
will also use the function setw to format the output in a tabular format.
Step 2
2 of 3
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double alpha[50];
//Initialize the first half as i*i - square of the index
for (int i = 0; i < 25; i++) {
alpha[i] = i * i;
}
//Initialize the second half as 3*i - 3 times the index
for (int i = 25; i < 50; i++) {
alpha[i] = 3 * i;
}
//Print the array - 10 elements per line
for (int i = 0; i < 50; i++) {
if (i % 10 == 0) cout << endl;
cout <<left<<setw(5)<< alpha[i] << " ";
}

}
Result
3 of 3
Traverse the array and set the values of the elements as described in the exercise.
After initializing the array, print it.

Question 6:
Step 1
1 of 3

We need to write a program that will

• Print the messages what the user should do (what to input)


• Input the names and votes, while also adding the votes so we can find the total
and storing the index of the candidate with the most votes
• Print and format the table as requested in the exercise
• We will use the total to find the percentage of the votes and the index of the
candidate with the most votes to print the winner.
Step 2
2 of 3
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int spaces = 20;
int main()
{
cout << "Input the last names of the five candidates and the nubmer of
the votes\n";
string names[5];
int votes[5];
int total = 0;
int indexMax = 0;

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


cout << "Input the last name\n";
cin >> names[i];
cout << "Input the vote count\n";
cin >> votes[i];
total += votes[i];//Calculate the total
if (votes[i] > votes[indexMax]) indexMax = i;//Find the
candidate with the most votes.
}
cout << left << setw(spaces) << "Candidate";
cout << left << setw(spaces) << "Votes Received";
cout << left << setw(spaces) << "% of Total Votes";
for (int i = 0; i < 5; i++) {
cout << endl;
cout << left << setw(spaces) << names[i];
cout << left << setw(spaces) << votes[i];
cout << left << setw(spaces) << (double)votes[i]/total*100;
}
cout << left << setw(spaces) << "\nTotal";
cout << left << setw(spaces) << total << endl;
cout << "The Winner of the Election is " << names[indexMax];
}
Result
3 of 3
Write a program that will enable to user to input the candidates, calculate the averages
and find the winner and print all of the data in a tabular format.

You might also like