Question

In: Computer Science

Below is the code for the isSmaller() member function: bool Location::isSmaller(const Location & r) const {...


Below is the code for the isSmaller() member function:

bool Location::isSmaller(const Location & r) const
{
// ORDER OF COMPARISONS ==> 1st country; 2nd state; 3rd city

int a = strcmp(country,r.country); // need strcmp
// country and r.country are arrays and in C++ the expression country < r.country will compare address, not data
// the data must be compared cell by corresponding cell in the 2 arrays, which takes place in the strcmp() function
// There are three possible return cases:
// - a negative number, country is alphabetically smaller than r.country (appears earlier in a Dictionary)
// - 0 value, the two C++ strings are identical
// - a positive value, the first parameter is alphabetically larger than the second parameter
if ( a < 0 ) return true;
else if ( a > 0 ) return false;

// at this point in the logic, the countries are the same
int b = strcmp(state,r.state); // need strcmp
if ( b < 0 ) return true;
else if ( b > 0 ) return false;

// at this point in the logic, the countries are the same
// and the states are the same
return ( city < r.city ); // < works for string objects
}

1. In location.h prototype a member function, isEqual, to check for equality, i.e. all 3 corresponding fields must be equal
2. In location.cpp write the code for the isEqual member function.
Note: arrays of char are compared using strcmp which string objects are compared using the normal operators: <, ==, >
3. In lecture4driver.cpp test your isEqual function, once to get true and once to get false.

Submit as 3 separate files ( no zip files): header file, implementation file, and driver file

Solutions

Expert Solution

1)

location.h

#include <string.h>
#define MAX 20
using namespace std;

class Location{
char country[MAX];
char state[MAX];
string city;
public:
Location(char[],char[],string);
bool isSmaller(const Location &)const;
bool isEqual(const Location &)const;

};

2) location.cpp

#include <iostream>
#include <string.h>
#include "location.h"

using namespace std;

Location::Location(char a[],char b[],string c){
strcpy(country,a);
strcpy(state,b);
city=c;
}

bool Location::isEqual(const Location & r) const
{
// ORDER OF COMPARISONS ==> 1st country; 2nd state; 3rd city

int a = strcmp(country,r.country); // need strcmp
// country and r.country are arrays and in C++ the expression country < r.country will compare address, not data
// the data must be compared cell by corresponding cell in the 2 arrays, which takes place in the strcmp() function
// There are three possible return cases:
// - a negative number, country is alphabetically smaller than r.country (appears earlier in a Dictionary)
// - 0 value, the two C++ strings are identical
// - a positive value, the first parameter is alphabetically larger than the second parameter
if ( a < 0 || a > 0) return false; /// countries are not equal


// at this point in the logic, the countries are the same
int b = strcmp(state,r.state); // need strcmp
if ( b < 0 || b > 0) return false; /// states are not equal


// at this point in the logic, the countries are the same
// and the states are the same
return ( city == r.city ); // < works for string objects if city name same then returns true
}

3) lecture4driver.cpp

#include <iostream>
#include <string.h>
using namespace std;
#include "location.h"

int main()
{
char a[]="INDIA";
char b[]="GUJARAT";
string c="PATAN";

Location A(a,b,c);
Location B(a,b,"PATAN");

if(B.isEqual(A)){
cout<<"\n\nBoth location A and B are equal ";
}
else{
cout<<"\n\nlocation A and B are NOT equal ";
}

Location C(a,b,"Surat");
Location D(a,b,"Rajkot");
///only cities differ in C and D
if(C.isEqual(D)){
cout<<"\n\nBoth location C and D are equal ";
}
else{
cout<<"\n\nlocation C and D are NOT equal ";
}
///example 3
///here states differ from one another
Location E(a,b,"Surat");
Location F(a,"Goa","Surat");
if(E.isEqual(F)){
cout<<"\n\nBoth location E and F are equal ";
}
else{
cout<<"\n\nlocation E and F are NOT equal \n";
}
return 0;
}




Related Solutions

In c++ A binary search member function of SortedType has the following prototype: void.SortedType::BinarySearch(int value, bool&...
In c++ A binary search member function of SortedType has the following prototype: void.SortedType::BinarySearch(int value, bool& found); Write the function definition as a recursive search, assuming an array-based implementation. Add a main function as a driver to test the BinarySearch. Test the edge cases.
Create a C module convertTo_csv.c that will implement the function loadAndConvert(const char* file) The code must...
Create a C module convertTo_csv.c that will implement the function loadAndConvert(const char* file) The code must be split into 2 files (.h file and a .c file). The convertTo_csv.c will have the function implementation in ti and the The convertTo_csv.h file will have the declaration. One argument will be given to the function, that is the name of the input file that needs to be converted. A function will create a new csv file called output.csv Know that the loadAndConvert...
(C++ programming) Assignment *Circle Class -Radius r (private) as an attribute variable -Member function -Get(): Function...
(C++ programming) Assignment *Circle Class -Radius r (private) as an attribute variable -Member function -Get(): Function that returns r value of property variable -Put(int d): A function that stores d in attribute variable r *Declare a one-dimensional array of type Circle and in each array element Read and store integers from standard input device *Declare the swap() function to swap two elements with each other *Write a program that sorts the elements of a one-dimensional array of circle type in...
5. In the following code snippet, which member function of the class Car is called first?...
5. In the following code snippet, which member function of the class Car is called first? class Car { public: void start(); void accelerate(double acc_speed); void stop(); double get_speed() const; Car(); private: double speed; }; Car::Car() { speed = 0; } void Car::start() { accelerate(get_speed() + 10); } void Car::stop() { speed = 0; } void Car::accelerate(double acc_speed) { speed = speed + acc_speed; } double Car::get_speed() const { return speed; } int main() { Car c1; c1.start(); c1.accelerate(10); c1.get_speed();...
code from assignment 1 #include #include using namespace std; const int nameLength = 20; const int...
code from assignment 1 #include #include using namespace std; const int nameLength = 20; const int stateLength = 6; const int cityLength = 20; const int streetLength = 30; int custNomber = -1; // Structure struct Customers { long customerNumber; char name[nameLength]; char state[stateLength]; char city[cityLength]; char streetAddress1[streetLength]; char streetAddress2[streetLength]; char isDeleted = 'N'; char newLine = '\n'; int zipCode; }; int main() { ofstream file; file.open("Customers.dat", fstream::binary | fstream::out); char go; long entries = 0; struct Customers data; do...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const Point& p2) { return this->x == p2.x and this->y == p2.y; } bool operator!=(const Point& p2) { return this->x != p2.x or this->y != p2.y; } friend ostream &operator<<( ostream &out, const Point &P ) { out << "(" << P.x << ", " << P.y << ")"; return out; } friend istream &operator>>( istream &in, Point &P ) { char d1, d2, d3;...
Sample Code and Models Run each of the models below and explain the code function and...
Sample Code and Models Run each of the models below and explain the code function and your findings for each system, do they agree/disagree with what you understand and why ?? Matlab Code % Winter 2018 Control Engineering % Lab No.3 - Root Locus problems % Mark Clarke clear s = tf('s') K = 1150; %Proportional Controller Gain, May need to be altered? % Enter Model 1 % This is a model of a simple 2nd order with no zeros...
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary...
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary string and iteratively constructs a decimal value (val) and width of each binary pattern (separated by spaces), until a space or a null character ('\0') is encountered in the string. Once a space or a null character is found, this function should call the assembly code (decode_morse()) to obtain the corresponding ASCII value, for the current val and width, and place the ASCII value...
Question about user defined function.(language:c++) If I were to make function, for example, bool Function(char i){...
Question about user defined function.(language:c++) If I were to make function, for example, bool Function(char i){ If(i=='a') return true; return false; } In this situation, I want to use this function for main1.cpp, main2.cpp, and main3.cpp. To do that, I want to use this function as a header file to save time. In Funtion.h file, I write bool Function(char i); between #ifndef,#define and #endif. (1) In Function.cpp file, I write the body of Function(char i) after I put #Function.h (2)...
Answer IN R CODE to get the following. Using the data below, Create a scatterplot of...
Answer IN R CODE to get the following. Using the data below, Create a scatterplot of y vs x Fit a simple linear regression model using y as the response and plot the regression line (with the data) Test whether x is a significant predictor and create a 95% CI around the slope coefficient. Report and interpret the coefficient of determination. For x=20, create a CI for E(Y|X=20). For x=150, can you use the model to estimate E(Y|X=150)? Discuss. Does...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT