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

This assignment is graded based on correctness and will require you to use highe

ID: 3718484 • Letter: T

Question

This assignment is graded based on correctness and will require you to use higher-order functions to sort automobiles. The description is below. You must make use of higher-order functions to sort the cars. You should not, for example, create entirely separate functions each with dedicated loops to sort the cars. You will need a loop (or potentially more than one loop depending on your sorting algorithm of choice) in the sortArr function but that is pretty much it. Use prototype whenever needed.

Explanation / Answer

MAIN PROGRAM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Automobile
{
class Program
{
static void Main(string[] args)
{

Automobile[] automobiles = { new Automobile(1995, "Honda", "Accord", "Sedan"),
new Automobile(1990,"Ford", "F-150", "Pickup"),
new Automobile(2000, "GMC", "Tahoe", "SUV"),
new Automobile(2010, "Toyota", "Tacoma", "Pickup"),
new Automobile(2005, "Lotus", "Elise", "Roadster"),
new Automobile(2008, "Subaru", "Outback", "Wagon")};
Program ob = new Program();
Console.WriteLine("*****");
Console.WriteLine(" Cars sorted by year are:");
ob.sortArr("year", automobiles);
ob.printArr(automobiles);
Console.WriteLine(" Cars sorted by make are:");
ob.sortArr("make", automobiles);
ob.printArr(automobiles);
Console.WriteLine(" Cars sorted by type are:");
ob.sortArr("type", automobiles);
for (int i = 0; i < automobiles.Count(); i++)
{
Console.WriteLine(automobiles[i].year.ToString() + " " + automobiles[i].make + " " + automobiles[i].model + " " + automobiles[i].type );
}
Console.WriteLine("*****");

Console.ReadLine();
}

void printArr(Automobile [] auto) {
for (int i = 0; i < auto.Count(); i++)
{
Console.WriteLine(auto [i].year.ToString()+" " + auto[i].make+ " " + auto [i].model);
}
}
void sortArr( string comparater , Automobile[] array )
{
Automobile temp ;
if (comparater=="year")
{
for (int j = 0; j < array.Count(); j++)
{
for (int i = 0; i < array.Count()-1; i++)
{
if (yearComparater(array[i].year,array[i+1].year )==false)
{
temp = array[i + 1];
array[i + 1] = array[i];
array[i] = temp;
}
}
}
}
else if (comparater=="make")
{
for (int j = 0; j < array.Count(); j++)
{
for (int i = 0; i < array.Count() - 1; i++)
{
if (makeComparater (array[i].make, array[i + 1].make) == false)
{
temp = array[i + 1];
array[i + 1] = array[i];
array[i] = temp;
}
}
}
}
else if (comparater=="type")
{
for (int j = 0; j < array.Count(); j++)
{
for (int i = 0; i < array.Count() - 1; i++)
{
if (typeComparater (array[i].type, array[i + 1].type) == false)
{
temp = array[i + 1];
array[i + 1] = array[i];
array[i] = temp;
}
}
}
}

}

bool yearComparater(int auto1, int auto2)
{
if (auto1>auto2 )
{
return true;
}
else
{
return false;
}
}

bool makeComparater(string auto1, string auto2)
{
if (string.Compare(auto1.ToLower(),auto2.ToLower())==-1)
{
return true;
}
else
{
return false;
}
}

bool typeComparater(string auto1, string auto2)
{

if (getTypeCode(auto1) <getTypeCode( auto2))
{
return true;
}
else
{
return false;
}
}

private int getTypeCode(string auto1)
{

switch (auto1.ToLower())
{
case "roadster": return 1;
case "pickup": return 2;
case "suv": return 3;
case "wagon": return 4;

default: return 5;
break;
}
}

}
}

AUTOMOBILE CLASS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Automobile
{
class Automobile
{
public int year;
public string make;
public string model;
public string type;
public Automobile(int year, string make, string model, string type)
{
this.year = year; //integer (ex. 2001, 1995)
this.make = make; //string (ex. Honda, Ford)
this.model = model; //string (ex. Accord, Focus)
this.type = type; //string (ex. Pickup, SUV)
}
}
}