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

#include<iostream> #include <fstream> #include <iomanip> #include <cstring> #inc

ID: 3610257 • Letter: #

Question

#include<iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <string>

using namespace std;
void sort(string[],double[],int);
void swap(string[],double[],int,int);
int valid(string);
int search(string,string[],int);
void display(string[],int);
int main(){
string license[400],plate;
double fine[400];
int count=0,i,loc;
ifstream in;
ofstream out;
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);
out.setf(ios::fixed,ios::floatfield);
out.precision(2);
    in.open("fines.txt");          //open file
  if(in.fail())            //is it ok?
       { cout<<"input file didnot open please check it ";
        system("pause");
        return 1;
        }
  out.open("sortfine.txt");          //open file, no more than 400 file stored.
  if(out.fail())            //is it ok?
       { cout<<"output file didnot open please check it ";
        system("pause");
        return 1;
        }
in >> license[count]>>fine[count];
  
  while(in)       
      {count++;
      in >>license[count]>>fine[count];
      }
sort(license,fine,count);
for(i=0;i<count;i++)
  out<<license[i]<<setw(8)<<fine[i]<<endl;
display(license,count);
cout<<" Enter license number interested in E to exit: ";
cin>>plate;
while(plate!="E"&&plate!="e")
   {
    switch(valid(plate))
       {case 1:cout<<" License plate number "<< plate<<" is toolong. Try again. ";
               break;
        case2:cout<<" License plate number "<<plate<<" isformatted incorrectly. "; // 3 letters followed by 3 numbers
              cout<<"Must be 3 upper case letters followed by 3 digits. Tryagain. ";
              break;
       default:loc=search(plate,license,count);
              if(loc<0)
                 cout<<" License plate number "<<plate<<" not indatabase. "; // not in the input files.
               else
                  cout<<" License plate number "<<plate<<" wasfined $"<<fine[loc]<<endl;
         }
    display(license,count);
     cout<<" Enter license numberinterested in E to exit: ";
     cin>>plate;
}
cout<<endl;
system("pause");
}   
void sort(string plate[], double fine[], int n)
{int i,j;
for (i=0;i<n-2;i++)
    for (j=i+1;j<n-1;j++)
        if (plate[i]<plate[j])
           swap (plate,fine,i,j);
return;
}
void swap(string plate[],double fine[],int i,int j)
{string stemp;
double dtemp;
stemp=plate[i];
plate[i]=plate[j];
plate[j]=stemp;
dtemp=fine[i];
fine[i]=fine[j];
fine[j]=dtemp;
return;
}
int search(string plate, string license[],int count)
{ int min=0,max=count-1,mid;
     while(min<=max)
        { mid=(min+max)/2;
          if(license[mid]==plate)
             return mid;
          else
             if(license[mid]<plate)
                max=mid-1;
          else
              if(license[mid]>plate)
                 min=mid+1;
          }
        return -1;
}
void display(string license[],int count)
{int line=0,i;
cout<<" License Plate Numbers on File are: ";
for(i=0;i<count;i++)
   {cout<<license[i]<<" ";
    line++;
    if(line==10)
       {line=0;
        cout<<endl;
        }
   }
   cout<<endl;
}
int valid(string plate)
{int i;
if(plate.length()>6)
    return 1;
for(i=0;i<3;i++)
   if(!isalpha(plate[i])||islower(plate[i]))
      return 2;
for(i=4;i<6;i++)
   if(!isdigit(plate[i]))
      return 2;
return 0;
}

Explanation / Answer

please rate - thanks for some reason the diagram is not inserting so I will describewhat should be there and you'll let me know if its ok variable name in red, other part ofdescription in black, function names in blue to sort-arrays license & fine, number ofitems in arrays (count) from sort - arrays license & fine sorted to swap-arrays license & fine andelements i & j to swap from swap-elements i & j of license & fine switchedwith each other to valid-plate number looking for from valid- codegiving validity of plate number 1=too long,2=incorrect format, 0=good do can search for it to display-array license and how many elements (count) in it to search-look for platenumber in the count elements oflicense from search-loc (location) of plate in array license or codeit's not in it