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

Problem 90 CSDP 221 Spring 2017 The Acme Electric Company problem due 17 April 2

ID: 3819491 • Letter: P

Question

Problem 90 CSDP 221 Spring 2017 The Acme Electric Company problem due 17 April 2017 at the beginning of class. The Acme Electric Company charges, according to the lollowing rate schcdule: 15 cents per kilowatt-hour (kwl) for clectricity up to and including the first 300 kwh; 10 cents per kwh lor the next 300 kwh (up to and including 600 kwl); 7.5 cents per kwh lor the next loo kwh (up to and including 1000 kwl); 5 cents per kwh for all electricity over 1000 kwh Write a program to compute the total charge for each customer using the data contained in an input file named electric dat The format of the input file is two columns of numbers. The first number is the customer number and the second is the number of kilo-watt hours used. The output should consist of a three-column table listing the customer number, the number of kilowatt-hours used, and the charge for cach customer. It should also compute and print the number of customers, total hours used and the total charges. All charges should be computed to the nearest penny and displayed in the usual dollars & cents format (XXX.XX). Each column should have a heading to identify the information beneath it. The values not in the table (number of customers, total hours, total charges) should have a descriptive sentence to explain the number being printed. This can be output to a lile named acme dat.

Explanation / Answer

Part 1

In C:

#include<stdio.h>

#include<math.h>

double calc(int units) {// function to calculate price of electricity

   double price = 0;

   if (units > 1000)

      price = 15*300+ 10*300 +7.5*400+5*(units - 1000);

   else if (units>600)

      price = 15*300+ 10*300 +7.5*(units - 600);

   else if (units>300)

      price = 15*300+ 10*(units - 300);

   else

      price = 15*units;

   return round(price)/100.0;

}


void main() {

   int unit, totalUnit = 0, count = 0;

   double amount, totalAmount = 0;

   char custid[10];

   FILE *inFile = fopen("electric.dat", "r");

   FILE *outFile = fopen("acme.dat", "w");

   fprintf(outFile, "Customer No. Units Cost ");//header

   

   while (fscanf(inFile, "%s %d", custid, &unit)>0) {

      count ++;

      totalUnit += unit;

      amount = calc(unit);

      totalAmount += amount;

      fprintf(outFile, "%s %d %.2lf ", custid, unit, amount);//print each row

   }

   fprintf(outFile, "Total user: %d Total Units: %dKWh Total amount = $%.2lf ", count, totalUnit, totalAmount);//write stats about the file

   fclose(inFile);

   fclose(outFile);

}

For part 2

In C++

#include<iostream>
#include<fstream>
using namespace std;

bool isPrime(int n) {
   for (int i = 2; i < n; i++) //if divible by any number between 2 and n-1 (both inclusive)
       if (n%i == 0)
           return false; //the number is not prime
   return true; //else it is divisible
}

int main() {
   int count = 0;
   ofstream output("prime.dat"); //open file
   if (output.is_open()) { //chk if the file is successfully opened
       for (int i = 2; count < 100; i++) //increase i till count reaches 100
           if (isPrime(i)) {
               output<<i<<" is Prime"<<endl; //peint prime numbers to the file
               count ++; //increase prime count
           }
       output.close();
   }
}

The codes given above are in C language and in C++ language respectively to show the difference between handling file in C and C++.

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