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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#include <iostream>
#include <stdio.h>
using namespace std;
void bubbleSort(int *, int );
void bubbleSort(float *, float);
void bubbleSort(char *, char);
void printElements(int *,int );
void printElements(float *,float ); // You declare "size" as an int in [code]main[/main], but seem to use it
void printElements(char *,char ); // as an int, a float, and a char.
// While this may work in your compiler,
// it won't always work, and I'd recommend changing the arguments
// so that each printElements function's second argument is an int
// Do the same with each bubbleSort
int main ()
{
int size, i, num[100];
float num1[100];
char letter[100];
cout << "Enter how many interger you want:"<< endl;
cin >> size;
cout << "Enter that amount of numbers: " << endl;
for( i = 0; i <size; i++){
cin >> num[i];
bubbleSort(num , size);
}
size = 0; // Unnecessary
cout << "How many decimals do you want: " << endl;
cin >> size;
cout << "Enter that amount"<<endl;
for( i = 0; i <size; i++){
cin >> num1[i];
bubbleSort(num1 ,size);
}
size= 0; // Unnecessary
cout << "How many letter do you want: " << endl;
cin >> size ;
cout<<"Enter that amount:"<< endl;
for( i = 0; i <size; i++){
cin >> letter[i];
bubbleSort(letter,size);
}
printElements(num,size);
printElements(num1,size);
printElements(letter,size);
system("PAUSE"); // Not sure why you need this
return 0;
}
void bubbleSort(int *x, int length)
{
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<i;j++)
{
if(x[i]>x[j])
{
int temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
}
void bubbleSort(float *x, float length) // See above comment about "size"
{
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<i;j++)
{
if(x[i]>x[j])
{
float temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
}
void bubbleSort(char *x, char length) // See above comment about "size"
{
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<i;j++)
{
if(x[i]>x[j])
{
float temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
}
void printElements(int *x,int length)
{
int i=0;
for(i=0;i<10;i++)
cout<<x[i]<<", "; // You can condense these two cout lines: cout << x[i] << ", " << endl;
cout << endl;
}
// Keep in mind, if the goal was to print numbers to the screen one-per-line, your code is fine, but if you want
// to put them all on the same line, the above cout lines can NOT be condensed, and the "cout << endl;" line must
// be moved outside of (below) the for loop's closing bracket
void printElements(float *x,float length)
{
int i=0;
for(i=0;i<10;i++)
cout<<x[i]<<", ";
cout << endl; // Same comments as above
}
void printElements(char *x,char length)
{
int i=0;
for(i=0;i<10;i++)
cout<<x[i]<<", ";
cout << endl; // Same comments as above
}
|