MIDTERM MAC-125 15 Questions : 45 possible points : 3 points each question Suppo
ID: 3912755 • Letter: M
Question
MIDTERM MAC-125 15 Questions : 45 possible points : 3 points each question Suppose your program contains the following class definition class Automobile public void setPrice (double newPrice) void setProfit (double newProfit) double getPrice( private: double price double profit; double getProfit 1: and suppose the main function of your program contains the following hat the program somehow sets the values of all the member variables to some values: Automobile hyundai, jaguar: Which of the following statements are then allowed in the main function of your program? hyundai.price 4999.99 jaguar.setPrice (30000.97) double aPrice, aProfit aPrice jaguar.getPrice) aProfit jaguar.getProfit() aProfit hyundai.getProfit) hyundai jaguar ; 2. (think nested loop, remember the grid) Write code that will fill the array a (declared next) with numbers typed in at the keyboard. The numbers will be input five per line, on four lines (although your solution need not depend on how the input numbers are divided into lines). int a (4] (s)Explanation / Answer
1. Solution
hyundai.price=4999.97 // Not allowed, because price is private member
jaguar.setPrice(30000.97) // Allowed
double aPrice,aProfit
aPrice=jaguar.getPrice(); // Allowed
aProfit=jaguar.getProfit(); // Not allowed, because getProfit() is private method
aProfit=hyundai.getProfit(); // Not allowed, because getProfit() is private method
hyundai=jaguar; // Allowed
2. Solution
#include<iostream>
using namespace std;
int main()
{
int a[4][5]; // declare 2d integer array
int num; // declare integer variable num for reading input
for(int i=0;i<4;i++){ // create outer for loop i for 4 lines
for(int j=0;j<5;j++){ // create inner for loop j for 5 elements in each line
cout<<" Enter ["<<i<<","<<j<<"]"<<" Number: ";
cin>>num; // reading numbers
a[i][j]=num; // assign num to 2d array
}
}
cout<<" The Grid is"<<endl;
for(int i=0;i<4;i++){ // create for loop i for 4 lines
for(int j=0;j<5;j++){ // create for loop j for 5 elements in each line
cout<<a[i][j]<<" "; // display first line and next so on until 4 lines
}
cout<<endl; // control goes next line(After print 5 elements then control goes to next line)
}
return 0;
}
OUTPUT
Enter [0,0] Number: 1
Enter [0,1] Number: 2
Enter [0,2] Number: 3
Enter [0,3] Number: 4
Enter [0,4] Number: 5
Enter [1,0] Number: 6
Enter [1,1] Number: 7
Enter [1,2] Number: 8
Enter [1,3] Number: 9
Enter [1,4] Number: 10
Enter [2,0] Number: 11
Enter [2,1] Number: 12
Enter [2,2] Number: 13
Enter [2,3] Number: 14
Enter [2,4] Number: 15
Enter [3,0] Number: 16
Enter [3,1] Number: 17
Enter [3,2] Number: 18
Enter [3,3] Number: 19
Enter [3,4] Number: 20
The Grid is
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
INSTRUCTOR<<M.MULLER
1. SOLUTION
#include<iostream>
using namespace std;
void output(const double a[],int size)
{
for(int i=0;i<size;i++){
cout<<a[i]<<" ";
}
}
void dropOdd(const int a[],int size)
{
int j=0;
for(int i=0;i<size;i++){
if(a[i]%2!=0)
cout<<a[i-1]<<" ";
}
/* for(int i=0;i<size;i++)
cout<<a[i]<<" ";*/
}
int main()
{
double x[10];
int a[10];
for(int i=0;i<3;i++)
cin>>x[i];
cout<<"Output: ";
output(x,3);
for(int i=0;i<10;i++)
cin>>a[i];
dropOdd(a,10);
return 0;
}
OUTPUT
4 5 6
Output: 4 5 6
1
2
3
4
5
6
7
8
9
10
0 2 4 6 8
2. Solution
class NodeType
{
public:
NodeType(){}
NodeType(char c,NodeType *link):ch(c),next(link){}
NodeType *getNext() const { return next;}
char getCh() const { return ch;}
void setCh(char c) { c=ch;}
void setNext(NodeType *ptr){next=ptr;}
private:
char ch;
NodeType *next;
};
typedef NodeType *PointerType;
3. Solution
#include<iostream>
using namespace std;
class Temperature
{
public:
void set(double newDegrees,char newScale)
{
degrees=newDegrees;
scale=newScale;
}
double getDegree()
{
return degrees;
}
char getScale()
{
return scale;
}
double degrees;
char scale;
};
int main()
{
Temperature t;
t.set(3.4,'F');
cout<<t.getDegree()<<" "<<t.getScale()<<endl;
return 0;
}
OUTPUT
3.4 F
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.