Hi, I wounder if anyone can help me start a program which takes "USER INPUT" for any 10 numbers and then print them in reverse order using arrays what should I need to do to get started with this. Please help.

Thanks,
Maverick

Recommended Answers

All 9 Replies

For most assignments of this nature, most go with storing user input into a cstring.. then use a loop to display the contents of the string in reverse order.

Simple. Straight-forward.

U mean using for loops? can you give some example please.

U mean using for loops?

To be quite honest, I believe it is possible to use any type of loop structure for any given situation. But yes, 'for' loops seem to be the defacto standard when it comes to array operations... but any loop you decide to use will work just fine.

char number[50];

cout << "Enter a number: ";
cin >> number;

//using do/while
int i = strlen(number);

do{     
     cout << number[i];
     i--;
}while(i >= 0);

//using while
int i = strlen(number);

while(i >= 0)
{
     cout << number[i];
     i--;
}
     
//using for
for(int i = strlen(number); i >= 0; i--)
{
     cout << number[i];
}


//using a function from <algorithm>
int end = strlen(numbers);

reverse(&numbers[0], &numbers[end]);

cout << numbers;

These are very basic array operations. The goal of this assignment was to teach you that you can use any loop structure to perform any array operation you desire.

For most assignments of this nature, most go with storing user input into a cstring.. then use a loop to display the contents of the string in reverse order.

Simple. Straight-forward.

I think he means something like this :

Enter 10 numbers :   10 9 8 7 6 5 4 3 2 1

The reverse of those inputs are : 1 2 3 4 5 6 7 8 9 10

Is this right, maverick?

Either way, the principle remains the same.

1. accept user inputs into an array of appropriate type
2. use a loop to achieve desired output (which in this case is to display the array reverse order)

There you go

#include <iostream.h>
#include <conio.h>

int i;
int a[10];

int main()
{
	cout<<"\n\n\tEnter The 10 Numbers : \n";
	for(i=0;i<10;i++)
	{
		cout<<"\t";
		cin>>a[i];
	}

	cout<<"\n\n\tThe 10 Numbers In Reverse : \n\t";
	for(i=9;i>=0;i--)
	{
		cout<<a[i]<<"\t";
	}
    return 0;
}
commented: Don't give out the answer just yet. +0

why did my answer get a negative point? :(

i have the same assignment so thank you very much <3

A friend needed this assignment but in flowchart form and this was really helpful to see for my own benefit as well. I apologize to you Xavier that I can't give you 2 more points to negate the negative's.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.