Monday
September 1, 2014

Homework Help: computer science

Posted by Cheryl on Saturday, February 18, 2012 at 7:27pm.

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.




#include<iostream>
#include<fstream>
using namespace std;

int *reverse(const int *, int);

int main()
{
int N;
cin >> N;
if(N > 50 || N < 0)
exit(0);

int *arr1 = new int[N], index;

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;
}

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;
}

I need to return a pointer but I am returning junk, help

Answer this Question

First Name:
School Subject:
Answer:

Related Questions

college - Write a C++ program to fill an array with 6 numbers, and then reverse...
programming - I really need help in this it makes me confuse.I should make a ...
math - don break a 4x7 array into a 2x7 array and another array. what is the ...
Math - Don breaks a4 by 7array into a 2 by 7 array and another array. What is ...
computer science - Create a method called countZeros, which takes an integer ...
Computer Science - I need help with this question please! Please write me step ...
computer science - Write a method printBackwards that prints an array of ...
assembler language - write an assembly language subordinate count according to ...
c++ - how do you pass only part of an Array through a function? I thought it was...
math - with each array write the factor pair. Rember that the # of row in the ...

Search
Members