/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Problem 13 (Single Array Questions) Write s... [FREE SOLUTION] | 91Ó°ÊÓ

91Ó°ÊÓ

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

Short Answer

Expert verified
a) Use a for loop to set each element of 'counts' to 0. b) Use a for loop to increase each element of 'bonus' by 1. c) Use a Scanner and a for loop to read values into 'monthlyTemperatures'. d) Use a for loop to print each 'bestScores' element on a new line.

Step by step solution

01

Initializing Array Elements to Zero

To initialize all elements of an integer array named 'counts' with 10 elements to zero, you can use a for loop that iterates through each element setting it to zero: for (int i = 0; i < counts.length; i++) { counts[i] = 0;}
02

Adding 1 to Each Element of an Array

To add 1 to each of the 15 elements in an integer array called 'bonus', use a for loop to iterate over each element and increase its value by 1: for (int i = 0; i < bonus.length; i++) { bonus[i] += 1;}
03

Reading Array Values from the Keyboard

To read 12 values into a double array named 'monthlyTemperatures', use a Scanner object to input values from the user: Scanner input = new Scanner(System.in);for (int i = 0; i < monthlyTemperatures.length; i++) { monthlyTemperatures[i] = input.nextDouble();}
04

Printing Array Values in Column Format

To print each of the 5 values of an integer array called 'bestScores' in column format, use a for loop to iterate and print each element on a new line: for (int i = 0; i < bestScores.length; i++) { System.out.println(bestScores[i]);}

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 91Ó°ÊÓ!

Key Concepts

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

Initializing Arrays
In C++, initializing arrays is a fundamental operation that sets the initial state of an array before use. For instance, setting all elements of an array to zero is a common practice when the values will be accumulated or replaced later on.

Considering the example provided, an integer array named 'counts' with 10 elements can be initialized to zero using a for loop. The loop runs from the first index (0) to the last index (array length - 1), and sets each element to zero:
  • for (int i = 0; i < 10; i++) {
    counts[i] = 0;
    }
It's important to note that uninitialized arrays contain garbage values, which are essentially random bits present in memory. Therefore, properly initializing an array ensures predictable and defined behavior of your program.
Iterating Over Arrays
To iterate over arrays in C++, you will commonly use for loops to access each element sequentially. This is particularly useful when performing operations like updating each element's value.

In the exercise, we needed to add 1 to each of the 15 elements of an integer array 'bonus'. A for loop made this task straightforward:
  • for (int i = 0; i < 15; i++) {
    bonus[i] += 1;
    }
Using array indices, the loop iterates through the 'bonus' array from start to end, incrementing each value. Iteration plays a crucial role in array manipulations and being comfortable with it allows you to perform various operations across array elements efficiently.
Reading User Input into Arrays
Accepting user input into arrays requires capturing data entered by the user and storing it into the correct array positions. The process usually occurs in a loop that runs for the number of elements you wish to input.

To read user inputs for the 'monthlyTemperatures' array with 12 elements, you can use the following:
  • Scanner input = new Scanner(System.in);
    for (int i = 0; i < 12; i++) {
    monthlyTemperatures[i] = input.nextDouble();
    }
Each iteration prompts the user for a value and stores it in the corresponding index. It is essential to ensure that the user inputs the correct type of data (in this case, a double) to prevent runtime errors or incorrect data storage.
Printing Array Elements
The printing of array elements is a common way to display the contents of an array to the user. Whether in a single line or formatted differently, the output helps in understanding the current state of array data.

For the 'bestScores' array, presenting its values in a column can be done like this:
  • for (int i = 0; i < 5; i++) {
    System.out.println(bestScores[i]);
    }
This loop traverses the 'bestScores' array and prints each element on a new line, resulting in a columnar display. Printing arrays can be customized in numerous ways depending on the desired output, for example, formatting numbers or aligning columns for better readability.

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

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

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

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

(Bubble Sort Enbancements) The bubble sort described in Exercise 7.11 is inefficient for large arrays. Make the following simple modifications to improve the performance of the bubble sort: a) After the first pass, the largest number is guaranteed to be in the highest-numbered element of the array; after the second pass, the two highest numbers are "in place," and so on. Instead of making nine comparisons on every pass, modify the bubble sort to make eight comparisons on the second pass, seven on the third pass, and so on. b) The data in the array may already be in the proper order or near-proper order, so why make nine passes if fewer will suffice? Modify the sort to check at the end of each pass if any swaps have been made. If none have been made, then the data must already be in the proper order, so the program should terminate. If swaps have been made, then at least one more pass is needed.

(Bucket Sort) A bucket sort begins with a one-dimensional array of positive integers to be sorted and a two-dimensional array of integers with rows subscripted from 0 to 9 and columns subscripted from 0 to \(n-1\), where \(n\) is the number of values in the array to be sorted. Each row of the two- dimensional array is referred to as a bucket. Write a function bucketsort that takes an integer array and the array size as arguments and performs as follows: a) Place each value of the one-dimensional array into a row of the bucket array based on the value's ones digit. For example, 97 is placed in row 7,3 is placed in row 3 and 100 is placed in row \(0 .\) This is called a "distribution pass." b) Loop through the bucket array row by row, and copy the values back to the original array. This is called a "gathering pass." The new order of the preceding values in the onedimensional array is 100,3 and 97 c) Repeat this process for cach subsequent digit position (tens, hundreds, thousands, etc.). On the second pass, 100 is placed in row 0,3 is placed in row 0 (because 3 has no tens digit) and 97 is placed in row \(9 .\) After the gathering pass, the order of the values in the one-dimensional array is 100,3 and \(97 .\) On the third pass, 100 is placed in row 1,3 is placed in row zero and 97 is placed in row zero (after the 3). After the last gathering pass, the original array is now in sorted order. Note that the two-dimensional array of buckets is 10 times the size of the integer array being sorted. This sorting technique provides better performance than an insertion sort, but requires much more memory. The insertion sort requires space for only one additional element of data. This is an example of the space-time trade-off: The bucket sort uses more memory than the insertion sort, but performs better. This version of the bucket sort requires copying all the data back to the original array on each pass. Another possibility is to create a second two-dimensional bucket array and repeatedly swap the data between the two bucket arrays.

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.