Question 1:
Using C++ programming language, write the following program:
(1) The program defines a constant SIZE equal to 20
(2) The program defines an array of integers with an array size equal to SIZE.
(3) The program reads 20 integers and store them in a array
(4) Without using any (swap) instruction, the program should reverse the positions of
values. The first value becomes the last one; the last value becomes the first one, and
so on, until the array becomes completely reversed.
Answer:
#include <iostream>
using namespace std;
const int SIZE=20;
void main(){
int A[SIZE];
int x;
for (int i=0; i< SIZE; i++)
{
cout << "Enter A[" << i <<"]: ";
cin >> A[i];
}
for (int i=0, j=SIZE-1; i<j; i++, j--)
{
x=A[i];
A[i]=A[j];
A[j]=x;
}
for (int i=0; i<SIZE; i++)
{
cout << A[i] << endl;
}
system("pause");
}
, Question 2:
Using C++ programming language, write the following program:
(1) The program should fix a constant SIZE equal to 20
(2) The program defines an array of integer with an array size equal to SIZE.
(3) The program reads 20 integers and store them in an array
(4) The program reads an integer value named (V).
(5) The program reads an array’s position named (index).
(6) The user should not be able to enter a value for index outside the scope of the
array.
(7) The program must put the value V at the position (index) in the array, shifting each
element from this position to the top, and dropping off the first element.
#include <iostream>
using namespace std;
const int SIZE=20;
void main(){
int A[SIZE];
int Value, index;
for (int i=0; i< SIZE; i++)
{
cout << "Enter A[" << i <<"]: ";
cin >> A[i];
}
cout << "Enter The Value V: ";
cin >> Value;
index=20;
while (index < 0 || index >= SIZE)
{
cout << "Enter The index: ";
cin >> index;
}
for (int i=SIZE-1; i>index; i--)
{
Using C++ programming language, write the following program:
(1) The program defines a constant SIZE equal to 20
(2) The program defines an array of integers with an array size equal to SIZE.
(3) The program reads 20 integers and store them in a array
(4) Without using any (swap) instruction, the program should reverse the positions of
values. The first value becomes the last one; the last value becomes the first one, and
so on, until the array becomes completely reversed.
Answer:
#include <iostream>
using namespace std;
const int SIZE=20;
void main(){
int A[SIZE];
int x;
for (int i=0; i< SIZE; i++)
{
cout << "Enter A[" << i <<"]: ";
cin >> A[i];
}
for (int i=0, j=SIZE-1; i<j; i++, j--)
{
x=A[i];
A[i]=A[j];
A[j]=x;
}
for (int i=0; i<SIZE; i++)
{
cout << A[i] << endl;
}
system("pause");
}
, Question 2:
Using C++ programming language, write the following program:
(1) The program should fix a constant SIZE equal to 20
(2) The program defines an array of integer with an array size equal to SIZE.
(3) The program reads 20 integers and store them in an array
(4) The program reads an integer value named (V).
(5) The program reads an array’s position named (index).
(6) The user should not be able to enter a value for index outside the scope of the
array.
(7) The program must put the value V at the position (index) in the array, shifting each
element from this position to the top, and dropping off the first element.
#include <iostream>
using namespace std;
const int SIZE=20;
void main(){
int A[SIZE];
int Value, index;
for (int i=0; i< SIZE; i++)
{
cout << "Enter A[" << i <<"]: ";
cin >> A[i];
}
cout << "Enter The Value V: ";
cin >> Value;
index=20;
while (index < 0 || index >= SIZE)
{
cout << "Enter The index: ";
cin >> index;
}
for (int i=SIZE-1; i>index; i--)
{