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
|
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
void sort(int a[], int used);
void swap(int& v1, int& v2);
int big(int a[], int start, int used);
const int ARRAYN = 50;
using namespace std;
int main()
{
ifstream fin;
string name;
char ans;
int count[ARRAYN];
int num[ARRAYN];
int j,k=0,used=0,a=0;
cout<< "How would you like to input data \n 'F' for file, 'K' for keyboard.";
cin>> ans;
if ((ans == 'f') || (ans == 'F'))
{
cout<<"Enter file name. \n";
cin>> name;
fin.open(name);
if (fin.fail())
{
cout<<"File open failed!";
exit(1);
}
while(fin >> j)
{
num[k] = j;
k++;
used++;
}
}
else
{
cout<<"Enter up to 50 integers. Type '.1' to signify you're done. \n";
while((num[k] != '.1') || (num[k] != '.1'))
{
cin>> num[k];
k++;
used++;
}
}
sort(num, used);
while(num[a]<used)
{
cout<< num[a];
a++;
}
return 0;
}
void sort(int a[], int used)
{
int bigger=0;
for (int ind= 0; ind < used-1; ind++)
{
bigger=big(a,ind,used);
if (bigger < ARRAYN)
{
swap(a[ind],a[bigger]);
}
else
{
cout << "ERROR: Trying to reach " << bigger << "th position of the array" << endl;
}
}
}
void swap(int& v1, int& v2)
{
int temp;
temp=v1;
v1= v2;
v2 = temp;
}
int big(int a[], int start, int used)
{
int max= a[start],imax=start;
for(int inde= start+1; inde < used; inde++)
{
if (a[inde]>max)
{
max= a[inde];
imax=inde;
}
}
return imax;
}
|