C++ Homework Help
C++ Homework Help
Problems
Problem 1.
struct data
{
float z;
char type;
};
int main()
{
data D1 = {20, 'P’};
cout << D1.z++ << D1.type << endl;
data D2;
D2.type = D1.type;
D2.z = 4 * D1.z;
Cout << ++D2.z << D2.type << endl;
Cout << D1.z-- <<D1.type;
return 0;
}
Problem 2.
class member
{ int memberNum = 25;
float memberPay;
public
void Input(cin >> memberNum >> memberPay);
void Output;
}
int main()
{ Member mem;
cin >> mem.memberNum >> mem.memberPay;
cout << mem.memberNum << '\t' << mem.memberPay;
}
void Output::Member()
{
cout << mem.memberNum << '\t' << mem.memberPay;
}
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Problem 3.
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
cpphomeworkhelp.com
reach us at : - https://www.cpphomeworkhelp.com/
vii. To withdraw money from the account. It should do
what the previous case did with a similar argument named
amountToWithdraw. However, before doing the
withdrawal, it should make sure that the balance after
withdrawing will not drop below $500. It should return
the amount actually withdrawn (0 if no money was
withdrawn).
viii. To return the Balance Amount
ix. To print all the data members.
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Problem 4.
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
a. CalculateAvg() that returns the average of the students’
grades.
a. Assign() that asks the user to input the ID, Name and
Grades and sets the average equal to the value returned
after calling the Calculate() function.
b. Output() that outputs all of the data on to the screen
Problem 5.
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
other code making its internal data inconsistent (say, a
SchoolStudent’s average being set to a number not
reflective of the grades). To selectively allow access, classes
often have “getter” and “setter” functions. For example, to
allow users of the Employee class to retrieve and modify
the employeeId, we might define:
class Employee {
…
int getId() { return Id; }
void setId(int newId) { Id = newId; }
…
};
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
1. Assigning values to all the elements of the array
(Calling the Assign() function for each of the objects)
2. Sorting the array of class objects on the basis of
average using bubble sort method (you will have to call
your average getter in the if-statement of the sort)
3. Displaying the array elements (You will have to call
the Output() function for each object)
Problem 6.
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
function, and you were adding the code below to the init
function of RectDrawing, what would you have to put
in the blank line below to solve this problem?
if( hasBeenInitialized() ) {
// Insert your code here
}
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Solutions
Solution 1.
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Solution 2.
class member
{
int memberNum;
float memberPay;
public:
void Input(){cin >> memberNum >> memberPay};
void Output();
};
int main()
{
Member mem;
mem.Input();
mem.Output();
return 0;
}
void Member::Output()
{
cout << memberNum << '\t' << memberPay;
}
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Solution 3.
class Account
{
long AccNum;
string Depositor;
char AccType;
double Amount;
public:
void Input();
double Deposit( double amountToDeposit);
double Withdraw (double amountToWithdraw);
double retBalance();
void Output();
}
void Account::Input()
{
cin >> AccNum >> Depositor >> AccType >> Amount;
}
void Account::Output()
{
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
cout << AccNum << ":" << Depositor << ":"
<< AccType << ":" << Amount << endl;
}
double Account::Deposit (double
amountToDeposit)
{
Balance += amountToDeposit;
return Balance;
}
double Account::Withdraw (double
amountToWithdraw)
{
if(Balance-amountToWithdraw >= 500)
{
Balance -= amountToWithdraw;
return Balance;
}
else
{
cerr << "Not allowed to withdraw" << endl;
return 0;
}
}
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
double Account::retBalance()
{
return Balance;
void Input(Account &Acc1, Account Acc2)
{
Acc1.Input();
Acc2.Input();
}
Account CompareBal(Account &Acc1, Account
&Acc2)
{
if(Acc1.retBalance()>Acc2.retBalance())
return Acc1;
else
return Acc2;
}
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Solution 4.
class SchoolStudent
{
private: // Optional
long studentID;
string studentName;
float average;
double grades[8];
float CalculateAvg();
public:
void Assign();
void Output();
};
void SchoolStudent::Assign()
{
cin >> studentID >> studentName;
for(int i=0;i> grades[i];
average = CalculateAvg();
}
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
float SchoolStudent::CalculateAvg()
{
float sum=0;
for(int i =0; i < 8; i++)
sum += grades[i];
return sum/8.0;
}
void SchoolStudent::Output()
{
cout << studentID << ":" << studentName << ":"
<< average << ":";
for(int i = 0; i < 8; i++)
cout << grades[i];
}
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Solution 5.
float SchoolStudent::GetAvg()
{
return average;
}
void Assign( SchoolStudent *Student, int n)
{
for(int i=0;
Student[i].Assign();
}
void Sort (SchoolStudent *Student, int n)
{
for(int i=0;I <5;i++)
{
for( int j=0;j<i;j++)
{
if(Student[i].GetAvg() > Student[j].GetAvg())
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
{
Student temp=Student[i];
//swap
Student[i]=Student[j];
Student[j]=temp;
}
}
}
}
void Display(SchoolStudent
*Student, int n)
{
for(int i = 0; i < n; i++)
Student[i].Output();
}
int main()
{
int n;
cout << "Enter the number of
students:";
cin >> n;
SchoolStudent *Student1 =
new
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
SchoolStudent[n];
Assign(Student1, n);
Sort(Student1, n);
Display(Student1, n);
}
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Solution 6.
class Rectangle
{
double x[4], y[4];
public:
void init();
};
Rectangle::init()
{
for (int i = 0; i < 4; i++)
cin >> x[i] >> y[i];
}
class RectDrawing
{
Rectangle *ArrRect;
public:
void init();
};
void Rectdrawing::init()
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
{
int n;
cin >> n;
ArrRect = new Rectangle[n];
}
void RectDrawing::init()
{
if( hasBeenInitialized() )
delete[] ArrRect;
int n;
cin >> n;
Rectangle *ArrRect= new Rectangle[n];
}