I have to create a program that reads data from a file about census info specifically a last name and the frequency of which last name appeared in census. I have to call a function to read in the data, calls a function to display the data, do a binary search of some names that are inputted by the data file, call a function to sort them by frequency, and then call the previous function to display the last name and frequency.
Okay so I'm completely lost. I've created a few arrays; array 1 contains the names; array 2 contains the frequency; and array 3 contains the names needing to be searched. I need array 3 go through each element and search for a match element in array 1. When it finds the matching element I need it to display that array element number for array 2. (IE array 1 {Anderson, Brown}; array 2{12, 4}; array 3 {Brown} okay take array 3's info which is search for Brown in array 1; Brown is in the second element so dispaly array 2's info in the second element which is 4.) I'm not sure how to do all of that in a binary search.
Also I'm not sure what the document meants by call the previous function to display the name and frequency. Yes I do have a function to sort the arrays; but how do I "call the function to display" rather then make a new function.
I'm also getting lots of fun errors:
pg1.cpp:27: error: too few arguments to function 'int binary(int, std::string, std::string*, int*, std::string*)'
pg1.cpp:69: error: at this point in file
pg1.cpp: In function 'void getInfo(int, std::string*, int*, std::string*)':
pg1.cpp:106: warning: statement has no effect
pg1.cpp: In function 'int binary(int, std::string, std::string*, int*, std::string*)':
pg1.cpp:143: error: 'searchname' was not declared in this scope
pg1.cpp:148: error: no match for 'operator>' in '*(((std::string*)(((long unsigned int)middle) * 8ul)) + searchnames) > searchnames'
Thanks for any help you can give me.
SSR
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// Function prototypes
void getInfo(int, string[], int[], string []);
void displayInfo(int, string[], int[]);
int binary(int, string, string[], int[], string[]);
int sort(int, string[], int[]);
void displayFrequency(int, string [], int []);
const int SIZE = 25;
int main()
{
// Names and Types of inputs
string names[SIZE];
int frequency[SIZE];
string searchnames[5];
string name;
int position;
getInfo(SIZE, names, frequency, searchnames);
displayInfo(SIZE, names, frequency);
cout << endl << "Please enter a name to search for: ";
cin >> name;
position = binary(SIZE, name, names, frequency);
if (position == -1)
cout << name << " is an not in the list" << endl;
else
cout << name << " is a valid charge account number" << endl;
cout << name <<
sort(SIZE, names, frequency);
cout << "Last Names By Frequency Rank" <<endl;
displayFrequency(SIZE, names, frequency);
return 0;
}
void getInfo(int SIZE, string names[], int frequency[], string searchnames[])
{
string temp;
int t=0;
for (int s = 0; s < SIZE; s++)
{
cin >> names[s];
cin >> frequency[s];
}
cin >>temp;
while (temp !="ZZZ")
{
searchnames[t];
t++;
}
}
void displayInfo(int size, string names[], int frequency[])
{
cout << endl << "Last Names Alphabetically" << endl;
cout << endl << "Alete Robison" << endl;
cout << endl << "________________________" << endl;
cout << endl << "Name Frequency" << endl;
for (int s = 0; s < SIZE; s++)
{
cout << setw(14) << names[s];
cout << setw(14) << frequency[s] << endl;
}
}
int binary(int SIZE, string name, string names[], int frequency[], string searchnames [])
{
int first = 0, // First array element
last = SIZE -1, // Last array element
middle, // Mid point of search
position = -1; // Position of search value
bool found = false; // Flag
while (!found && first <= last)
{
middle = (first + last) / 2; // Calculate mid point
if (searchnames[middle]== searchname) // If value is found at mid
{
found = true;
position = middle;
}
else if (searchnames[middle] > searchnames) // If value is in lower half
last = middle - 1;
else
first = middle + 1;
}
return position;
}
int sort(int SIZE, string names[], int frequency[])
{
int count, s, temp;
string temp2;
for (count =0; count<SIZE; count++)
for (s=0; (s<SIZE-1); s++)
if (frequency[s]<frequency[s+1])
{
temp =frequency[s];
frequency[s]=frequency[s+1];
frequency[s+1] = temp;
temp2= names[s];
names[s]= names[s+1];
names[s+1]=temp2;
}
return 0;
}
void displayFrequency(int SIZE, string names[], int frequency[])
{
cout << endl << "Name Frequency" << endl;
for (int s = 0; s < SIZE; s++)
cout << " " << left << setw(10) << names[s]
<< right << setw(8) << frequency[s] << endl;
}