(Write \(C++\) Statements) 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. c) 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.

Short Answer

Expert verified
const int arraySize = 10;double fractions[arraySize] = {0};Element Name: fractions[3];Element Reference: fractions[4];fractions[9] = 1.667;fractions[6] = 3.333;std::cout << std::fixed << std::setprecision(2) << fractions[5] << ' ' << fractions[9] << std::endl;for(int i = 0; i < arraySize; ++i) { std::cout << fractions[i] << ' '; } std::cout << std::endl;

Step by step solution

01

- Define a constant integer variable

To start, define a constant integer variable named `arraySize` and initialize it to 10. This defines the size of the array called fractions. In C++, this is done using the `const` keyword.Example: const int arraySize = 10;
02

- Declare and Initialize the Array

Next, declare an array of type double with `arraySize` elements. Initialize all elements to 0 using the syntax for aggregate initialization in C++.Example: double fractions[arraySize] = {0};
03

- Name the Fourth Element

In C++ arrays, indexing starts at 0. Therefore, the fourth element is at index 3. Simply refer to it as fractions[3].
04

- Refer to Array Element 4

The fifth element in the array (commonly thought to be element 4 due to zero-based indexing) is referred to with index 4. Use fractions[4] to access it.
05

- Assign Value to Ninth Element

Assign the value 1.667 to the element at index 9, which is the tenth element in the array.Example: fractions[9] = 1.667;
06

- Assign Value to the Seventh Element

Assign the value 3.333 to element 6 (the seventh element, since indices are zero-based).Example: fractions[6] = 3.333;
07

- Print Specific Elements with Precision

Print the values of elements 5 (actual 6th element) and 9 (actual 10th element) with two digits of precision using the iostream `setprecision` manipulator from the library.Example: std::cout << std::fixed << std::setprecision(2) << fractions[5] << ' ' << fractions[9] << std::endl;
08

- Print All Array Elements Using a Loop

Use a `for` loop with the control variable `i` to iterate over the array elements. Print each element in the loop.Example:for(int i = 0; i < arraySize; ++i) { std::cout << fractions[i] << ' ';}std::cout << std::endl;

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

C++ array initialization
In C++, an array is a collection of items stored at contiguous memory locations. To effectively use these arrays, you must first initialize them. That's where array initialization comes in, which is best thought of as setting a starting point. Consider array initialization as the act of giving your variables a value before they're used.

For example, when you have an arraySize defined, you can declare an array of that size and set all the elements to zero like this:
double fractions[arraySize] = {0};
This statement uses aggregate initialization, which is a way to initialize an array with a list of values. In this case, only the first element is explicitly set to zero, and all other elements automatically take the same value due to default initialization in C++.
Array indexing in C++
Understanding array indexing is like knowing how to find a book in a library. Arrays are zero-indexed, which means the first element has an index of 0, the second element has an index of 1, and so on.

Let's take the array fractions as an example; to access the fourth element, you'd use fractions[3]. Remember, it might seem counterintuitive at first, but indexing starts at 0, so you always subtract one from the human-friendly 'position' to get the correct index in code.
C++ for loops
Just like a runner goes around the track loop, a for loop in C++ allows you to repeatedly execute a block of code. This is an exceptional tool for iterating over arrays. When you wish to print all elements of the fractions array, you set up a loop that starts with an index at 0 and runs until it reaches the final index, which is the size of the array minus one.

Here’s an example:
for(int i = 0; i < arraySize; ++i) {
std::cout << fractions[i] << ' ';
}
std::cout << std::endl;

This loop makes use of a control variable i, which is incremented after each iteration, allowing the loop to access each consecutive array element.
C++ setprecision
Imagine you need the exact amount of ingredients to make a perfect cake – not an approximate. In C++, when printing floating-point numbers, the setprecision manipulator from the <iomanip> library helps you control the number of digits printed after the decimal point. If you want to display numbers with two digits of precision, you would use it like so:

std::cout << std::fixed << std::setprecision(2) << fractions[5] << ' ' << fractions[9] << std::endl;
Using setprecision in tandem with std::fixed ensures that the number is printed in fixed-point notation, providing clarity and precision in representation, necessary for accurate calculations or display.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

(Double Array Initialization) Label the elements of a 3 -by- 5 two-dimensional array sales to indicate the order in which they're set to zero by the following program segment: for ( row = 0; row < 3; ++row ) for ( column = 0; column < 5; ++column ) sales[ row ][ column ] = 0;

(Single Array Questions) Write single statements that perform the following one-dimensional array operations: a) Initialize the 10 elements of integer array counts to zero. b) Add 1 to each of the 15 elements of integer array bonus. c) Read 12 values for double array month 7 yTemperatures from the keyboard. d) Print the 5 values of integer array bestscores in column format.

(Fill in the Blanks) Fill in the blanks in each of the following: a) The names of the four elements of array p (int p[4];) are ___, ___ , ___ and ___. b) Naming an array, stating its type and specifying the number of elements in the array is called __ the array. c) By convention, the first subscript in a two-dimensional array identifies an element's ___ and the second subscript identifies an element's ___. d) An m-by-n array contains __ rows, __ columns and ___ elements. c) The name of the element in row 3 and column 5 of array \(d\) is __.

(Palindromes) A palindrome is a string that is spelled the same way forward and backward. Examples of palindromes include "radar" and "able was i ere i saw elba." Write a recursive function testPalindrome that returns true if a string is a palindrome, and false otherwise. Note that like an array, the square brackets ( [] ) operator can be used to iterate through the characters in a string.

(Dotable Array Questions) Consider a \(2-\mathrm{by}-3\) integer array t. a) Write a declaration for t. b) How many rows does t have? c) How many columns does t have? d) How many elements does t have? e) Write the names of all the elements in row 1 of \(t\) f) Write the names of all the elements in column 2 of t. g) Write a statement that sets the element of t in the first row and second column to zero. h) Write a series of statements that initialize each element of t to zero. Do not use a loop. i) Write a nested for statement that initializes each element of to zero. j) Write a statement that inputs the values for the elements of trom the keyboard. k) Write a series of statements that determine and print the smallest value in array t. l) Write a statement that displays the elements in row 0 of t. \(\mathrm{m}\) ) Write a statement that totals the elements in column 3 of t. n) Write a series of statements that prints the array t in neat, tabular format. List the column subscripts as headings across the top and list the row subscripts at the left of each row.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free