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

Files, Vectors & LINQ Our most recent lecture(s) covered a lot of new material.

ID: 3873541 • Letter: F

Question

Files, Vectors & LINQ
Our most recent lecture(s) covered a lot of new material. This assignment gives you
the opportunity to truly understand some of the new concepts introduced therein.
This assignment includes a total of 4 files in Contacts.zip:


• The instructions (this document)
• The data: us-100000.csv (holds 100,000 contacts)
• Cpplinq.hpp: provides LINQ functions
• Contacts.cpp: the main program.

THE CONTACTS.CPP:

#include <string> // Include the string library.

#include <fstream> // Include to manipulate files.

#include <vector> // Include to use vectors.

#include <sstream> // Include to use stringstream.

#include <iostream>

#include <iomanip>

#include"cpplinq.hpp"

using namespace std;

class contact {

public:

string firstName;

string lastName;

string companyName;

string address;

string city;

string county;

string state;

string zip;

string phone1;

string phone2;

string email;

string web;

void out() {

cout << this->lastName << ", " << this->firstName << endl;

cout << "---------------------------------------------------" << endl;

cout << this->companyName << endl;

cout << this->address << endl;

cout << this->city << endl;

cout << this->county << endl;

cout << this->state << endl;

cout << this->zip << endl;

cout << this->phone1 << endl;

cout << this->phone2 << endl;

cout << this->email << endl;

cout << this->web << endl;

cout << endl;

}

;

};

int computes_a_sum();

void loadContacts();

void loadContacts(vector<contact>&contacts);

void printContacts(vector<contact>&contacts, int n);

vector<contact> findContactsByLastName(vector<contact>&contacts, string name);

int main() {

cout << "Loading contacts. Please wait..." << endl; // prints Contacts

vector<contact> contacts;

loadContacts(contacts);

cout << "We loaded " << contacts.size() << " contacts" << endl;

vector<contact> results = findContactsByLastName(contacts, "M");

cout << "We found " << results.size() << " contacts!" << endl<<endl;

// print first five results

printContacts(results, 5);

return 0;

}

int computes_a_sum()

{

using namespace cpplinq;

int ints[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 4 };

auto result = from_array(ints) >> where([](int i) {return i%2 ==0;}) // Keep only even numbers

>> sum() // Sum remaining numbers

;

return result;

}

// Return a vector containing contacts where last name

// begins with or matches the criteria.

vector<contact> findContactsByLastName(vector<contact>&contacts,string criteria)

{

using namespace cpplinq;

auto result =

from(contacts)

>> where(

[=](contact const & c) {return c.lastName.find(criteria)==0;})

>> to_vector();

return result;

}

// Prints contacts to console

void printContacts(vector<contact>&contacts, int n=-1)

{

if (contacts.size()==0)

{

cout << "[nothing to print.]"<<endl<<endl;

return;

}

if (n==-1) // default to printing all contacts

n = contacts.size();

for (int i = 0; i < n; i++) {

contacts[i].out();

}

}

// load contacts from local delimited file

void loadContacts(vector<contact>&contacts)

{

ifstream file("us-100000.csv");

contact c;

while (file.good()) {

c = contact();

getline(file, c.firstName, ',');

getline(file, c.lastName, ',');

getline(file, c.companyName, ',');

getline(file, c.address, ',');

getline(file, c.city, ',');

getline(file, c.county, ',');

getline(file, c.state, ',');

getline(file, c.zip, ',');

getline(file, c.phone1, ',');

getline(file, c.phone2, ',');

getline(file, c.email, ',');

getline(file, c.web, ' ');

contacts.push_back(c);

}

std::cout << "File Finished" << std::endl;

}

THE CPPLINQ.HPP:

// Copyright (c) Mårten Rånge.
// ----------------------------------------------------------------------------------------------
// This source code is subject to terms and conditions of the Microsoft Public License. A
// copy of the license can be found in the License.html file at the root of this distribution.
// If you cannot locate the Microsoft Public License, please send an email to
// dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
// by the terms of the Microsoft Public License.

(too lengthy to paste, but if need me to paste please tell me)


Cpplinq.hpp and Contacts.cpp should reside together in the same folder. Us-
100000.csv contains the actual data in comma-delimited form. You will need to
place this file in your /debug or /release folder. Since the compiled program runs
from that folder, my code will only look in the same folder the program is running
from. Where you place this file might depend on your particular environment. Use
Google to learn where your program will look by default. If you cannot get this to
work properly, you may avoid pathing issues by supplying a fully qualified path to
where the data file actually resides on your system. Note that in some environments
you can just leave the file where the cpp file is.


First, simply use the source code as-is. That is, try to compile the code without
changing it, and run it. You should see the following output:


Loading contacts. Please wait...
File Finished
We loaded 1000001 contacts
We found 85268 contacts!
Morasca, Simona
---------------------------------------------------
Chapman| Ross E Esq
3 Mcauley Dr
Ashland
Ashland
OH
44805
419-503-2484
419-800-6759
simona@morasca.com
http://www.chapmanrosseesq.com
Marrier, Kris
---------------------------------------------------
King| Christopher A Esq
228 Runamuck Pl #2808
CS3060-001 Wk 06; HW 01: Files, Vectors & LINQ - Due: Tuesday (03:00pm) Oct 03, 2017
Baltimore
Baltimore City
MD
21224
410-655-8723
410-804-4694

http://www.kingchristopheraesq.com
Maclead, Abel
---------------------------------------------------
Rangoni Of Florence
37275 St Rt 17m M
Middle Island
Suffolk
NY
11953
631-335-3414
631-677-3675
http://www.rangoniofflorence.com
Malet, Blair
---------------------------------------------------
Bollinger Mach Shp & Shipyard
209 Decker Dr
Philadelphia
Philadelphia
PA
19132
215-907-9111
215-794-4519
bmalet@yahoo.com
http://www.bollingermachshpshipyard.com
Mastella, Marjory
---------------------------------------------------
Vicon Corporation
71 San Mateo Ave
Wayne
Delaware
PA
19087
610-814-5533
610-379-7125
mmastella@mastella.com
http://www.viconcorporation.com


The program loads all the contacts, searches for all contacts where last name begins
with “M”, and displays the first 5 results out of 85,268 contacts found.

WHAT I AM SUPPOSED TO DO:

Now It’s Your Turn!
Modify the code I provided to you so that you can answer the following questions.
Submit ALL your answers in a Word doc, below the repeated question, along with
your source code. Zip it all up. DO NOT INCLUDE THE DATA FILE IN THE ZIP! If
you do, points will be deducted to bring you down at least one letter grade.


To learn more about CppLinq, go here: https://cpplinq.codeplex.com/


Provide Answers for the Following Questions:
(1) How many contacts are there where last name begins with “st”?
(2) How many contacts are there where last name begin with “z”?
(3) How many contacts live in CA?
(4) How many contacts are there who have emails with aol.com?
(5) How many contacts are there with first names starting with “ba”
while their last names start with “f”?
(6) How many contacts are there with first names starting with “t”
while their last names start with “f” and who live in “MI”?
(7) Sort your contacts list by State. List the first 10 contacts you
see after performing the sort.
(8) Sort contacts by state and city. List the first 10 contacts you
see after performing the sort.
(9) Sort contacts by state, city, and zip. List the first 10
contacts you see after performing the sort.


Note:
Understand that if I say for problem 9 to sort by state, city, and zip this means that
you are performing ONE sort. This sort will list people in order by state first, city
second, and then by zip.
So, if you had 100 people who lived in MI, people who live in the city of Ann Arbor
would be listed before those who live in the city of Azalia. Furthermore, those who
live in Ann Arbor with zip code of 48103 would be listed before those who live in
Ann Arbor with zip code of 48109. Problem 9 is an example of a report with a first,
second, and third sort order.

(I HAVE ANSWERS FOR THESE, BUT I NEED TO KNOW IF I AM SOLVING THEM CORRECTLY. IF POSSIBLE, CAN CODE BE PROVIDED FOR EACH PROBLEM? IF NOT, A GENERAL DESCRIPTION OF HOW TO GET THERE? THANK YOU)

Explanation / Answer

(1) How many contacts are there where last name begins with “st”?

Sol:

Use the existing logic and use count function to count the list of matched contracts as follows:

// Return number of contacts containing contacts where last name begins with ‘st’

vector<contact> findContactsByLastName(vector<contact>&contacts,string criteria)

{

using namespace cpplinq;

auto numberofcontacts =

from(contacts)

>> where(

[=](contact const & c) {return c.lastName.find(criteria)==0;})

.count();

return result;

}

// print number of contacts

Void numberofcontacts(int n)

Std;cout << std:endl<< number of contacts" << n<< std:endl;

return;


(2) How many contacts are there where last name begin with “z”?

    Sol: use the same logic as first question.
(3) How many contacts live in CA?

Sol:

   // Return number of contacts containing contacts wo live in ‘CA’

vector<contact> findContactsByPlace(vector<contact>&contacts,string criteria)

{

using namespace cpplinq;

auto numberofcontacts =

from(contacts)

>> where(

[=](contact const & c) {return c.county.find(criteria)==0;})

.count();

return result;

}

// print number of contacts

Void numberofcontacts(int n)

Std;cout << std:endl<< number of contacts" << n<< std:endl;

return;


(4) How many contacts are there who have emails with aol.com?

Sol:

Use existing logic to match the string and return the vector.
(5) How many contacts are there with first names starting with “ba”
while their last names start with “f”?

Sol:

// Return number of contacts containing contacts with first name begins with ‘ba’ and last name with ‘f’.

vector<contact> findContactsByFirstLastName(vector<contact>&contacts,string criteria1, String criteria2)

{

using namespace cpplinq;

auto numberofcontacts =

from(contacts)

>> where(

[=](contact const & c) {return c.lastName.find(criteria1)==0 && c.FirstName.find(criteria2);})

.count();

return result;

}

// print number of contacts

Void numberofcontacts(int n)

Std;cout << std:endl<< number of contacts" << n<< std:endl;

return;


(6) How many contacts are there with first names starting with “t”
while their last names start with “f” and who live in “MI”?

Sol:

// Return number of contacts containing contacts with first name begins with ‘t’ and last name with ‘f’ and live in ‘MI’.

vector<contact> findContactsByFirstLastNamePlace(vector<contact>&contacts,string criteria1, String criteria2,string criteria3)

{

using namespace cpplinq;

auto numberofcontacts =

from(contacts)

>> where(

[=](contact const & c) {return c.lastName.find(criteria1)==0 && c.FirstName.find(criteria2)&& c.county.find(criteria3);})

.count();

return result;

}

// print number of contacts

Void numberofcontacts(int n)

Std;cout << std:endl<< number of contacts" << n<< std:endl;

return;


(7) Sort your contacts list by State. List the first 10 contacts you
see after performing the sort.

Sol:

vector<contact> sortContactsBystate(vector<contact>&contact)

{

using namespace cpplinq;

auto result =

from(contacts)

>> where(

[=](contact const & c) {return ;})

Orderby_ascending (contact const & c) {return c.state;})

>> to_vector();

return result;

}

// print first 10 results

printContacts(results, 10);

return 0;

}


(8) Sort contacts by state and city. List the first 10 contacts you
see after performing the sort.

Sol:

//sort by state and city

vector<contact> sortContacts(ByStateCityvector<contact>&contact)

{

using namespace cpplinq;

auto result =

from(contacts)

>> where(

[=](contact const & c) {return ;})

Orderby_ascending (contact const & c) {return c.state;})

Orderby_ascending (contact const & c) {return c.city;})

>> to_vector();

return result;

}

// print first 10 results

printContacts(results, 10);

return 0;

}


(9) Sort contacts by state, city, and zip. List the first 10
contacts you see after performing the sort.

Sol:

//sort by state and city and zip

vector<contact> sortContacts(ByStateCityvector<contact>&contact)

{

using namespace cpplinq;

auto result =

from(contacts)

>> where(

[=](contact const & c) {return ;})

Orderby_ascending (contact const & c) {return c.state;})

Orderby_ascending (contact const & c) {return c.city;})

thenby_descending (contact const & c) {return c.zip;})

>> to_vector();

return result;

}

// print first 10 results

printContacts(results, 10);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote