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 a program that will read up to 10 nonnegative integers into an array called number_array and then write the integers back to the screen. For this exercise you need not use any functions. This is just a toy program and can be very minimal.

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.

Insert const before any of the following array parameters that can be changed to constant array parameters: void output(double a[], int size); //Precondition: a[0] through a[size - 1] have values. //Postcondition: a[0] through a[size - 1] have been //written out. void drop_odd(int a[], int size); //Precondition: a[0] through a[size - 1] have values. //Postcondition: All odd numbers in a[0] through //a[size - 1] have been changed to 0.

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);

Following is the declaration for an alternative version of the function search defined in Display \(7.12 .\) In order to use this alternative version of the search function we would need to rewrite the program slightly, but for this exercise all you need to do is to write the function definition for this alternative version of search. bool search(const int a[], int number_used, int target, int& where); //Precondition: number_used is <= the declared size of the //array a; a[0] through a[number_used -1] have values. //Postcondition: If target is one of the elements a[0] //through a[number_used - 1], then this function returns //true and sets the value of where so that a[where] == //target; otherwise this function returns false and the //value of where is unchanged.

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