In C++ 1. Look at the following array definition. double sales[8][10]; a) How ma
ID: 3574488 • Letter: I
Question
In C++
1. Look at the following array definition.
double sales[8][10];
a) How many rows does the array have?
b) How many columns does the array have?
c) How many elements does the array have?
d) Write a statement that stores 3.52 in the last column of the last row in the array.
2. A weather analysis program uses the following array to store the temperature for each hour of the day on each day of a week.
int temp[7][24];
Each row represents a day (0 = Sunday, 1 = Monday, etc.) and each column represents a time (0 = midnight, 1 = 1 a.m., ..., 12 = noon, 13 = 1 p.m., etc.).
A) Write code to find Tuesday’s average temperature.
B) Write code to find the average weekly noon temperature.
3. Each of the following functions contains errors. Locate as many as you can.
A)
void showValues(int nums)
{
for (int count = 0; count < 8;
count++) cout << nums[count];
}
B)
}
void showValues(int nums[4][])
{
for (rows = 0; rows < 4; rows++)
for (cols = 0; cols < 5; cols++)
cout << nums[rows][cols];
4. What is the output of the following program? (You may need to consult the ASCII table at http://www.asciitable.com/.
#include <iostream>
using namespace std;
// Function prototypes
void fillArray(char [], int);
void showArray(const char [], int);
int main ()
{ char prodCode[8] = {'0', '0', '0', '0', '0', '0', '0', '0'};
fillArray(prodCode,8);
showArray(prodCode,8);
return 0;
}
// Definition of function fillArray
// (Hint: 65 is the ASCII code for 'A'.)
void fillArray(char arr[], int size)
{ char code = 65;
for (int k = 0; k < size; k++) arr[k] = code++;
}
// Definition of function showArray
void showArray(const char codes[], int size) { for(intk=0;k<size;k++)
cout << codes[k];
}
5. What will each of the following program segments display?
A) x=2;
y = x++;
cout <<x<<""<<y;
B) x=2;
y = ++x;
cout <<x<<""<<y;
C) x=2;
y = 4;
cout <<x++<<""<<--y;
D) x=2;
y = 2*x++;
cout <<x<<""<<y;
E) x=99;
if (x++ < 100)
cout "It is true! ";
else
cout << "It is false! ";
F) x=0;
if (++x)
cout << "It is true! "; else
cout << "It is false! ";
6. Assuming dataFile is an ofstream object associated with a disk file named payroll.dat, which of the following statements would write the value of the salary variable to the file?
A) cout << salary;
B) ofstream << salary;
C) dataFile << salary;
D) payroll.dat << salary;
7. Assume you have an output file named numbers.txt that is open and associated with an ofstream object named fout. Write a program segment that uses a ‘for’ loop to write the numbers 1 through 10 to the file.
8. Write a program that reads in from a file a starting month name, an ending month name, and then the monthly rainfall for each month during that period. As it does this, it should sum the rainfall amounts and then report the total rainfall and average rainfall for the period. For example, the output might look like this:
During the months of March–June the total rainfall was 7.32 inches and the average monthly rainfall was 1.83 inches.
The sample contents of Rainfall.txt file: June
September
2.35 1.15 2.03 1.41
Hint: After reading in the month names, you will need to read in rain amounts until the EOF is reached, and count how many pieces of rain data you read in.
2. A weather analysis program uses the following array to store the temperature for each hour of the day on each day of a week.
int temp[7][24];
Each row represents a day (0 = Sunday, 1 = Monday, etc.) and each column represents a time (0 = midnight, 1 = 1 a.m., ..., 12 = noon, 13 = 1 p.m., etc.).
A) Write code to find Tuesday’s average temperature.
B) Write code to find the average weekly noon temperature.
Explanation / Answer
1. a) No of rows = 8
No of columns = 10
Number of elements = 8*10 = 80
sales[7][9]=3.52
2.
double sum = 0;
for (int i=0; i<24; i++){
sum+= temp[1][i];
}
ans = sum/24;
sum = 0;
for (int i=0; i<7; i++){
sum+=temp[i][12];
}
ans = sum/7;
3. A) nums is of type int. it should be an array.
B) Need to know the size of the columns
5. A)2
B) 2
C) 3
D) 6
E) It is false