reversing the output c++
#include <iostream>
#include <new>
using namespace std;
int main ()
{
int i,n;
int * p;
cout << "How many numbers would you like to type? ";
cin >> i;
p= new (nothrow) int[i];
if (p == 0)
cout << "Error: memory could not be allocated";
else
{
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
cout << p[n] << ", ";
delete[] p;
}
return 0;
for example i want 3 numbers, say the numbers you enter are 1,2,3, it'l will come up ive entered 1,2,3 in that order. i want to know how you reverse that so that it reads what you've typed in,(dynamic memory etc.) then reverses that order. tried about with it but cant get it going? any takers?
Related Article: phonebook program
is a C++ discussion thread by help wanted that has 1 reply and was last updated 1 year ago.
pjh-10
Junior Poster in Training
71 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Infraction Points: 5
Either you insert the numbers in the array in reverse order and print the array normally
for (n=i-1; n!=-1; n--)
{
cout << "Enter number: ";
cin >> p[n];
}
or normally insert items in the array, and print them in reverse order
for (n=i-1; n!=-1; n--)
cout << p[n] << ", ";
Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12
did that second method there and its working si far except for one instance, when i do 1,2,3 it comes up 0,3,2,1?
pjh-10
Junior Poster in Training
71 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Infraction Points: 5
Yeah, well initially was
for (n=i; n!=-1; n--)
but I edited in
for (n=i-1; n!=-1; n--)
Was a little bit quick on typewriting, forgot that -1 near the i. Try it now, it will definately work.
Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12
lifesaver lucaci, as you can probably tell, im a beginner,trying to learn all the time.lol
Thanks again
pjh-10
Junior Poster in Training
71 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Infraction Points: 5
Glad I could help. Also, don't forget to mark this thread as solved, since the matter at hand is no longer the problem. If you do have further questions, for each question start a new thread explaining the situation.
Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12