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 143 144 145 146 147 148 149
|
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <string>
#include <cctype>//library to use 'toupper', 'tolower'....
#include <vector>
#include <fstream>
using namespace std;
using std::ifstream;
using std::ofstream;
using std::endl;
using std::ios;
void input(istream& input, int d[], int size, int& number_used);
void print(const int a[], int number_used);
void sort_array(int a[], int number_used);
void track_and_count(int a[], int number_used);
const int MAX=50;
int main()
{
char choice, input_file[20];
int file_or_key, number_used, data[MAX];
ifstream in;
do
{
cout<<"Do you want to enter the data manualy or from a file?\n";
cout<<"Press '1' for keyboard, or '2' file\n";
cin>>file_or_key;
if(file_or_key==1)
{
cout<<"Enter any number of integers less than fifty\n";
input(cin,data,MAX,number_used);
cout<<"You have entered the following data\n";
print(data,number_used);
track_and_count(data,number_used);
// cout<<"After sorting you have the following\n";
// sort_array(data,number_used);
print(data, number_used);
}
else if(file_or_key==2)
{
cout<<"Enter the name of the file you wish to open\n";
cin>>input_file;
in.open(input_file);
if(in.fail())
{
cout<<"file failed to open\n";
exit(1);
}
//code goes here
}
else
{
cout<<"Incorrect option\n";
exit(1);
}
cout<<"Again?\n";
cin>>choice;
}while(choice=='y' || choice=='Y');
exit(1);
_getch();
}
void track_and_count(int a[], int number_used)
{
int temp, counter=0, j=0,n=0;
bool appeared;
while(j<number_used)
{
appeared=false;
temp=a[j];
for(int i=j+1;i<number_used;i++)
{
if(temp==a[i])
{
/////////////////////////////////////
//problem here!!
appeared=true;
counter++;
//problem here!!
//problem here!!
//problem here!!
//problem here!!
/////////////////////////////////////
}
}
if(!appeared)
cout<<"Temp is "<<temp<<" and occurs "<<counter+1<<" times "<<endl;
j++;
counter=0;
}
}
void input(istream& input, int d[], int size, int& number_used)//input array
{
int next, index=0;
input>>next;
while(next>=-999 && index<size)
{
d[index]=next;
index++;
input>>next;
}
number_used=index;
}
void print(const int a[], int number_used)//print array
{
for(int i=0;i<number_used;i++)
{
cout<<a[i]<<",";
}
cout<<endl;
}
void sort_array(int a[], int number_used)//bubble sort
{
bool swapped=true;
int temp;
while(swapped)
{
swapped=false;
for(int i=0;i<number_used;i++)
{
if(a[i+1]<a[i])
{
swap(a[i+1],a[i]);
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
swapped=true;
}
}
}
}
|