Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey can any of y\'all help me figure out why i am gettin an error when i call my

ID: 3771346 • Letter: H

Question

Hey can any of y'all help me figure out why i am gettin an error when i call my sort function? I added the comments where the error is occuring in the main.cpp

HEADER.CPP
#include <stdio.h>
#include "Header.h"

//constructor

FemaleNames::FemaleNames()

{
  
this->rank=0;
  
this->name="";
  
}

//accessor methods of class FemaleNames

int FemaleNames::getRank()

{
  
return this->rank;
  
}

string FemaleNames::getName()

{
  
return this->name;
  
}

//mutator methods of class FemaleNames

void FemaleNames::setRank(int rank)

{
  
this->rank = rank;
  
}

void FemaleNames::setName(string name)

{
  
this->name = name;
  
}

HEADER.H
#ifndef Header_H

#define Header_H

#include<iostream>

#include<string>

using namespace std;

class FemaleNames

{
  
private:
  
int rank;
  
string name;
  
public:
  
FemaleNames();
  
int getRank();
  
string getName();
  
void setRank(int rank);
  
void setName(string name);
  
};

#endif

MAIN.CPP

#include "Header.h"

#include<iostream>

#include <fstream>

#include <iostream>

#include <string>

#include <time.h>

#include<ctime>

using namespace std;

void sortNames(int[],int,int);

int linearSearch (FemaleNames *fnames, string searchKey, int count);

int binarySearch(FemaleNames *fnames, string searchKey, int count);

string toUpper(string name);
const int SIZE=1000;

int main()

{
  
FemaleNames fnames[SIZE];
  
string name;
  
int value;
  
int count=0;
  
string searchKey;
  
ifstream infile("CommonFemaleNames.txt");
  
if(infile)
  
{
  
infile>>value;
  
infile>>name;
  
fnames[count].setName(name);
  
fnames[count].setRank(value);
  
while(!infile.eof())
  
{
  
count++;
  
infile>>value;
  
infile>>name;
  
fnames[count].setName(name);
  
fnames[count].setRank(value);
  
}
  
}
  
infile.close();
  
sortNames(fnames[], 0, SIZE-1); //THE ERROR IS HERE IT TELLS ME IT "EXPECTED AN EXPRESSION"

//INSIDE THE[], AND IF I PUT A VALUE INSIDE THE [] I GET "NO MATCHING FUNCTION FOR CALL TO

//'SORTNAMES' " I AM LOST ON HOW TO FIX THIS ERROR
  
for(int i=0;i<=count;i++)
  
cout<<fnames[i].getName()<<" "<<fnames[i].getRank()<<endl;
  
clock_t startClock,finishClock;
  
double timeCount;
  
cout << "Enter name to find: " << endl;
  
cin >> searchKey;
  
searchKey=toUpper(searchKey);
  
cout << searchKey<<endl;
  
startClock = clock()/1000;
  
int pos1=linearSearch(fnames,searchKey,count);
  
finishClock = clock()/1000;
  
timeCount=finishClock-startClock;
  
if(pos1!=-1)
  
cout << "The name is ranked:# "<<pos1+1 << endl;
  
else
  
cout << "Name not found" << endl;
  
cout<< "The time taken to find the name using linear search is: " << timeCount << " nanoseconds." << endl;
  
startClock = clock()/1000;
  
int pos2=binarySearch(fnames,searchKey,count);
  
finishClock = clock()/1000;
  
timeCount=finishClock-startClock;
  
if(pos2!=-1)
  
cout << "The name is ranked:# "<<pos2+1 << endl;
  
else
  
cout << "Name not found" << endl;
  
cout<< "The time taken to find the name using binary search is: " << timeCount << " nanoseconds." << endl;
  
system("pause");
  
return 0;
  
}

string toUpper(string name)

{
  
for(int i=0;i<name.size();i++)
  
{
  
name[i]=toupper(name[i]);
  
}
  
return name;
  
}

void sortNames(int fnames[],int left, int right)

{
int i=left,j=right;
int temp;
int pivot=fnames[(left+right)/2];
  
while (i<=j) {
while (fnames[i]<pivot)
i++;
while (fnames[j]>pivot)
j--;
if (i<=j) {
temp=fnames[i];
fnames[i]=fnames[j];
fnames[j]=temp;
i++;
j--;
}
};
if (left<j)
sortNames(fnames,left,j);
if (i<right)
sortNames(fnames,i,right);
}


int linearSearch (FemaleNames *fnames, string searchKey, int count) {
  
for (int i = 0; i <=count; ++i)
  
{
  
if (fnames[i].getName().compare(searchKey) == 0)
  
return i;
  
}
  
return -1;
  
}

int binarySearch(FemaleNames *fnames, string searchKey, int count)

{
  
int first = 0,last = count+1,middle,position = -1;
  
bool found = false;
  
while (!found && first < last)
  
{
  
middle = (first + last) / 2;
  
if ((fnames[middle].getName().compare(searchKey)) == 0)
  
{
  
return middle;
  
}
  
else if ((fnames[middle].getName().compare(searchKey)) > 0)
  
last = middle - 1;
  
else
  
first = middle + 1;
  
}

Explanation / Answer

sortNames(fnames[], 0, SIZE-1); //THE ERROR IS HERE IT TELLS ME IT "EXPECTED AN EXPRESSION"

//INSIDE THE[], AND IF I PUT A VALUE INSIDE THE [] I GET "NO MATCHING FUNCTION FOR CALL TO

//'SORTNAMES' " I AM LOST ON HOW TO FIX THIS ERROR

Error1: When you pass array in function then you simply write its name, DONT USE []

  sortNames(fnames, 0, SIZE-1); is correct syntax of function calling

Error 2: See the data type of fnames. You have declared data type of 1st arg. of sortNames function to be int[] but you have passed fnames during its calling which is of FemaleNames[] type

I have corrected some part of code. See if this is running


#ifndef Header_H

#define Header_H

#include<iostream>

#include<string>

using namespace std;

class FemaleNames

{
  
private:
  
int rank;
  
string name;
  
public:
  
FemaleNames();
  
int getRank();
  
string getName();
  
void setRank(int rank);
  
void setName(string name);
  
};

#endif


#include <stdio.h>
#include "Header.h"

//constructor

FemaleNames::FemaleNames()

{
  
this->rank=0;
  
this->name="";
  
}

//accessor methods of class FemaleNames

int FemaleNames::getRank()

{
  
return this->rank;
  
}

string FemaleNames::getName()

{
  
return this->name;
  
}

//mutator methods of class FemaleNames

void FemaleNames::setRank(int rank)

{
  
this->rank = rank;
  
}

void FemaleNames::setName(string name)

{
  
this->name = name;
  
}


#include "Header.h"

#include <iostream>

#include <fstream>

#include <iostream>

#include <string>

#include <time.h>

#include<ctime>

using namespace std;

void sortNames(FemaleNames[],int,int);

int linearSearch (FemaleNames *fnames, string searchKey, int count);

int binarySearch(FemaleNames *fnames, string searchKey, int count);

string toUpper(string name);
const int SIZE=1000;

int main()

{
  
FemaleNames fnames[SIZE];
  
string name;
  
int value;
  
int count=0;
  
string searchKey;
  
ifstream infile("CommonFemaleNames.txt");
  
if(infile)
  
{
  
infile>>value;
  
infile>>name;
  
fnames[count].setName(name);
  
fnames[count].setRank(value);
  
while(!infile.eof())
  
{
  
count++;
  
infile>>value;
  
infile>>name;
  
fnames[count].setName(name);
  
fnames[count].setRank(value);
  
}
  
}
  
infile.close();
  
sortNames(fnames, 0, SIZE-1);
//THE ERROR IS HERE IT TELLS ME IT "EXPECTED AN EXPRESSION"
//INSIDE THE[], AND IF I PUT A VALUE INSIDE THE [] I GET "NO MATCHING FUNCTION FOR CALL TO
//'SORTNAMES' " I AM LOST ON HOW TO FIX THIS ERROR
  
for(int i=0;i<=count;i++)
  
cout<<fnames[i].getName()<<" "<<fnames[i].getRank()<<endl;
  
clock_t startClock,finishClock;
  
double timeCount;
  
cout << "Enter name to find: " << endl;
  
cin >> searchKey;
  
searchKey=toUpper(searchKey);
  
cout << searchKey<<endl;
  
startClock = clock()/1000;
  
int pos1=linearSearch(fnames,searchKey,count);
  
finishClock = clock()/1000;
  
timeCount=finishClock-startClock;
  
if(pos1!=-1)
  
cout << "The name is ranked:# "<<pos1+1 << endl;
  
else
  
cout << "Name not found" << endl;
  
cout<< "The time taken to find the name using linear search is: " << timeCount << " nanoseconds." << endl;
  
startClock = clock()/1000;
  
int pos2=binarySearch(fnames,searchKey,count);
  
finishClock = clock()/1000;
  
timeCount=finishClock-startClock;
  
if(pos2!=-1)
  
cout << "The name is ranked:# "<<pos2+1 << endl;
  
else
  
cout << "Name not found" << endl;
  
cout<< "The time taken to find the name using binary search is: " << timeCount << " nanoseconds." << endl;
  
system("pause");
  
return 0;
  
}

string toUpper(string name)

{
  
for(int i=0;i<name.size();i++)
  
{
  
name[i]=toupper(name[i]);
  
}
  
return name;
  
}

void sortNames(FemaleNames fnames[],int left, int right)

{

int i=left,j=right;
FemaleNames temp;
string pivot=fnames[(left+right)/2].getName();
  
while (i<=j) {
while (fnames[i].getName()<pivot)
i++;
while (fnames[j].getName()>pivot)
j--;
if (i<=j) {
temp=fnames[i];
fnames[i]=fnames[j];
fnames[j]=temp;
i++;
j--;
}
};
if (left<j)
sortNames(fnames,left,j);
if (i<right)
sortNames(fnames,i,right);

}

int linearSearch (FemaleNames *fnames, string searchKey, int count) {
  
for (int i = 0; i <=count; ++i)
  
{
  
if (fnames[i].getName().compare(searchKey) == 0)
  
return i;
  
}
  
return -1;
  
}

int binarySearch(FemaleNames *fnames, string searchKey, int count)

{
  
int first = 0,last = count+1,middle,position = -1;
  
bool found = false;
  
while (!found && first < last)
  
{
  
middle = (first + last) / 2;
  
if ((fnames[middle].getName().compare(searchKey)) == 0)
  
{
  
return middle;
  
}
  
else if ((fnames[middle].getName().compare(searchKey)) > 0)
  
last = middle - 1;
  
else
  
first = middle + 1;
  
}
}