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

Modify the below C++ Program (//Electricity Cost Program) to the following that

ID: 3622420 • Letter: M

Question

Modify the below C++ Program (//Electricity Cost Program) to the following that includes a - f (below):

a. all data is to be read from a file.
b. the data file will be created using the Notepad editor.
c. the customer number does not need to be validated to verify that it is exactly 4 digits and the Kilowatt-hours does not need to be validated.
d. Each customer has a name.
e. Sorted into ascending order by customer number and print the results.
f. Sort into alphabetical order and print the results.
f. Use the data shown below.

Customer Number kilowatt-hours used Kilowatt-hours used for name
1231 300 Washington, George
3431 600 Adams, John
5767 950 Jefferson, Tom
1515 250 Madison, John
4532 1300 Wilson, Woodrow
2541 1450 Hoover, Herbert
4325 47 Roosevelt, Teddy
5151 1500 Roosevelt, Franklin
4624 2500 Truman, Harry
3333 800 Eisenhower, Ike
3451 1000 Kennedy, John
7856 1451 Johnson, Lyndon
1111 5242 Nixon, Richard
2121 379 Carter, Jimmy
3541 4545 Reagan, Ronald

PROGRAM THAT NEEDS TO BE MODIFIED:
//Electricity Cost Program
#include < iostream >
using namespace std;
//function prototype
double computeCharge(double kilowatts);


int main()
{
double kilowatts[10], charge[10];
int id[10];
int num;
int i=0;
cout<<"Enter number of customer:";
cin>>num;
do
{
cout<<"Enter Customer id:";
cin>>id[i];
if(id[i]<1000)
{
cout<<"Invalid Enter Customer id:";
cin>>id[i];
}

// data input
cout << "Enter the kilowatt-hours used: ";
cin >> kilowatts[i];
//function call to compute
charge[i]=computeCharge(kilowatts[i]);
// display the result
cout.setf(ios::fixed | ios::showpoint);
cout.precision(2);
cout << "The charge for " << kilowatts[i]
<< " kilowatt-hours is $"
<< charge[i]<< endl;
i++;

}while(i<num);
system("pause");
return 0;
}
double computeCharge(double kilowatts)
{
double charge;
// calculate charge for kilowatts-hours
if(kilowatts > 1500)
charge = 0.12*400 + 0.1*600 + 0.08*500+ 0.06*(kilowatts - 1500);
else if (kilowatts > 1000)
charge = 0.12*400 + 0.1*600 + 0.08*(kilowatts - 1000);
else if (kilowatts > 400)
charge = 0.12*400 + 0.1*(kilowatts - 400);
else
charge = 0.12*kilowatts;
return charge+20;
}

void sortID(int id[],double kilo[],double charge[],int n)
{

double temp1,temp2;
int i,j,min,minat;

for(i=0;i<n-1;i++)
{
minat=i;
min=array[i];
for(j=i+1;j<n;j++) //select the min of the rest of array
{
if(min>id[j])
//ascending order for descending reverse
{
minat=j;
//the position of the min element
min=id[j];
}
}
int temp=id[i];
temp1=kilo[i];
temp2=charge[i];
kilo[i]=kilo[minat];
kilo[minat]=temp2;
charge[i]=charge[minat];
charge[minat]=temp2;
id[i]=id[minat]; //swap
id[minat]=temp;
}
}

Explanation / Answer

//Electricity Cost Program
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

#define MAX 10

struct customer
{
       int id;
       double kilowatt;
       double charge;
       char name[30];
};
struct customer List[MAX];

double computeCharge(double kilowatts)
{
    double charge;
    // calculate charge for kilowatts-hours
    if(kilowatts > 1500)
        charge = 0.12*400 + 0.1*600 + 0.08*500+ 0.06*(kilowatts - 1500);
    else if (kilowatts > 1000)
        charge = 0.12*400 + 0.1*600 + 0.08*(kilowatts - 1000);
    else if (kilowatts > 400)
        charge = 0.12*400 + 0.1*(kilowatts - 400);
    else
        charge = 0.12*kilowatts;
    return charge+20;
}

int readData()
{
    char filename[255];
    cout<<"Enter Filename: ";
    cin>>filename;
    ifstream fin(filename);
    for(int i=0;i<MAX;i++)
    {
        fin>> List[i].id;
        fin>> List[i].kilowatt;
        fin.getline(List[i].name,30);  
        List[i].charge = computeCharge(List[i].kilowatt);
    }
    fin.close ();
    return 0;
}
void display()
{
     for(int i=0;i<MAX;i++)
             cout<<List[i].id<<" "<<List[i].kilowatt<<" "<<List[i].name<<endl;
}
int sortID()
{
     struct customer tmp;
     int i,j;
     for(i=0;i<MAX-1;i++)
         for(j=i+1;j<MAX;j++)
             if(List[i].id>List[j].id)
             {
                 tmp=List[i];
                 List[i]=List[j];
                 List[j]=tmp;                    
             }
     return 0;
}
int sortName() //bubble sort
{
     struct customer tmp;
     int i,j;
     for(i=0;i<MAX-1;i++)
         for(j=i+1;j<MAX;j++)
             if(strcmp(List[i].name,List[j].name)>0)
             {
                 tmp=List[i];
                 List[i]=List[j];
                 List[j]=tmp;                    
             }
     return 0;
}
int main()
{
   
    readData();
    display();
    cout<<" Sort by ID ";
    sortID();
    display();
    cout<<" Sort by name ";
    sortName();
    display();
    system("pause");
    return 0;
}

Full source code : http://www.mediafire.com/?pbbhbxytg8z7m9d

I use Dev-C++ to compile the source code

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