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 statements that perform the following one-dimensional-array operations: a) Set the 10 elements of integer array counts to zero. b) Add one to each of the 15 elements of integer array bonus. c) Display the five values of integer array bestscores in column format.

Short Answer

Expert verified
a) Use a for loop to set each element of 'counts' to zero. b) Use a for loop to increment each element of 'bonus' by 1. c) Use a for loop to print each element of 'bestscores' on a new line.

Step by step solution

01

Initialize Array 'counts' to Zero

To set the 10 elements of the integer array 'counts' to zero, we can use a loop that iterates through each element of the array and assigns the value zero to it. In C++, this would typically be done using a for loop, iterating from 0 to 9 (array indices start at 0 and end at array size - 1), and using the array index to set the value.
02

Increment Elements of Array 'bonus'

To add one to each element of the 15-element integer array 'bonus', we can similarly use a for loop. This time, we iterate from 0 to 14 and increment the value of each element by 1. The increment operation can be done by using the '++' operator.
03

Display Array 'bestscores'

To display the values of the integer array 'bestscores' in a column format, a for loop can be used to go through each of the 5 elements, and print them on a separate line. This can be done in any language with a looping construct and a print or output function. In C++, you’d use 'std::cout' for console output, and you can ensure each value appears on a new line by using the newline character ''.

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.

Array Initialization
Array initialization is the process of assigning values to an array when it is declared. The goal is to provide the array with a default set of values before we start using it. In the exercise, setting the 10 elements of the array 'counts' to zero initializes each slot in the array with a starting value. To do this in code, a common technique is to use a loop that goes over each index and sets the value to zero. For example, in C++:

for(int i = 0; i < 10; i++) {    counts[i] = 0;}

This method ensures that all elements of 'counts' start at the same value, which is crucial when performing calculations or operations on the array later on. Initializing arrays at declaration is also possible using specific syntax, depending on the programming language.
Looping through Arrays
Looping through arrays allows you to access each element in the array sequentially. For each step in our sample exercise, we used a form of a loop. When you loop through an array, you commonly use a for loop and an index to keep track of your position within the array. The index starts at 0 because arrays are zero-indexed, meaning the count starts from zero, not one. Here’s how you generally use a 'for' loop to access array elements:

for(int i = 0; i < arrayLength; i++) {    // Operations on array[i]}

This structure is vital for performing any operation that needs to touch each element of the array, such as reading the values, updating them, or performing calculations. Properly understanding and using loops is crucial in programming with arrays.
Array Incrementation
Array incrementation refers to increasing the value of each element in an array. This operation is commonly performed with loops. In our exercise, to add one to each element of the 'bonus' array, we use a loop to visit each index and increment its value. Here's a practical example using the '++' operator within a loop:

for(int i = 0; i < 15; i++) {    bonus[i]++;}

This code increases the current value of each array element by one. It is essential for tasks like counting, scoring, or tracking cumulative quantities. Incrementation can also be done using compound assignment operators (e.g., '+=') for cases where the increment value is greater than one.
Displaying Array Values
To present the information stored in an array, you must display its values. The exercise asked us to display the 'bestscores' array in column format, which is done by iterating over the array and outputting each value followed by a newline character. Here’s how this might be achieved in C++:

for(int i = 0; i < 5; i++) {    std::cout << bestscores[i] << '';}

The newline '' ensures each value appears on a new line, creating the column effect. Displaying array values is often one of the final steps of a program and provides a visual confirmation of what has been stored or calculated within the array.

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

(Variable-Length Argument List) Write an application that calculates the product of a series of integers that are passed to method product using a variable-length argument list. Test your method with several calls, each with a different number of arguments.

(Turtle Graphics) The Logo language made the concept of turtle graphics famous. Imagine a mechanical turtle that walks around the room under the control of a Java application. The turtle holds a pen in one of two positions, up or down. While the pen is down, the turtle traces out shapes as it moves, and while the pen is up, the turtle moves about freely without writing anything. In this problem, you'll simulate the operation of the turtle and create a computerized sketchpad. Use a 20 -by- 20 array floor that's initialized to zeros. Read commands from an array that contains them. Keep track of the current position of the turtle at all times and whether the pen is currently up or down. Assume that the turtle always starts at position (0,0) of the floor with its pen up. The set of turtle commands your application must process are shown in Fig. 7.29 Command Meaning 1 Pen up 2 Pen down 3 Turn right 4 Turn left 5,10 Move forward 10 spaces (replace 10 for a different number of spaces) 6 Display the 20-by-20 array 9 End of data (sentinel) Suppose that the turtle is somewhere near the center of the floor. The following "program" would draw and display a 12 -by-12 square, leaving the pen in the up position: \\[ \begin{array}{l} 2 \\ 5,12 \\ 3 \\ 5,12 \\ 3 \\ 5,12 \\ 3 \\ 5,12 \\ 1 \\ 6 \\ 9 \end{array} \\] As the turtle moves with the pen down, set the appropriate elements of array floor to 1 s. When the 6 command (display the array) is given, wherever there's a 1 in the array, display an asterisk or any character you choose. Wherever there's a \(0,\) display a blank. Write an application to implement the turtle graphics capabilities discussed here. Write several turtle graphics programs to draw interesting shapes. Add other commands to increase the power of your turtle graphics language.

(Using the Enhanced for Statement) Write an application that uses an enhanced for statement to sum the double values passed by the command-line arguments. [Hint: Use the static method parseDouble of class Double to convert a String to a double value.]

a) Error: Assigning a value to a constant after it has been initialized. Correction: Assign the correct value to the constant in a final int ARRAY_SIZE declaration or declare another variable. b) Error: Referencing an array element outside the bounds of the array (b[10]). Correction: Change the \(<=\) operator to \(<\) c) Error: Array indexing is performed incorrectly. Correction: Change the statement to a \([1][1]=5 ;\)

(Total Sales) Use a two-dimensional array to solve the following problem: A company has four salespeople \((1 \text { to } 4)\) who sell five different products \((1 \text { to } 5) .\) Once a day, each salesperson passes in a slip for each 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 the slips for last month is available. Write an application that will read all this information for last month's sales and summarize the total sales by salesperson and by product. All totals should be stored in the two- dimensional array sales. After processing all the information for last month, display the results in tabular format, with each column representing a salesperson and each row 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 output should include these cross-totals to the right of the totaled rows and to the bottom of the totaled columns.

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