Question

9. Write some C++ code that will fill an array a with 20 values of type...

9. Write some 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.

10. Suppose you have the following array declaration in your program:

int your_array[7];

Also, suppose that in your implementation of 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[0].

What will be the address of the indexed variable your_array[3]?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

9.

// C++ program to input 20 number from keyword
# include <iostream>
using namespace std;

int main()
{
   int arr[20] ; // declare an array of 20 elements
   int i;
   // loop to input 20 numbers from keyboard
   for(i=0;i<20;i++)
   {
       cout<<"Enter element-"<<(i+1)<<" : ";
       cin>>arr[i];
   }
   return 0;
}

10.

int your_array[7];
In C++ variables of type int use two bytes of memory.
The above array your_array is of size 7. Since each int uses 2 bytes of memory, therefore 7 int value will occupy 7*2 = 14 bytes of memory.
your_array will consume 14 bytes of memory.
your_array[0] is assigned the memory address 1000
Each value of your_array will consume 2 bytes of memory (since type is int).
your_array[3] will point to 4th element of array. Address of your_array[3] = 1000 + 2*3 = 1000+6 = 1006

Add a comment
Know the answer?
Add Answer to:
9. Write some C++ code that will fill an array a with 20 values of type...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 3. Suppose that an array of size 5 is declared, of type integer (4 bytes). int...

    3. Suppose that an array of size 5 is declared, of type integer (4 bytes). int theArray[5]; Also, suppose that the computer assigns the memory address 3152 to the variable at index 0. Explain how the computer would access theArray[2], and what the memory address would be. Draw diagrams as necessary. Following are example question and answer: Suppose that an array of size 5 is declared, of type double (8 bytes). double theArray[5]; Also, suppose that the computer assigns the...

  • C++ program Write code that will fill the array a (declared next) with numbers typed in...

    C++ program Write code that will fill the array a (declared next) 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];

  • This is a fill in the code type: // FILL IN THE CODE - Write a...

    This is a fill in the code type: // FILL IN THE CODE - Write a program to multiply 2 numbers, print out the results and print out the original numbers in ascending order. #include <iostream> using namespace std; int main() {   int num1;       // holds 1st number   int num2;       // holds 2nd number   int result;       // holds result of multiplication   int *num1Ptr = nullptr; // int pointer which will be set to point to the 1st number   int *num2Ptr...

  • #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='...

    #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='z';      pc=&(c[8]);      pk=&(k[0]);      for (i=0; i<9; i++)     {           *pc=a-(char)i;           pc--;           *pk=(int)a-i;           pk++;           }                     return 0; }//end of main Answer the below questions with the above code Write out the memory map for the above C code in the following table. For array variables, list the address range for the entire array. Assume the memory address starts from 100, that is, the address for a...

  • Please answer 2.6.1 2.6 The table below shows 32-bit values of an array stored in memory...

    Please answer 2.6.1 2.6 The table below shows 32-bit values of an array stored in memory Address Data 24 38 2 4 32 36 40 2.6.1 [5] <COD §§22, 2.3-For the memory locations in the table above, write C code to sort the data from lowest to highest, placing the lowest value in the smallest memory location shown in the figure. Assume that the data shown represents the C variable called Array, which is an array of type int, and...

  • Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You...

    Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You can initialize the variables to any legal value of your choosing. Next, declare and initialize a pointer variable to each of the variables Using printf), output the following: The address of each of the first three variables. Use the "Ox%x" format specifier to print the addresses in hexadecimal notation The value of each variable. They should equal the value you gave...

  • Assuming that the array upc declared below has already been filled with MAX_LENGTH values, write a...

    Assuming that the array upc declared below has already been filled with MAX_LENGTH values, write a function that will ask to input an integer from the keyboard and perform a sequential search of the array for the input integer. If the input integer is found, print the index of the matching value in the array. Otherwise, print "Not found". Include declarations for all variables that you use.             const int MAX_LENGTH = 25;             int upc[MAX_LENGTH]; Please write in C++

  • Three C Code Questions: 5. Write a C program that declares an array of 10 strings,...

    Three C Code Questions: 5. Write a C program that declares an array of 10 strings, each of max length 50 characters, and then reads an entire line of text from the keyboard into each string. 6. Write C code that finds the longest name in this array of strings that you read in Q5. Hint: find the position of the longest string, not the string itself. 7. Write a C program that creates a 4 x 6 two dimensional...

  • Suppose a is an array of values of type double that is partially filled. The array...

    Suppose a is an array of values of type double that is partially filled. The array contains meaningful values in only the first numberUsed elements. where numberUsed is a variable of type int. Write some java code to display all the meaningful values in the array a.

  • Consider the following code. Assume that the array begins at memory address Ox200. What is printed...

    Consider the following code. Assume that the array begins at memory address Ox200. What is printed by the following code. Assume sizeof(int) is 4 bytes and sizeof(char) is 1 byte. Do not include the Ox or leading zeros in your answer. Enter -1 if the answer is indeterminate, or -2 if the code generates a compile error, or -3 if the code generates a runtime error. #include <stdio.h> int main() { int a[] {1,2,3}; int *iptr a; printf("%p\n", iptr+1); return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT