In: Computer Science
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];
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];
}
}