Have two problems with this program:
1. The user is not supposed to enter more than 20 numbers, but the program wont move on unless 20 numbers are enteres.
2. How would i put the bubble sort part of the program into a function to be called. I tried and i kept getting the expression must have pointer-to-object type.
Code:
#include<iostream>
using namespace std;
int main()
{
int temp;
int num[20];
cout<<"Pleas enter a series of numbers(no more than 20): "<<endl;
for(int i=0; i<20; i++)
{
cin>>num[i];
}
while(!cin){//says while the input is not equal to an interger go to next line
cout<<"Error, Please enter a series of numbers: "<<endl;
for(int i=0; i<20; i++)
{
cin.clear();//clears the input from num[i]
cin.ignore(256,'\n');//flushes the input stream
cin>>num[i];
}
}
cout<<endl;
cout<<"Orignally entered array is: "<<endl;
for(int j=0; j<20; j++)
{
cout<<num[j];
cout<<endl;
}
cout<<endl;
for(int i=0; i<19; i++)
{
for(int j=0; j<19; j++)
{
if(num[j]>num[j+1])
{
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
cout<<"Sorted Array is: "<<endl;
for(int i=0; i<20; i++)
{
cout<<num[i]<<endl;
}
system("pause");
}