Starting from:

$24.99

Searching and Sorting Arrays_Lab 8 Solution

– Lab 08 –Searching and Sorting Arrays

Goals:

·         Searching through arrays

·         Sorting arrays with bubble and selection sorts

Development Environment: (all students must use Visual Studios)

Skills:arrays, searching sorting, functions,

Reading: Chap 7, Chap 8

Deliverables:1) This lab withtwo screen shots2) lastnameFirstLab08.cpp

 

Part I – Skills Practice (10 points)

·         Open a new project in Visual Studios.  Call the project Lab08.  Don’t forget to uncheck the Pre-compiled headers and Security Development Lifecycle checks.  Check the box for an empty project

You will be creating 4 files called lab08a.cpp, Car.h and CarFunctions.h, CarFunctions.cpp

In Car.h.  If you start with the working files from a working Lab07, you will have a lot less to type.

Create a file called Car.h under header files

#ifndef CAR_H

#defineCAR_H

#include<string

usingnamespacestd;

structCar

{

     string make;

     string model;

     int year;

};

#endif//CAR_H

 

 

Remember in your Car.h, you should remove the #pragma once, and add the duplicate guard:

·         Now create a file called CarFunctions.hunder header files

 

#ifndef CARFUNCTIONS_H

#defineCARFUNCTIONS_H

#include"Car.h"

CarnewCar(istream&in);

booladdCar(CarCarToAdd, CarcarArray[], int&currentSize, constintMAXSIZE);

voidlistCars(ostream&out, CarcarArray[], int&currentSize);

voidsearchCarMake(stringmake, CarcarArray[], intcurrentSize);

voidsortCarMake(CarcarArray[], intcurrentSize);

voidswap(int&a, int&b);

voidswap(string&a, string&b);

#endif

 

·         Now create a file called CarFunctions.cppunder source files

#include<iostream

#include"Car.h"

#include"CarFunctions.h"

 

usingnamespacestd;

CarnewCar(istream&in)

{

     Car car1;

     cout<<"Make: ";

     in.ignore();

     getline(in, car1.make);

     cout<<"\nModel: ";

     getline(in, car1.model);

     cout<<"\nYear: ";

     in car1.year;

     cout<<endl;

     return car1;

}

booladdCar(CarcarToAdd, CarcarArray[], int&currentSize, constintMAXSIZE)

{

     currentSize++;

     if (currentSize<MAXSIZE)

     {

           carArray[currentSize - 1] =carToAdd;

           returntrue; //successful add

     }

     else

           returnfalse;

}

voidlistCars(ostream&out, CarcarArray[], int&currentSize)

{

     for (inti = 0; i<currentSize; i++)

     {

           out<<"Car "<<i + 1 <<endl;

           out<<carArray[i].make<<endl;

           out<<carArray[i].model<<endl;

           out<<carArray[i].year<<endl<<endl;

     }

}

 

 

voidsearchCarMake(stringmake, CarcarArray[], intcurrentSize)

{

     bool found = false;

     for (inti = 0; i<currentSize; i++)

     {

           if (carArray[i].make==make)

           {

                found = true;

                cout<<"Car at position "<<i<<endl;

                cout<<carArray[i].make<<endl;

                cout<<carArray[i].model<<endl;

                cout<<carArray[i].year<<endl<<endl;

           }//if

     }//for

     if(!found)

           cout<<"Record not found\n";

}

 

voidsortCarMake(CarcarArray[], intcurrentSize)

{

     //bubble sort

     intmaxElement;

     int index;

 

     for (maxElement = currentSize - 1; maxElement 0; maxElement--)

     {

           for (index = 0; index <maxElement; index++)

           {

                if (carArray[index].makecarArray[index + 1].make)

                {

                     //swap the entire record (make, model and year)

                     swap(carArray[index].make, carArray[index + 1].make);

                     swap(carArray[index].model, carArray[index + 1].model);

                     swap(carArray[index].year, carArray[index + 1].year);

                    

                }//if

           }//for

     }//for

}//swap

 

voidswap(int&a, int&b)

{

     int temp = a;

     a = b;

     b = temp;

}

 

voidswap(string&a, string&b)

{

     string temp = a;

     a=b;

     b= temp;

}

·         Finally, create a file called lab08a.cpp

#include<iostream

#include"Car.h"

#include"CarFunctions.h"

usingnamespacestd;

intmain()

{

     constint MAXSIZE = 100;

     intcurrentSize = 0;

     Car car1;

     CarcarArray[MAXSIZE];

     int menu;

     string make;

     bool done = false;

     while(!done)

     {

           cout<<"1. Add a new car to the array\n";

           cout<<"2. List out cars\n";

           cout<<"3. Search for a car by Make\n";

           cout<<"4. Sort cars by Make\n";

           cout<<"5. exit\n";

           cin menu;

           switch (menu)

           {

           case 1: car1 =newCar(cin);

                addCar(car1, carArray, currentSize, MAXSIZE);

                break;

           case 2: listCars(cout, carArray, currentSize);

                break;

           case 3: cout<<"What make do you want to search for? ";

                cin make;

                searchCarMake(make, carArray, currentSize);

                break;

           case 4:

                sortCarMake(carArray, currentSize);

                break;

           case 5: exit(0);

                break;

           default: cout<<"Number between 1 and 5\n";

           }//switch

     }//while

}//main

Ø  Run it adding two cars.  Take a screen shot of the successful output and place it below. Replace this output with your output (and 2 different cars)For a Windows 10 screen shot: Alt key + PrtSc key.  Then Ctrl + V to paste.  For Mac: Shift + Command + 4.  You will not credit unless you have a successful screen shot with Your name in the output.



·

More products