Question

In: Computer Science

I am currently working on this problem. It says it is wrong, and I was wondering...

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;

}

Solutions

Expert Solution

//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


Related Solutions

I am working on this problem for the company AT & T and am not sure...
I am working on this problem for the company AT & T and am not sure how to start it. Draw a chart of the main inter-organizational linkage mechanisms (e.g., long -term contacts, strategic alliances, mergers) that your organization uses to manage its symbiotic resource interdependencies. Using resource dependence theory and transaction cost theory, discuss why the organization to manage its interdependencies in this way. Do you think the organization has selected the most appropriate linkage mechanisms? Why or why...
I am currently doing an experiment on continous wave NMR and was wondering if someon could...
I am currently doing an experiment on continous wave NMR and was wondering if someon could explain the theroy behind how resonace is created in a sample, for example hydrogen 1. If possible type would be best as I have trouble reading, on my laptop the hand written answers somtimes.
Explain applying for a position that I am currently working as a para-educator, however, I am...
Explain applying for a position that I am currently working as a para-educator, however, I am hired as an adult assistant. We are a new school short staff so I am needed to do more of the Para-educator's work alongside taking care of my student. How do I explain that showing I am qualified to do the new posting of Para-educator?
Here is a project I am currently working on and I could use some feedback on...
Here is a project I am currently working on and I could use some feedback on how to get started. I calculated the general probabilities for the first two columns of each spreadsheet (successes / total outcomes). I don't want to move forward until I know I'm on the right track but I'm concerned that I've oversimplified the question being asked. I'm also using Excel and am having a hard time finding an appropriate formula for conditional probability. My book...
Attached is the problem I am working on I have to use phantoms, and i have...
Attached is the problem I am working on I have to use phantoms, and i have already completed steps p and H, I need help help with step A , which is to "state and check the assumptions for the hypothesis test", I think the correct hypothesis test to use would be the 2 sample t test, but im not sure. The number of cell phones per 100 residents in countries in Europe is given in table #9.3.9 for the...
What am I doing wrong in this titration problem? Calculate the ph at the equivalence point...
What am I doing wrong in this titration problem? Calculate the ph at the equivalence point for the following titration 0.20M HCl versus 0.20M methylamine (CH3NH2). The Ka of methylammonium is 2.3x10^-11. First I have to divide .20M methylamine by 2 (Why?) to get .10M Then, I set up the equilibrium: (2.3 x 10^-11) = x^2 / .10M Since the ka is SO small, I just multiplied .10 with (2.3 x 10^-11) to get 2.3x10^-12, which is wrong. Why is...
I was wondering if I was doing a conversion right. I am supposed to take aqueous...
I was wondering if I was doing a conversion right. I am supposed to take aqueous HNO3 with a density of 1.42 g/mL and a mass percent of solution of 70% and convert that into molarity. Would the correct conversion be to take 70 g of HNO3 over 100 g of H2O multiplied by 1 mol HNO3 over 63.01 g HNO3 (the molar mass) and then multiply that by 1 g H2O over 0.001 L of H2O to get the...
Need some clarification on the following: I am currently working on an assignment with the following...
Need some clarification on the following: I am currently working on an assignment with the following question: - Identify the commands that are generally causing the highest load on your system and discuss your perspectives on why they cause such a load. Discuss your perspectives on the considerations system administration must give while using these commands. In my research, I can only find linux commands used to identify high loads on a linux system, but as we can see the...
I am currently working on creating a dice game. I have not figured out how to...
I am currently working on creating a dice game. I have not figured out how to make it work? What should I do to make it work? Here is what I have so far: <!doctype html> <html> <head> <meta charset="UTF-8"> <title>Dice Game</title> <link rel="stylesheet" type="text/css" href="dice.css"> </head> <body> <div class="row" align="center"> <div class="col-4"> <h3>Your Dice</h3> <img src="dice images/m1.png" width="100" height="100" alt="roll: 1" id="mydice1"/> <img src="dice images/m1.png" width="100" height="100" alt="roll: 1" id="mydice2"/> </div> <div class="col-4"> <h3>Opponent's Dice</h3> <img src="dice images/o1.png" width="100"...
I just need 3 and 5. I am not sure what I am doing wrong. I...
I just need 3 and 5. I am not sure what I am doing wrong. I get different numbers every time. Superior Markets, Inc., operates three stores in a large metropolitan area. A segmented absorption costing income statement for the company for the last quarter is given below: Superior Markets, Inc. Income Statement For the Quarter Ended September 30 Total North Store South Store East Store Sales $ 4,800,000 $ 960,000 $ 1,920,000 $ 1,920,000 Cost of goods sold 2,640,000...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT