In: Computer Science
C++
Q2.
Given the code as below. Which statement is correct?
int myAry[100];
for(int i=0; i<100; i++)
myAry = i + 2;
for(int i=100; i>0; i--)
cout << myAry[i] << '\t';
The first for loop assigns myAry 99 values and the null character. |
||
The second for loop prints out myAry elements backwards. |
||
The index in the second for loop is out of bounds. |
||
Only the first loop needs the null character. |
Q3.
A value returning function that takes one parameter, an integer. The function computes and returns the result of the following formula. The sum stops when the exponent reaches x.
21 + 22 + 23 + 24 + … + 2x
For example, if the parameter sent to the function is 4, the function will return 30, which is equal to 2 + 4 + 8 + 16.
1. (5 points) Write the function definition (header and body)
2. (2 point) Give the function prototype.
3. (2 point) Give an example of the function call (assume it is called by main but you do not need to construct the main function. )
!!! You must show the answer for each question. DO NOT attach a full program. If a full program is your answer for all these three questions, you won't earn any points.
Q4.
Write a full program, including comments!
Suppose you have a file gifts.txt that has many lines with possible Amazon gift names, gift price, and star rating.
(HERE ARE ONLY THE FIRST 4 LINES, THERE ARE MANY MORE IN THE FILE!
EchoDot 22.00 4.6
EchoShow 49.99 4.4
Fire7 29.99 3.9
KindlePaperwhite 110.00 4.3
.
.
.
The first line means that a EchoDot costs $22.00 and has 4.6 stars.
WE DO NOT KNOW HOW MANY LINES THERE ARE IN THE FILE.
Write the C++ code that (reads each line from the file and) prints out the names of all the gifts, the price and the stars.
It also prints a Y if we will buy the gift, and a M if we are not sure (maybe) if we will buy the gift. Our program has a function
that will return Y or M, depending on the price and stars. If the price is less than 30 and stars are more than 4.5 we will buy
the gift. Otherwise we will maybe buy the gift.
The program will print how many gifts we will buy, and how many we might (maybe) will buy.
So if the file was just the four lines above (OF COURSE IT HAS MORE), the output is:
Name Price Stars Buy
EchoDot 22.00 4.6 Y
EchoShow 49.99 4.4 M
Fire7 29.99 3.9 M
Kindle 110.00 4.3 M
Number of gifts we will buy: 1
Number of gifts we will maybe buy: 3
Q5. Write the definition of a void function that takes two
double parameters, say n and m. The function prints out the sum and
average of all the numbers between n and m (inclusive).
!!! Write the definition only. If your answer is a full program,
you don't earn any points.
Q6.
void bookSale(int & a, char & c, double p)
{
if ( c == 'r')
a +=4;
else
{
a *=2;
c = 'r';
}
In this function definition, which statement below is correct?
This function has 3 local parameters. |
||
A and C are reference parameters. |
||
a will change the actual parameter's value when this function is called. |
||
c can not change the actual parameter's value when this function is called because it is just a character. |
2)The index in second loop is out of bounds because it starts at 0 and ends 99
Option 3
3)
i)prototype:function_name(int x)
Definition:
function_name(int x)
{
double sum=0;
for(int i=1;i<=n;i++)
sum+=pow(2,i);//we should include cmath library for this
return sum;
}
Function call
res=function_name(5);
4)
#include <iostream>
#include<fstream>
using namespace std;
char func(double price,double rating)
{
if(price<30&&rating>4.5)
return 'Y';
else
return 'M';
}
int main()
{
string name;
char ch;
double price,rating;
ifstream f1;
f1.open("file.txt");//open file
while(f1>>name)//read each of them separately
{
f1>>price;
f1>>rating;
ch=func(price,rating);//call function
cout<<name<<" "<<price<<"
"<<rating<<" "<<ch<<endl;
}
return 0;
}
5)
void function_name(double n,double m)
{
double sum=0,avg;
for(double i=n;i<=m;i++)//double double or int any one
{
sum+=i;
}
avg=sum/(m-n+1);
cout<<sum<<" "<<avg;
}
6)a will change the actual parameter's value when this function is called. because it is a reference variable Option 3