Write one or more statements that perform the following tasks for an array calle
ID: 3734229 • Letter: W
Question
Write one or more statements that perform the following tasks for an array called fractions:
a) Define a constant integer variable arraySize initialized to 10. b) Declare an array with arraySize elements of type double, and initialize the elements to 0. c) Name the fourth element of the array. d) Refer to array element 4. e) Assign the value 1.667 to array element 9. f) Assign the value 3.333 to the seventh element of the array. g) Print array elements 6 and 9 with two digits of precision to the right of the decimal point, and show the output that is actually displayed on the screen. h) Print all the array elements using a for statement. Define the integer variable i as a control variable for the loop. Show the output.
Explanation / Answer
a) Define a constant integer variable arraySize initialized to 10.
Answer: const int arraySize = 10;
b) Declare an array with arraySize elements of type double, and initialize the elements to 0.
Answer: double a[arraySize] = {0,0,0,0,0,0,0,0,0,0};
c) Name the fourth element of the array. Answer: a[3]
d) Refer to array element 4.
Answer: a[3]
e) Assign the value 1.667 to array element 9.
Answer: a[8] = 1.667;
f) Assign the value 3.333 to the seventh element of the array.
Answer: a[6] = 3.333
g) Print array elements 6 and 9 with two digits of precision to the right of the decimal point, and show the output that is actually displayed on the screen.
Answer:
cout<<fixed<<setprecision(2)<<a[5]<<" "<<a[8]<<endl;
h) Print all the array elements using a for statement. Define the integer variable i as a control variable for the loop.
Answer:
for(int i=0;i<arraySize;i++) {
cout<<a[i]<<" ";
}
cout<<endl;
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
const int arraySize = 10;
double a[arraySize] = {0,0,0,0,0,0,0,0,0,0};
a[8] = 1.667;
a[6] = 3.333;
cout<<fixed<<setprecision(2)<<a[5]<<" "<<a[8]<<endl;
for(int i=0;i<arraySize;i++) {
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}
Output:
0.00 1.67
0.00 0.00 0.00 0.00 0.00 0.00 3.33 0.00 1.67 0.00
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.