by CodeChum Admin
We've already made listing an array the easy way, but how about listing and printing the array in reverse order?
Make a program that will input an integer and then using loops, add items on an array/list one by one for the same number of times as that of the first inputted integer. Then, print out the array/list in ascending order. After doing that, print the array again on the next line in reverse order, that is, starting from the last item on the array/list down to the first one, each in separated lines.
Input
The first line contains the size of the array.
The next lines contains the items of the array (integers).
5
1
64
32
2
11
Output
The first line contains the elements of the array printed in ascending order.
The second line contains the elements of the array printed in reversed order.
1·64·32·2·11
11·2·32·64·1
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
using namespace std;
void swap(int *p,int *q)
{
unsigned int t;
t=*p;
*p=*q;
*q=t;
}
void sort(int a[],int n)
{
int i,j;
int temp;
for(i = 0;i < n-1;i++)
{
for(j = 0;j < n-i-1;j++)
{
if(a[j] > a[j+1]) swap(&a[j],&a[j+1]);
}
}
cout<<"\nAscending Order: ";
for(i = 0;i < n;i++) cout<<a[i]<<" - ";
cout<<"\nDescending Order: ";
for(i = n-1;i >=0;i--) cout<<a[i]<<" - ";
}
int main()
{
int Count;
cout<<"\n\tEnter Count of Numbers: "; cin>>Count;
int Nums[Count],n=0;
for(n=0;n<Count;n++)
{
cout<<"\n\tEnter Number-"<<n+1<<": "; cin>>Nums[n];
}
sort(&Nums[0],Count);
return(0);
}
Comments
Leave a comment