Question

In: Computer Science

6 C++ Questions 13. True/False: every while loop is guaranteed to execute at least one time....

6 C++ Questions

13. True/False: every while loop is guaranteed to execute at least one time.

14. Assume there is a file named "cards.txt" in the current directory which contains 3 ints. Write a code snippet which reads in the three ints and outputs their average (mean) to the screen.

15. True/False: the following function prototype takes an array and its size as its parameters. If you change the value of the array's elements within the function, the changes persist after the function returns.

 void sortCards(int cards[], int size);

16a. Write a function which takes an int array and its size as parameters. It returns an int indicating how many multiples of 3 are contained in the array. For example, if the array contains {2, 6, 8} your function should return 1. If the array contains {3, 10, 5, 6} your function should return 2. Here is the function prototype:

// int array[]: array to search
// size: number of elements in the array
int countMult(const int array[], int size);

16b. Demonstrate that your function from 16a works by writing a main() function which declares an array of ints, gives the array some values, calls the function with the array and its size as parameters, and prints out the function's return value.  

17. Consider the following code snippet. Will it compile? Is there anything wrong with it?

int cards[3];
cards[1] = 2;
cards[2] = 4;
cards[3] = 7;

18. Assume the 2D array below has been assigned valid integer values. NUM_PLAYERS and NUM_CARDS are global const ints with positive values. Write a code snippet which computes the total of all the elements in the 2D array.

 int cards[NUM_PLAYERS][NUM_CARDS];

Solutions

Expert Solution

13.

False

Explanation:

Every do while loop is guaranteed to execute one time not while loop. While loop only executes till the condition satisfies.

14.

// declaring variables
int x,sum=0;
// declaring variable to store average
double average;

//declaring an input filestream variable
    ifstream file;

// opening the file
    file.open("cards.txt");

   // checking if file is opened, if not terminate the program
    if (!file)
     {
        cout << "Unable to open file";
        exit(1); // terminate with error
    }
  
    // reading inout from file just like reading from cin
    while (file >> x) {
        // adding all the values together
        sum = sum + x;
    }

   // calculating average
    average = (double)sum / 3;
  
    // closing the file
    file.close();

     // printing the average
    cout <<average<<endl;

15.

True

Explanation:

whatever changes we make in the array inside the function will persist even after control comes out of the function, because changes happen at address of the array.

16.

a)

int countMult(const int array[], int size)
{   // count variable to store the number of values divisible by 3
    int count = 0;

    // loop to iterate through array one by one
    for(int i =0;i<size;i++)
    {
        // checking if the element is divisible by 3
        if(array[i]%3==0)
            // if element is divisibel by 3 increasing the count by 1
            count++;
    }
    //returning the count of values divisible by 3
    return count;
}

b)

#include<iostream>
using namespace std;
int countMult(const int array[], int size)
{   // count variable to store the number of values divisible by 3
    int count = 0;

    // loop to iterate through array one by one
    for(int i =0;i<size;i++)
    {
        // checking if the element is divisible by 3
        if(array[i]%3==0)
            // if element is divisibel by 3 increasing the count by 1
            count++;
    }
    //returning the count of values divisible by 3
    return count;
}


int main()
{
    // declaring and initialising array
int array[] = {3,10,5,6};
// calculting size of array
int size = sizeof(array)/sizeof(array[0]);

// calling the function with the appropriate parameters and printing the value
cout<<countMult(array,size)<<endl;
}

17.

Yes, it will compile but desired output won't come.

the index values are not proper, cards[3] tries to access array out of bound, since there are only 3 value that array cards can store. thus index will start from 0 and will end at 2.

18.

int cards[NUM_PLAYERS][NUM_CARDS];
/* sum is initialised to zero that wil store the sum of all elements in array */
long long int sum =0;

// iterating through the 2d array
for(int i =0; i< NUM_PLAYERS; i++)
    {for(int j=0; j< NUM_CARDS; j++)
        {
           // adding the elements together and storing the result in sum
           sum = sum + cards[i][j];

           }
             }


Related Solutions

True or False: It's easy to loop through an array using a for loop in C++...
True or False: It's easy to loop through an array using a for loop in C++ because you can use the .size() function to get the total number of spaces in the array True or False: It's easy to loop through a vector using a for loop in C++ because you can use the .size() function to get the total number of spaces in the vector
A student takes an exam containing 13 true or false questions. At least 9 correct answers...
A student takes an exam containing 13 true or false questions. At least 9 correct answers are required to pass. If the student guesses, what is the probability that he will pass? Round your answer to four decimal places.
True/False Questions. For each question below, please answer “true” or “false” and explain why. 6. A...
True/False Questions. For each question below, please answer “true” or “false” and explain why. 6. A consumer with convex preferences who is indifferent between the bundles (5,2) and (11,6) will like the bundle (8,4) at least as well as either of the first two bundles. Assume these two goods are imperfect substitutes. 7. The marginal rate of substitution is always the same constant number when the goods are imperfect substitutes and no matter how many of each good is being...
PART 6: True or false and multiple-choice questions Question 16: True or false, firms in situation...
PART 6: True or false and multiple-choice questions Question 16: True or false, firms in situation of monopolistic competition have deadweight losses associated with monopoly because of barriers to entry. Question 17: Which of the two demands functions below is most elastic A) Qa=100-2p B) Qb=120-p Question 18: If a buyer values an object for $4 and a seller’s cost is $1, which of the statements below is false! If a transaction takes place, total surplus will be equal to...
Question 13 (1 point) Every invertible matrix is diagonalizable. Question 13 options: True False Question 14...
Question 13 (1 point) Every invertible matrix is diagonalizable. Question 13 options: True False Question 14 (1 point) Every diagonalizable matrix is invertible. Question 14 options: True False
Several psychology students are unprepared for a surprise​ true/false test with 13 ​questions, and all of...
Several psychology students are unprepared for a surprise​ true/false test with 13 ​questions, and all of their answers are guesses. a. Find the mean and standard deviation for the number of correct answers for such students. b. Would it be unusual for a student to pass by guessing​ (which requires getting at least 11 correct​ answers)? Why or why​ not?
True false questions Neuron cell bodies are only found in CNS but not in PNS, while...
True false questions Neuron cell bodies are only found in CNS but not in PNS, while axons are the opposite. True False If the threshold for AP is set at -60mV, a neuron receives total 8 EPSPs of -65mV and 6 IPSPs of -75mV, after the summation, this neuron will launch an AP.  True  False Sodium-potassium exchange pump is the key factor in maintaining the concentration gradients of the cations across the plasma membrane. True  False The resting potential, -70mV, being close to...
True/false Every ratio tells us for every one of​ what’s on the​ top, here’s how many...
True/false Every ratio tells us for every one of​ what’s on the​ top, here’s how many we have of​ what’s on the bottom. True or​ False:Ceteris paribus​, the cash coverage ratio and the rate of interest paid on debt are inversely related. True or​ False:  An increase in the cash coverage ratio means that a firm is less likely to default on its outstanding debt. True/false Every ratio tells us for every one of​ what’s on the​ top, here’s how...
C++ Questions: 1. True/False: an int uses 4 bytes in memory on all systems. 2. True/False:...
C++ Questions: 1. True/False: an int uses 4 bytes in memory on all systems. 2. True/False: a double typically offers better precision than a float. 3. True/False: The following two statements are equivalent assuming isPair is a variable of type bool: if (isPair) if (isPair == true) 4. Provide a value we could put in the blank so that the value of x will be 20 after the code snippet. int x = _____; x *= 4; 5. Provide a...
Answer all questions with a true or false Communication between provider and consumers affects every facet...
Answer all questions with a true or false Communication between provider and consumers affects every facet on the health continuum from health promotion and disease prevention to assessment, diagnosis, and treatment. Treatment adherence leads to better health out- comes and no adherence places the patient at risk for poor recovery and dis- ease progression. Treatment adherence refers to not only medication adherence but also compliance with appointment schedules, diet and exercise regimens, or lifestyle modifications. The relationship of trust between...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT