Bubble Sort, how can I validate the numbers?
Nov 1, 2010 at 8:19pm UTC
So far, I have written a program that will allow me to enter the number of numbers I want in to the array, and read in the numbers. It then will use bubble sort to sequentally order them..
But I'm having a hard time doing the following;
(1) How can I validate the entered character is a number? I don't want to allow the user to enter a letter or anything other than an integer.
(2) How can I restrict the max number entries into the array to a number like 20? Right now I can enter in as many as I want, and I'm having a hard time figuring out how to restrict it.
Here's what I have so far..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include <iostream>
using namespace std;
int compare(int , int );
void sort(int [], const int );
void swap(int *, int *);
int compare(int x, int y)
{
return (x > y);
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void sort(int table[], const int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n-1; j++)
{
if (compare(table[j], table[j+1]))
swap(&table[j], &table[j+1]);
}
}
}
int quantity;
int * tab;
void main()
{
cout << "Input quantity: " ;
cin >> quantity;
tab = new int [quantity];
cout << "Input numbers: \n\n" ;
for (int i = 0; i < quantity; i++)
{
int x = i;
cout << "#" << ++x << ": " ;
cin >> tab[i];
}
cout << "\nBefore sorting: " ;
for (int i = 0; i < quantity; i++)
{
cout << tab[i] << " " ;
}
cout << "\nAfter sorting: " ;
sort(tab, quantity);
for (int i = 0; i < quantity; i++)
{
cout << tab[i] << " " ;
}
}
Nov 1, 2010 at 8:36pm UTC
Nov 4, 2010 at 3:22pm UTC
I'm still having some trouble with it.. I am wanting to restrict the array to a size of 20 and this is what I came up with, but my loop doesn't work..it just hangs after the first cout =[
1 2 3 4 5 6 7 8 9 10 11 12 13 14
{
cout << "Enter the amount of numbers you would like to sort : " ;
cin >> quantity;
}
/*validate user input*/
while (!isdigit(quantity));{
cout << "Please enter an integer : " ;
cin >> quantity;}
{
Last edited on Nov 4, 2010 at 3:23pm UTC
Nov 4, 2010 at 3:44pm UTC
Did you read the link?
Nov 4, 2010 at 4:21pm UTC
I got the program running, but I still can't grasp how to verify what being entered is an integer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
#include <iostream>
using namespace std;
int compare(int , int );
void sort(int [], const int );
void swap(int *, int *);
int compare(int x, int y)
{
return (x > y);
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void sort(int table[], const int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n-1; j++)
{
if (compare(table[j], table[j+1]))
swap(&table[j], &table[j+1]);
}
}
}
int quantity;
int * tab;
void main()
{
cout << "Input quantity: " ;
cin >> quantity;
if (quantity > 20)
{cout << "Enter a value less than 20 \n\n" ;
cin >> quantity;
}if (quantity > 20)
{cout << "Enter a value less than 20 \n\n" ;
cin >> quantity;
}if (quantity > 20)
{cout << "Enter a value less than 20 \n\n" ;
cin >> quantity;
}if (quantity > 20)
{cout << "Enter a value less than 20 \n\n" ;
cin >> quantity;
}if (quantity > 20)
{cout << "Enter a value less than 20 \n\n" ;
cin >> quantity;
}if (quantity > 20)
{cout << "Enter a value less than 20 \n\n" ;
cin >> quantity;
}if (quantity > 20)
{cout << "Enter a value less than 20 \n\n" ;
cin >> quantity;
}if (quantity > 20)
{cout << "Enter a value less than 20 \n\n" ;
cin >> quantity;
}
else (quantity <= 20);
{cout << "Thanks \n\n" ;
}
tab = new int [quantity];
cout << "Input numbers: \n\n" ;
for (int i = 0; i < quantity; i++)
{
int x = i;
cout << "#" << ++x << ": " ;
cin >> tab[i];
}
cout << "\nBefore sorting: " ;
for (int i = 0; i < quantity; i++)
{
cout << tab[i] << " " ;
}
cout << "\nAfter sorting: " ;
sort(tab, quantity);
for (int i = 0; i < quantity; i++)
{
cout << tab[i] << " " ;
}
}
Last edited on Nov 4, 2010 at 7:38pm UTC
Topic archived. No new replies allowed.