(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 Vaia!

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

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

(True or False) State whether the following are true or false. If the answer is \(f a l s e\), explain why. a) An array can store many different types of values. b) An array subscript should normally be of data type float. c) If there are fewer initializers in an initializer list than the number of elements in the array, the remaining elements are initialized to the last value in the initializer list. d) It's an error if an initializer list has more initializers than there are elements in the array. e) An individual array element that is passed to a function and modified in that function will contain the modified value when the called function completes execution.

( The Sieve of Eratosthenes) A prime integer is any integer that is evenly divisible only by itself and 1\. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows: a) Create an array with all elements initialized to 1 (true). Array elements with prime subscripts will remain \(1 .\) All other array elements will eventually be set to zero. You'll ignore elements 0 and 1 in this exercise. b) Starting with array subscript 2 , every time an array element is found whose value is 1 loop through the remainder of the array and set to zero every element whose subscript is a multiple of the subscript for the clement with value 1 . For array subscript \(2,\) all elements beyond 2 in the array that are multiples of 2 will be set to zero (subscripts 4,6 \(8,10, \text { etc. }) ;\) for array subscript \(3,\) all elements beyond 3 in the array that are multiples of 3 will be set to zero (subscripts \(6,9,12,15,\) etc.); and so on. When this process is complete, the array elements that are still set to one indicate that the subscript is a prime number. These subscripts can then be printed. Write a program that uses an array of 1000 elements to determine and print the prime numbers between 2 and \(999 .\) Ignore element 0 of the array.

(Dice Rolling) Write a program that simulates the rolling of two dice. The program should use rand to roll the first die and should use rand again to roll the second die. The sum of the two values should then be calculated. [Note: Each die can show an integer value from 1 to 6, so the sum of the two values will vary from 2 to \(12,\) with 7 being the most frequent sum and 2 and 12 being the least frequent sums.] Figure 7.26 shows the 36 possible combinations of the two dice. Your program should roll the two dice 36,000 times. Use a one-dimensional array to tally the numbers of times each possible sum appears. Print the results in a tabular format. Also, determine if the totals are reasonable (i.e., there are six ways to roll a \(7,\) so approximately one-sixth of all the rolls should be 7 ).

(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