In: Computer Science
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.
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!!