Question

In: Computer Science

Write a program in c++ to do the following : (1) Declare an array a of...

Write a program in c++ to do the following : (1) Declare an array a of size 10 and three pointer variables p, q, and v. (2) Write a loop to fill array a with values 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 (3) write following statement: p= &a[2]; q = &a[5]; i = *q - *p; cout<<“The value of i is”<< i; i = *p - *q; cout<<“The value of i is %d”<< i; 4) assign address of a to v. 5) write a loop to print values of array a using pointer variable v

Solutions

Expert Solution

//Program:

#include<iostream>
using namespace std;
int main()
{
   int a[10], *p, *q, *v; //Declare an array a of size 10 and three pointer variables p, q, and v
   int i;
   for(int i=0; i<10; i++)   //loop to fill array a with values 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
   {
       a[i]=(i+1)*10;
   }
   p= &a[2];              //assigning value of a[2] to p
   q = &a[5];             //assigning value of a[5] to q
   i = *q - *p;
   cout<<"The value of i is"<< i<<endl;
   i = *p - *q;
   cout<<"The value of i is "<< i<<endl;
   v=a;                       //assign address of a to v
   for(i=0; i<10; i++)           //loop to print values of array a using pointer variable v
   {
      cout<<*(v+i)<<" ";
   }
   return 0;
}

//Output:


Related Solutions

Rewrite your program for part 1. Do not declare the array globally, declare it in the...
Rewrite your program for part 1. Do not declare the array globally, declare it in the loop function. This now requires that you add two parameters to your fill array and print array functions. You must now pass the array name and array size as arguments, when the program calls these functions. The program has the same behavior as problem 1, but illustrates the difference between globally and locally declared variables. The program code for part 1 was: int Array[15]...
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an...
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an integer pointer p. 3. Let p pointing to the array a[ ]. 4. Use p (you have to use p) to put 0 into the first element of this array, 2 into the second element, 4 into the 3rd element, 6 into the 4th element, ... 198 into the 100th element of this array. 5. Use a (you have to use a) to display...
Write the following program in MIPS: a) declare an array A of the following numbers: 3,...
Write the following program in MIPS: a) declare an array A of the following numbers: 3, 5, 8, 10, 12, 2, 76, 43, 90, 44 b) declare a variable called size which stores the number of element in array A, that is 10. c) write a subroutine to search for a number stored in an array and return true or false. In C++ the subroutine is as follows: search(array, size, number_To_Search) e.g. search(A, 10, 12) The subroutine should return 0...
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt the user for 6 integer values and store them in arr. Prompt the user for a target integer target. Search for targetin arr. If targetis found to match an element in the arr, then the program prints out a message which contains the address of the found element, otherwise, if no element found then the message “the element target not found” will be printed...
C++ Vectors. Create a program do the following in the program: 1. declare an vector without...
C++ Vectors. Create a program do the following in the program: 1. declare an vector without specifying the size 2. use push_back to add random integers between 100 and 999 to the vector 3. write a function that returns the smallest, largest, and average of the numbers in the vector display the smallest, largest, and average of the numbers in the vector
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
Write a program in c++ to do the following: 2. Create an array to hold up...
Write a program in c++ to do the following: 2. Create an array to hold up to 20 integers. 3. Create a data file or download attached text file (twenty_numbers.txt) that contains UP TO 20 integers. 4. Request the input and output file names from the user. Open the files being sure to check the file state. 5. Request from the user HOW MANY numbers to read from the data file, up to twenty. Request the number until the user...
Write C++ program to do the following: 1. Create integer array size of 10 2. Ask...
Write C++ program to do the following: 1. Create integer array size of 10 2. Ask user input the values of the array's element using for loop 3. pass the array to void function. in void function do the following: a. Find the maximum of the array. b. Compute the element average c. Find out how many numbers are above the average d. Find out and print how many numbers are below the average e. find out how many numbers...
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT