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 some \(\mathrm{C}++\) code that will fill an array a with 20 values of type int read in from the keyboard. You need not write a full program, just the code to do this, but do give the declarations for the array and for all variables.

Short Answer

Expert verified
Question: Write code to create an array of integers with a size of 20, and then use a for loop to get input from the user for each element in the array. Answer: ```cpp int a[20]; int i; for (i = 0; i < 20; i++) { std::cin >> a[i]; } ```

Step by step solution

01

Declare the array and required variables

Declare an int array 'a' of size 20 and a variable 'i' which will be used as an iterator in the for loop. ```cpp int a[20]; int i; ```
02

Use a for loop to iterate through the array

Use a for loop with iterator 'i' and loop from 0 to 19 (inclusive), as the size of our array is 20. ```cpp for (i = 0; i < 20; i++) { // We'll add the input code in the next step } ```
03

Get input from the user for each element in the array

Inside the for loop, use 'std::cin' to read inputs from the user for each element in the array a[i]. ```cpp for (i = 0; i < 20; i++) { std::cin >> a[i]; } ``` As the question only asks for the specific code to achieve the task and not for a full program, this concludes the solution.

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!

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

Write code that will fill the array a (declared below) with numbers typed in at the keyboard. The numbers will be input five per line, on four lines (although your solution need not depend on how the input numbers are divided into lines). int a[4][5];

Consider the following function definition: void tripler(int& n) { n = 3*n; } Which of the following are acceptable function calls? int a[3] = {4, 5, 6}, number = 2; tripler(number); tripler(a[2]); tripler(a[3]); tripler(a[number]); tripler(a);

Suppose we expect the elements of the array a to be ordered so that a[0] ? a[1] ? a[2]? ... However, to be safe we want our program to test the array and issue a warning in case it turns out that some elements are out of order. The following code is supposed to output such a warning, but it contains a bug. What is it? double a[10]; for (int index = 0; index < 10; index++) if (a[index] > a[index + 1]) cout << "Array elements " << index << " and " << (index + 1) << " are out of order.";

Write a function definition for a function called one_more, which has a formal parameter for an array of integers and increases the value of each array element by one. Add any other formal parameters that are needed.

Suppose you have the following array declaration in your program: int your_array[7]; Also, suppose that in your implementation of \(\mathrm{C}++,\) variables of type int use two bytes of memory. When you run your program, how much memory will this array consume? Suppose that when you run your program, the system assigns the memory address 1000 to the indexed variable your_array \([\theta]\) What will be the address of the indexed variable your_array[3]?

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