Question

In: Computer Science

C++ Q2.   Given the code as below. Which statement is correct? int myAry[100]; for(int i=0; i<100;...

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.

Solutions

Expert Solution

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


Related Solutions

Consider the following fragment of C code: for (i=0; i<100; i++) { A[i]=B[i]+C; } Assume that...
Consider the following fragment of C code: for (i=0; i<100; i++) { A[i]=B[i]+C; } Assume that A and B are arrays of 64-bit integers, and C and i are 64-bit integers. Assume that all data values and their addresses are kept in memory (at addresses 1000, 3000, 5000, and 7000 for A, B, C, and i, respectively) except when they are operated on. Assume that values in registers are lost between iterations of the loop. Assume all addresses and words...
Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
Show the output of the following code segment. int count=0;                         for (int i=2; i <= 4; i++ ) {                                     StdOut.println(i);                                     for (int j=1; j <3; j++) {                                                 count++;                                                 StdOut.println(i +" " + j +" "+ count);                                     }                         } count i j I print 0 2 1 3 1 1 1 2 3 2 2 3 3 3 3 4 3 Show the output of the function call statements shown.             double x =...
How do you translate this pseudocode go regular code in C++ int iMin = 0,i =...
How do you translate this pseudocode go regular code in C++ int iMin = 0,i = 0; for(j = 0; j < n - 1; j++) int iMin = j; for(i = j + 1; i < n; i++) if(a[i] < a[iMin]) iMin = i; if(iMin != j) swap(a[i], a[iMin]);
Given the root C++ code: void sort() {    const int N = 10;    int...
Given the root C++ code: void sort() {    const int N = 10;    int x[N];    for(int i = 0; i < N; i++)    {        x[i] = 1 + gRandom-> Rndm() * 10;        cout<<x[i]<<" "; }    cout<<endl;    int t;       for(int i = 0; i < N; i++)    {    for(int j = i+1; j < N; j++)    {        if(x[j] < x[i])        {   ...
The code below is giving an arithmetic overflow. Correct the below given the below code, so...
The code below is giving an arithmetic overflow. Correct the below given the below code, so that the value of s0 is printed before calling the function fun1, inside the function Fun1 and after returning from Fun1 in main. Upload the corrected .asm or .s file in the drop box. .text main: addi $s0,$zero,2 jal Disp jal Fun1 jal Disp j Exit Fun1: addi $sp,$sp,-4 sw $s0,0($sp) addi $s0,$s0,15 jal Disp lw $s0,0($sp) addi $sp,$sp,4 jr $ra Disp: li $v0,1...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i <n; ++ i) {cout << "j =" + j; j = j + 5; }}
Which statement below is most correct?
 Which statement below is most correct? a) The economic theory of the firm focuses upon explaining firms' decision making by assuming firms are primarily motivated by attempting to maximize their contribution to general public's social welfare. b) The theory of the firm holds that the primary goal of a firm is to maximize the discounted present value of the positive difference between the firm's total revenue and the firm's total cost or to minimize the present value of the negative difference between the firm's total revenue...
What is the Θ( ) for each code segment below? (a) for( int i = 1,...
What is the Θ( ) for each code segment below? (a) for( int i = 1, i<= n, i = i*2){       some constant operation } (b) for( int i = 1, i<= n, i++){      for( int j = 2*i, j<=n, j++){           some constant operation      } } (c) for( int i = 1, i<= n, i = i*2){      for( int j = 1, j<=n, j = j*2){           some constant operation      } }
Given the following code: for (i=2;i<100;i=i+1) { a[i] = b[i] + a[i]; /* S1 */ c[i-1]...
Given the following code: for (i=2;i<100;i=i+1) { a[i] = b[i] + a[i]; /* S1 */ c[i-1] = a[i] + d[i]; /* S2 */ a[i-1] = 2 * b[i]; /* S3 */ b[i+1] = 2 * b[i]; /* S4 */ } a. List all the dependencies by their types (TD: true-data, AD: anti-data, OD: output-data dependencies). b. Show how Software Pipelining can exploit parallelism in this code to its fullest potential. How many functional units would be sufficient to achieve the...
(C++)Change the following loop to a while loop: int i; for (i=0; i<10; i++) {    ...
(C++)Change the following loop to a while loop: int i; for (i=0; i<10; i++) {     cout<<i<<endl; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT