2) Complete the following program by writing the missing code . You need to use
ID: 1716586 • Letter: 2
Question
2) Complete the following program by writing the missing code . You need to use a for() loop to write the missing code. Write a function that takes the original array of integers (origInt[]) as well as a new array of 5 integers (reversedInt[]) and returned the original 5 integers reversed in the second array. Both arrays are declared in main(). Main() calls both of these functions
// following program reverses the order of the array
// i.e., it reverses the array origInt[5]={1.2.3.4.5} into another array
// reversedInt[5]={5.4.3.2.1}
#include <iostream>
using namespace std;
// Function prototypes
void reverseOrigInt(.........);
int main()
{
int origInt[5]={1.2.3.4.5};
int reversedInt[5];
// call the function that reverses the array origInt[] into another reversedInt[]
// display original array using a for() loop
// display the reversed array using a for() loop
cout << " "; system("pause");
return 0;
}
void reverseOrigInt(............)
{
// using a for(; ; ) loop, reverse the array of 5 integers
}
Explanation / Answer
solution :
#include <iostream>
using namespace std;
// Function prototypes
void reverseOrigInt(int *p , int *q);
int main()
{
int i=0;
int origInt[5]={1,2,3,4,5};
int reversedInt[5];
// call the function that reverses the array origInt[] into another reversedInt[]
reverseOrigInt( origInt, reversedInt);
// display original array using a for() loop
for(i=0;i<5;i++)
{cout<<"origInt["<<i<<"]="<<origInt[i]<<endl;}
cout<<endl;
// display the reversed array using a for() loop
for(i=0;i<5;i++)
{cout<< "reversedInt["<<i<<"]="<<reversedInt[i]<<endl;}
cout << " ";
return 0;
}
void reverseOrigInt(int *p , int *q)
{
int i=0;
//ing a for(; ; ) loop, reverse the array of 5 integers
for(i=0;i<5;i++)
{ q[i]= p[4-i];}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.