Question

In: Computer Science

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 the first 10 values in this array, one number on a line.

6. Ask the user to enter a positive integer as the size of an array.

7. Use p to declare an integer array of that size using the "new".

8. Copy the data from array a to array p. If array a's size is bigger than p's, fill the array p then stop. If the array a's size is smaller, fill the first 100 elements of array p with the values in a.

Solutions

Expert Solution

Hi, Please find the below code in c++;

#include <iostream>

using namespace std;

int main()
{
// create an integer static array a[ ] with 100 elements.
static int a [100];
//Declare an integer pointer p.
// Let p pointing to the array a[ ].
int *p = a;
// create an integer to store value to inserted in array a[]
int value = 0;
// create an integer to the new size entered by the user to create a new array
int newSize = 0;
// using p store the even numbers in array a[]
for(int j =0 ;j<100 ;j++){
p[j] = value;
// increment value by 2 to get the next even integer
value += 2;
}
// display first 10 integer in array a[]
for(int j =0 ;j<10 ;j++){
// use endl to change the line
cout<< p[j] << endl;
}
  
// display a message to take the positive integer to create a new array
cout<<"Enter a positive integer ";
  
cin>> newSize;
//Use p to declare an integer array of that size using the "new".
p = new int [newSize];
  
//Copy the data from array a to array p.
//If array a's size is bigger than p's, fill the array p then stop.
//If the array a's size is smaller, fill the first 100 elements of array p with the values in a.
if(newSize > 100){
for(int j =0 ;j<100;j++){
p[j] = a[j];
}
}
else{
for(int j =0 ;j<newSize;j++){
p[j] = a[j];
}
}
  

return 0;
}

OUTPUT: The first 10 elements in array a[] with a new integer size to create a new array

Thanks

Hope it helps!!


Related Solutions

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])...
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...
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
Write a c program arrays2.c that checks if an integer array contains three identical consecutive elements....
Write a c program arrays2.c that checks if an integer array contains three identical consecutive elements. Assume the number of identical consecutive elements are no more than three if they exist. Sample input/output #1: Enter the length of the array: 11 Enter the elements of the array: -12 0 4 2 2 2 36 7 7 7 43 Output: The array contains 2 of three identical consecutive elements: 2 7 Sample input/output #2: Enter the length of the array: 4...
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)
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements...
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements b) Inputs an integer n from 1-30 from the keyboard. If n < 1 set n = 1. If n > 30 set n = 30. the program should keep asking the user the input n one by one, followed by printing of the value of n (n=n if bigger than 1 and smaller than 30, 1 if smaller than 1 and 30 if...
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: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
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...
Write a C function to swap the first and last elements of an integer array. Call...
Write a C function to swap the first and last elements of an integer array. Call the function from main() with an int array of size 4. Print the results before and after swap (print all elements of the array in one line). The signature of the arrItemSwap() function is: void arrItemSwap(int *, int, int); /* array ptr, indices i, j to be swapped */ Submit the .c file. No marks will be given if your pgm does not compile...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT