Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

(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

(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.

(Polling) The Internet and the web are enabling more people to network, join a cause, voice opinions, and so on. The presidential candidates in 2008 used the Internet intensively to get out their messages and raise money for their campaigns. In this exercise, you'll write a simple polling program that allows users to rate five social-consciousness issues from 1 (least important) to 10 (most important). Pick five causes that are important to you (e.g., political issues, global environmental issues). Use a one-dimensional array topics (of type string) to store the five causes. To summarize the survey responses, use a 5 -row, 10 -column two-dimensional array responses (of type int), each row corresponding to an element in the topics array. When the program runs, it should ask the user to rate each issue. Have your friends and family respond to the survey. Then have the program display a summary of the results, including: a) \(A\) tabular report with the five topics down the left side and the 10 ratings across the top, listing in each column the number of ratings received for each topic. b) To the right of each row, show the average of the ratings for that issue. c) Which issue received the highest point total? Display both the issue and the point total. d) Which issue received the lowest point total? Display both the issue and the point total.

(Print a String Backuard) Write a recursive function stringReverse that takes a string and a starting subscript as arguments, prints the string backward and returns nothing. The function should stop processing and return when the end of the string is encountered. Note that like an array the square brackets ([]) operator can be used to iterate through the characters in a string.

( Sales Summary ) Use a two-dimensional array to solve the following problem. A company has four salespeople ( 1 to 4 ) who sell five different products ( 1 to 5 ). Once a day, each salesperson passes in a slip for cach different type of product sold. Each slip contains the following: a) The salesperson number b) The product number c) The total dollar value of that product sold that day Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information from all of the slips for last month is available. Write a program that will read all this information for last month's sales (one salesperson's data at a time) and summarize the total sales by salesperson by product. All totals should be stored in the two-dimensional array sales. After processing all the information for last month, print the results in tabular format with cach of the columns representing a particular salesperson and each of the rows representing a particular product. Cross total each row to get the total sales of each product for last month; cross total each column to get the total sales by salesperson for last month. Your tabular printout should include these cross totals to the right of the totaled rows and to the bottom of the totaled columns.

(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;

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