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

(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

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

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

(Fill in the Blanks) Answer each of the following: a) Lists and tables of values can be stored in ___ or ___. b) The elements of an array are related by the fact that they have the same ___ and __. c) The number used to refer to a particular element of an array is called its __. d) \(A(n)\) __ should be used to declare the size of an array, because it makes the program more scalable. e) The process of placing the elements of an array in order is called __ the array.vv f) The process of determining if an array contains a particular key value is called __ the array. g) An array that uses two subscripts is referred to as a(n) __ array.

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

(Find the Minimum Valtue in an Arraty) Write a recursive function recursiveMinimum that takes an integer array, a starting subscript and an ending subscript as arguments, and returns the smallest element of the array. The function should stop processing and return when the starting subscript equals the ending subscript.

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