In: Computer Science
I am currently working on this problem. It says it is wrong, and I was wondering if someone could correct my code to where its ouput is as followed.
Expected Output:
6↵ 5↵ 4↵ 3↵ 2↵ 1
Actual Output:
9↵ 8↵ 7↵ 6↵ 5↵ 4↵ 3↵ 2↵ 1↵ 0↵
9.10: Reverse Array
Write a function that accepts an int array and the array’s size as
arguments. The function should create a copy of the array, except
that the element values should be reversed in the copy. The
function should return a pointer to the new array. 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.
I used this code:
#include<iostream>
#include<fstream>
using namespace std;
int *reverse(const int *, int);
int main()
{
int N;
N=10;
/*
cin >> N;
if(N > 50 || N < 0)
exit(0);
*/
// int *arr1 = new int[N], index;
int *arr1=new int[N];
int index;
for(int i=0;i<N;i++)arr1[i]=i;
/*
fstream datafile;
datafile.open("data.txt", ios::in | ios::out);
for(index = 0; index < N; index++)
{
datafile >> arr1[index];
}
*/
int *temp;
temp = reverse(arr1,N);
for(index = 0; index < N; index++)cout << temp[index]
<< endl;
system("PAUSE");
}
int *reverse(const int *arr1, int N)
{
int index = N-1;
int *arr2;
arr2 = new int[N];
for(int count = 0; count < N; count++)
{
arr2[count] = arr1[index];
index--;
}
return arr2;
}
//header files
#include<iostream>
#include<fstream>
using namespace std;
//declaring reverse function
int *reverse(const int *, int);
//main function
int main()
{
//declaring N of integer type
int N;
//Previously you had declared N=10 which is not required as N is
supposed to be taken as an input
//so I removed N=10
//previously you commented the below 3 lines of code, which is
required
//so I uncommented it
//Here it reads N as input from user
cin >> N;
//if N>50 or N<0 then
if(N > 50 || N < 0)
exit(0);//exit from the program
// int *arr1 = new int[N], index;
//declaring an integer pointer and allocating it a memory of N
integers
int *arr1=new int[N];
//declaring index
int index;
//previously you used below commented loop for storing values in
array arr1
//so that loop was basically storing the index value as an element
in arr1
//but In question It is required to take data from the file and
then store it in arr1
//so i commented that loop as shown just below
//for(int i=0;i<N;i++)arr1[i]=i;
//now previously it was commented but it is required here as
question requires data to be fetched from file
//so declaring datafile as object of fstream class
fstream datafile;
//now opening the file data.txt using open function
datafile.open("data.txt", ios::in | ios::out);
//loop which will run N times
for(index = 0; index < N; index++)
{
//fetching data from data.txt and storing it in arr1 at index
position 'index'
datafile >> arr1[index];
}
//declaring pointer of integer type
int *temp;
//now calling reverse function by passing arr1 and N to it and
storing the returned value in temp
temp = reverse(arr1,N);
//loop which will run for N times
for(index = 0; index < N; index++)
cout << temp[index] << endl;//now printing value at
index in array temp
system("PAUSE");//wait until user press any key to end the
program
}//main ends
//reverse function
int *reverse(const int *arr1, int N)
{
//setting index to N-1
int index = N-1;
//declaring arr2 pointer of integer type
int *arr2;
//now allocating memory of N integers to arr2
arr2 = new int[N];
//loop which will run for N times
for(int count = 0; count < N; count++)
{
//now storing the value present at index 'index' of arr1 array in
the index 'count' of arr2 array
//so this is just to do the reverse that is copy arr1 to arr2 such
that
//element values should be reversed in the arr2
arr2[count] = arr1[index];
//decrement index by 1
index--;
}
//at last return arr2
return arr2;
}//reverse function ends
//now On the console where output will be displayed, firstly an
integer as input will be taken from user
//and then output will be displayed
//Data.txt