Reverse Array Write a function that accepts an int array and the array ’s size a
ID: 3824966 • Letter: R
Question
Reverse Array Write a function that accepts an int array and the array ’s size as arguments . The function should create a copy of thearray , except that the element values should be reversed in the copy. The function should return a pointer to the newarray . Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file named data into an array . The program then passes the array to the your reverse array function, and prints the values of the new reversed array on standard output , one value per line. You may assume that the file data has at least N values . Prompts And Output Labels. There are no prompts for the integer and no labels for the reversed array that is printed out. Input Validation. If the integer read in from standard input exceeds 50 or is less than 0 the program terminates silently.
Here is my program but i am not getting any output :(
/* C++ Program that reads data from file and reverse the array and prints back */
#include <iostream>
#include <fstream>
using namespace std;
//Function that reverses the array
int* reverseArray(int arr[], int N)
{
int i, j=0;
int* rev;
//Allocating memory
rev = new int[N];
//Looping in back side
for(i=N-1; i>=0; i--, j++)
{
//Storing values
rev[j] = arr[i];
}
//Returning pointer
return rev;
}
//Main function
int main()
{
int N, i;
//Array declaration
int *values;
cin >> N;
//Validating N value
if(N > 50 || N < 0)
return 0;
//Allocating memory
values = new int[N];
//Opening file for reading
fstream fin("data.txt", ios::in);
i = 0;
//Reading 10 values
while(i<N)
{
//Reading data from file
fin >> values[i];
cout << " " << values[i];
i++;
}
//Reverse array and print to console
values = reverseArray(values, N);
cout << " ";
//Printing values
for(i=0; i<N; i++)
cout << " " << values[i];
cout << " ";
//Closing file
fin.close();
return 0;
}
Explanation / Answer
Your code works fine. (It had memory leak issue which I fixed.)
Basically it needs two things, one is there should exist a file named data.txt which contain integer space separated or one integer per line.
Second thing is after running this program, type a number from 1 to 50 and then you will get the result.
/* C++ Program that reads data from file and reverse the array and prints back */
#include <iostream>
#include <fstream>
using namespace std;
//Function that reverses the array
int* reverseArray(int arr[], int N)
{
int i, j=0;
int* rev;
//Allocating memory
rev = new int[N];
//Looping in back side
for(i=N-1; i>=0; i--, j++)
{
//Storing values
rev[j] = arr[i];
}
delete [] arr;
//Returning pointer
return rev;
}
//Main function
int main()
{
int N, i;
//Array declaration
int *values;
cin >> N;
//Validating N value
if(N > 50 || N < 0)
return 0;
//Allocating memory
values = new int[N];
//Opening file for reading
fstream fin("data.txt", ios::in);
i = 0;
//Reading N values
while(i<N)
{
//Reading data from file
fin >> values[i];
cout << " " << values[i];
i++;
}
//Closing file
fin.close();
//Reverse array and print to console
values = reverseArray(values, N);
cout << " ";
//Printing values
for(i=0; i<N; i++)
cout << " " << values[i];
cout << " ";
delete [] values;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.