The first part I'm having trouble with is having the user type in the file name to read in to the array.
Second, I need to count the occurance of each element of the array and display the element next to it's number of occurances.
The text file looks like this: 1 2 3 2 6 7 4 5 3 7 4 5 2 4 7
Please help me!
My code looks like this: (Don't laugh)
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include<string>
using namespace std;
void sort(char a[], int number_used);
void swap_values(char& v1, char& v2);
int index_of_smallest(const char a[], int start_index, int number_used);
int main()
{
char input_file[21];
int number_used;
ifstream in_stream;
in_stream.open(input_file);
cout<< "Please enter the name of the input file.\n";
cin>> input_file;
cin.read(input_file, 21);
sort(input_file, number_used);
while (!in_stream.eof())
{
for (int index = 0; index < number_used; index++)
cout<< input_file[index] << " \n";
in_stream.close();
}
return 0;
}
void sort(char a[], int number_used)
{
int index_of_next_smallest;
for (int index = 0; index < number_used - 1; index++)
{
index_of_next_smallest = index_of_smallest(a, index, number_used);
swap_values(a[index], a[index_of_next_smallest]);
}
}
void swap_values(char& v1, char& v2)
{
int num;
num = v1;
v1 = v2;
v2 = num;
}
int index_of_smallest(const char a[], int start_index, int number_used)
{
int min = a[start_index],
index_of_min = start_index;
for (int index = start_index + 1; index < number_used; index++)
if (a[index] < min)
{
min = a[index];
index_of_min = index;
}
return index_of_min;
}
--------------------------------------------------------------------------------