# Need some direction

• 10-05-2012
McFail
Need some direction
Hello, I've been assigned to write a program for my C Programming class. Without detailing the situation, I'm basically having to learn everything on my own. The first program was fairly simple, and only contained concepts from the first six chapters of the book. The current assignment is more involved, and contains concepts from much later in the book. I'm a little overwhelmed at the moment, so any help would be greatly appreciated.

Assignment details:

Overview - Calculate and display a report for a monthly utility bill. The rates for usage are on an escalating scale depending on the units used.

I've been given three tiers of rates:
up to 300KWH = .10151 cents p/KWH
301-500KWH = .1411 cents p/KWH
Over 500KWH = .1680 cents p/KWH

Input:
Customer I.D. (Five digit code) ex. 12345
Starting KWH reading ex. 100 (only the value needs
Ending KWH reading ex. 750 input, not the unit)

Output: Structure of the output needs to follow these rules

Report Headings: Company Name(name doesn't matter), date reported (another input I assume, no date format given though), title of report(again, just needs some kind of title, like a heading)

Column Headings: customer id # and amount due
Detail Lines(I assume he means rows): customer id and amount due (these are the specific values for each variable)
Summary Lines: average usage:
highest usage:
lowest usage:
# of low rate users
# of mid rate users
# of high rate users
Total billing for all users for the month:

Those are my instructions. Basically what should happen is a user inputs a customer ID at prompt (the number is arbitrary, it just has to be 5 digits and show up on the end chart, or 'report'), then is prompted to input the 'start' KWH reading, followed by another prompt to input the 'stop' KWH reading.

The 'start' value is subtracted from the 'stop' value to get the KWH reading for the month, which is applied to the 'tier chart' listed above.

The trick is, a user must be able to enter as many different 'customers' as they want at a time (which includes a customer ID, start and stop inputs). Regardless of how many 'customers'
were entered, the program must then (when the user decides they're finished entering customers) calculate the amount due, and list the amount next to the matching customer ID number.
It must also calculate the average usage, highest usage, and lowest usage and print on the screen under the chart. Should also find the # of low/mid/high rate users (according to the 'rate tiers' above) and list the # of each. FINALLY, it should calculate the total billing from the sum of all users for the month.

That is my task, and I must do this with only 5 weeks of C programming (or any programming) knowledge under my belt. I will list the concepts that I have learned so far below:

-main(), printf(), and scanf() functions. (no clue on getchar(), and putchar())
-all of the basic data types (int, char, float, double, etc)
-I understand how to define constants
-I understand operators
-I can do all of the math correctly (within the program)
-I have a limited understanding of while loops
-I have a very limited understanding of creating functions, which I believe is necessary for this program
-Fair understanding of if, and else. Not sure about the combo of else if and if else though.

-Everything beyond that is knowledge that I have not acquired

Sorry for the novel, but I'm quite lost on this one. My main issue is figuring out how to gather the data (customer id, start, stop inputs), calculate it appropriately, then place the results into the format detailed above.

If I need to provide more details, just let me know. Again, any advice is greatly appreciated.
• 10-05-2012
std10093
McFail nobody is going to do the hw for you because of the policy of the forum :) Give it a shot,post your code and then there are maaaany people here willing to help (and able to help)
• 10-05-2012
McFail
Sorry, I'll post what I've done so far. It's incredibly inefficient, but so far the results are 'correct'. However, the way I'm getting there most likely won't fly, just because it isn't really dynamic. The way I've done it (so far the only way Ive been able to figure it out) is you have to enter 10 customer values and you have to enter 10 pairs of start/stop readings. The assignment says it must work regardless of the number of customers entered, and it must be dynamic (professor says he's just entering three customer ID's, plus start/stop readings for grading). So I'm trying to figure out how to make it dynamic. Yes, I understand some kind of 'loop' is necessary, but I just don't know how to apply that to this situation. Not sure how to find these variables yet as well:

highest usage:
lowest usage:
# of low rate users
# of mid rate users
# of high rate users

I'm trying really hard to gain the knowledge necessary in order to properly write this program. The time constraints make that difficult to do. I'm not asking for people to write code for me, but looking for advice on what C concepts are necessary to do this. Anyways, here it is...

Code:

```#include <stdio.h> #include <conio.h> #define LOW        .10151    #define MID        .1411 #define HIGH    .1680 #define TIER1    300 #define TIER2    500 int print_list(void); void strt(void); void get_endrd(void); int get_cust(void); int get_cust2(void); int get_cust3(void); int get_cust4(void); int get_cust5(void); int get_cust6(void); int get_cust7(void); int get_cust8(void); int get_cust9(void); int get_cust10(void); float get_amount(void); float get_amount2(void); float get_amount3(void); float get_amount4(void); float get_amount5(void); float get_amount6(void); float get_amount7(void); float get_amount8(void); float get_amount9(void); float get_amount10(void); float get_amounttotal(void); int get_date(void); void main(void) {     int cust, cust2, cust3, cust4, cust5, cust6, cust7, cust8, cust9, cust10;     float amount, amount2, amount3, amount4, amount5, amount6, amount7, amount8;     float amount9, amount10;      float amounttotal;     float amountave;                int date;     date = get_date();     cust = get_cust();     cust2 = get_cust2();     cust3 = get_cust3();     cust4 = get_cust4();     cust5 = get_cust5();     cust6 = get_cust6();     cust7 = get_cust7();     cust8 = get_cust8();     cust9 = get_cust9();     cust10 = get_cust10();         amount = get_amount();     amount2 = get_amount2();     amount3 = get_amount3();     amount4 = get_amount4();     amount5 = get_amount5();     amount6 = get_amount6();     amount7 = get_amount7();     amount8 = get_amount8();     amount9 = get_amount9();     amount10 = get_amount10();     amounttotal = amount+amount2+amount3+amount4+amount5+amount6+amount7+amount8+amount9+amount10;         amountave = amounttotal/10;         printf("\n**********************************************\n");     printf("Sparkie's Electric Co. Bill for %d\n\n", date);     printf("Customer ID\tAmount Due\n\n");     printf("%d\t\t\$%2.2f", cust, amount);     printf("\n%d\t\t\$%2.2f", cust2, amount2);     printf("\n%d\t\t\$%2.2f", cust3, amount3);     printf("\n%d\t\t\$%2.2f", cust4, amount4);     printf("\n%d\t\t\$%2.2f", cust5, amount5);     printf("\n%d\t\t\$%2.2f", cust6, amount6);     printf("\n%d\t\t\$%2.2f", cust7, amount7);     printf("\n%d\t\t\$%2.2f", cust8, amount8);     printf("\n%d\t\t\$%2.2f", cust9, amount9);     printf("\n%d\t\t\$%2.2f", cust10, amount10);     printf("\n**********************************************");     printf("\nAverage Usage: \$%2.2f", amountave);     printf("\nHighest Usage:");     printf("\nLowest Usage :");     printf("\n# of low-rate users:");     printf("\n# of mid-rate users:");     printf("\n# of hgh-rate users:");     printf("\nTotal Billing for month: \$%2.2f\n", amounttotal);     } int get_date(void) {     int date_to_return;         printf("Enter the month code: ");     scanf("%d", &date_to_return);         return (date_to_return); } int get_cust(void)  //cust1(cust) {     int cust_to_return;         printf("*****Sparkie's Electric Comapny Utility Bill Calculator*****\n\n");     printf("Please follow the step by step instructions in order to\n");     printf("determing the customer(s) bill for the month.\n\n");         printf("Please enter the Customer ID number:");     scanf("%d", &cust_to_return);     return (cust_to_return); } int get_cust2(void)    //cust2 {     int cust2_to_return;         printf("Please enter the second Customer ID number:");     scanf("%d", &cust2_to_return);         return (cust2_to_return); } int get_cust3(void)    //cust3 {     int cust3_to_return;         printf("Please enter the third Customer ID number:");     scanf("%d", &cust3_to_return);         return (cust3_to_return); } int get_cust4(void)    //cust4 {     int cust4_to_return;         printf("Please enter the fourth Customer ID number:");     scanf("%d", &cust4_to_return);         return (cust4_to_return); } int get_cust5(void)    //cust5 {     int cust5_to_return;         printf("Please enter the fifth Customer ID number:");     scanf("%d", &cust5_to_return);         return (cust5_to_return); } int get_cust6(void)    //cust6 {     int cust6_to_return;         printf("Please enter the sixth Customer ID number:");     scanf("%d", &cust6_to_return);         return (cust6_to_return); } int get_cust7(void)    //cust7 {     int cust7_to_return;         printf("Please enter the seventh Customer ID number:");     scanf("%d", &cust7_to_return);         return (cust7_to_return); } int get_cust8(void)    //cust8 {     int cust8_to_return;         printf("Please enter the eighth Customer ID number:");     scanf("%d", &cust8_to_return);         return (cust8_to_return); } int get_cust9(void)    //cust9 {     int cust9_to_return;         printf("Please enter the ninth Customer ID number:");     scanf("%d", &cust9_to_return);         return (cust9_to_return); } int get_cust10(void)    //cust10 {     int cust10_to_return;         printf("Please enter the tenth Customer ID number:");     scanf("%d", &cust10_to_return);         return (cust10_to_return); } float get_amount(void) {     int start, end;     int remain;     float amount_to_return;             printf("\n\nEnter the start reading value:");     scanf("%d", &start);     printf("\nEnter the end reading value:");     scanf("%d", &end);                 remain = end - start;         if (remain <= TIER1)         amount_to_return = remain * LOW;     if (remain > TIER1 && remain <= TIER2)         amount_to_return = remain * MID;     if (remain > TIER2)         amount_to_return = remain * HIGH;         return (amount_to_return); } float get_amount2(void) //amount2 {     int start, end;     int remain;     float amount2_to_return;             printf("\n\nEnter the start reading value:");     scanf("%d", &start);     printf("\nEnter the end reading value:");     scanf("%d", &end);                 remain = end - start;         if (remain <= TIER1)         amount2_to_return = remain * LOW;     if (remain > TIER1 && remain <= TIER2)         amount2_to_return = remain * MID;     if (remain > TIER2)         amount2_to_return = remain * HIGH;         return (amount2_to_return); } float get_amount3(void) //amount3 {     int start, end;     int remain;     float amount3_to_return;             printf("\n\nEnter the start reading value:");     scanf("%d", &start);     printf("\nEnter the end reading value:");     scanf("%d", &end);                 remain = end - start;         if (remain <= TIER1)         amount3_to_return = remain * LOW;     if (remain > TIER1 && remain <= TIER2)         amount3_to_return = remain * MID;     if (remain > TIER2)         amount3_to_return = remain * HIGH;         return (amount3_to_return); } float get_amount4(void) //amount4 {     int start, end;     int remain;     float amount4_to_return;             printf("\n\nEnter the start reading value:");     scanf("%d", &start);     printf("\nEnter the end reading value:");     scanf("%d", &end);                 remain = end - start;         if (remain <= TIER1)         amount4_to_return = remain * LOW;     if (remain > TIER1 && remain <= TIER2)         amount4_to_return = remain * MID;     if (remain > TIER2)         amount4_to_return = remain * HIGH;         return (amount4_to_return); } float get_amount5(void) //amount5 {     int start, end;     int remain;     float amount5_to_return;             printf("\n\nEnter the start reading value:");     scanf("%d", &start);     printf("\nEnter the end reading value:");     scanf("%d", &end);                 remain = end - start;         if (remain <= TIER1)         amount5_to_return = remain * LOW;     if (remain > TIER1 && remain <= TIER2)         amount5_to_return = remain * MID;     if (remain > TIER2)         amount5_to_return = remain * HIGH;         return (amount5_to_return); } float get_amount6(void) //amount6 {     int start, end;     int remain;     float amount6_to_return;             printf("\n\nEnter the start reading value:");     scanf("%d", &start);     printf("\nEnter the end reading value:");     scanf("%d", &end);                 remain = end - start;         if (remain <= TIER1)         amount6_to_return = remain * LOW;     if (remain > TIER1 && remain <= TIER2)         amount6_to_return = remain * MID;     if (remain > TIER2)         amount6_to_return = remain * HIGH;         return (amount6_to_return); } float get_amount7(void) //amount7 {     int start, end;     int remain;     float amount7_to_return;             printf("\n\nEnter the start reading value:");     scanf("%d", &start);     printf("\nEnter the end reading value:");     scanf("%d", &end);                 remain = end - start;         if (remain <= TIER1)         amount7_to_return = remain * LOW;     if (remain > TIER1 && remain <= TIER2)         amount7_to_return = remain * MID;     if (remain > TIER2)         amount7_to_return = remain * HIGH;         return (amount7_to_return); } float get_amount8(void) //amount8 {     int start, end;     int remain;     float amount8_to_return;             printf("\n\nEnter the start reading value:");     scanf("%d", &start);     printf("\nEnter the end reading value:");     scanf("%d", &end);                 remain = end - start;         if (remain <= TIER1)         amount8_to_return = remain * LOW;     if (remain > TIER1 && remain <= TIER2)         amount8_to_return = remain * MID;     if (remain > TIER2)         amount8_to_return = remain * HIGH;         return (amount8_to_return); } float get_amount9(void) //amount9 {     int start, end;     int remain;     float amount9_to_return;             printf("\n\nEnter the start reading value:");     scanf("%d", &start);     printf("\nEnter the end reading value:");     scanf("%d", &end);                 remain = end - start;         if (remain <= TIER1)         amount9_to_return = remain * LOW;     if (remain > TIER1 && remain <= TIER2)         amount9_to_return = remain * MID;     if (remain > TIER2)         amount9_to_return = remain * HIGH;         return (amount9_to_return); } float get_amount10(void) //amount10 {     int start, end;     int remain;     float amount10_to_return;             printf("\n\nEnter the start reading value:");     scanf("%d", &start);     printf("\nEnter the end reading value:");     scanf("%d", &end);                 remain = end - start;         if (remain <= TIER1)         amount10_to_return = remain * LOW;     if (remain > TIER1 && remain <= TIER2)         amount10_to_return = remain * MID;     if (remain > TIER2)         amount10_to_return = remain * HIGH;         return (amount10_to_return); } float get_amounttotal(void) //sum of amounts {     float amount, amount2;     float amounttotal_to_return;         amounttotal_to_return = amount+amount2;         return (amounttotal_to_return); }```
• 10-05-2012
Salem
> int cust, cust2, cust3, cust4, cust5, cust6, cust7, cust8, cust9, cust10;

As soon as you start putting numeric suffixes on your variables, you need arrays.

Eg.
Code:

```int customers[10]; for ( i = 0 ; i < 10 ; i++ ) {   printf("Enter customer number %d\n", i+1 );   scanf("%d", &customers[i] ); }```
Do you see how you can use this to have only one get_cust() function and one get_amount() function.
• 10-06-2012
Welcome to the forum, McFail! :cool:

When you have re-written your program using arrays, you'll have shortened the code a great deal.

Anytime you see yourself repeating almost the exact same code over and over, in a program STOP! Because it's an error in the design of the program.
• 10-06-2012
McFail
Thanks for the responses. I feel like I have to use a while loop in order to allow for an indefinite number of 'customers', instead of a set number. This is my next attempt:

Code:

```#include <stdio.h>#include <conio.h> #define LOW                .10151        #define MID                .1411 #define HIGH        .1680 #define TIER1        300 #define TIER2        50 int main(void) {         int id, start, stop;         int remain;         int one;         int high, low, mid, day;                 char month[3];           float ave, highest, lowest, amount;                 one = 0;                 while (1)         {                 printf("Enter month: ");                 scanf("%s", &month);                 printf("Enter day: ");                 scanf("%d", &day);                                 printf("enter id:");                 scanf("%d", &id);                                 if(id == one) break;                                 printf("enter start:");                 scanf("%d", &start);                 printf("enter stop:");                 scanf("%d", &stop);                                 remain = stop - start;                                 if (remain <= TIER1)                 amount = remain * LOW;                   if (remain > TIER1 && remain <= TIER2)                 amount = remain * MID;                 if (remain > TIER2)                 amount = remain * HIGH;                                 ave = remain/1;                                 if (remain <= TIER1)                 low = 1;                 else                 low = 0;                 if (remain > TIER1 && remain <= TIER2)                 mid = 1;                 else                 mid = 0;                 if (remain > TIER2)                 high = 1;                 else                 high = 0;                                 printf("********************************\n");                 printf("Electric Utility Bill %s %d\n", month, day);                 printf("\nCustomer ID\tAmount Due");                 printf("\n%d          \$%2.2f", id, amount);                 printf("\n\n");                 printf("Average Usage: %2.2f", ave);                 printf("\nHighest Usage: ");                 printf("\nLowest Usage : ");                 printf("\n\nlow-rate users : %d", low);                 printf("\nmid-rate users : %d", mid);                 printf("\nhigh-rate users: %d\n", high);                 printf("********************************\n");         }                 printf("Goodbye\n"); return 0; }```
I rewrote part of my first attempt with the for loop and attempted to use arrays, but it got messy, so I had to start over. I think this try is closer to the idea, but it still doesn't allow for several unique 'customers' to be inputted (regardless of the number).
• 10-07-2012
Three suggestions:

1) It appears your teacher wants you to use the idea of "parallel arrays". This is the idea where the data at array1[N] relates to the data at array2[N], for every N. It could be names in array1, and numbers in array2. (most common). It could be first names in array1, and last names in array2, or any other types of data.

You would normally use a struct for this, but you haven't been taught this yet, and parallel arrays are taught before structs, so I'm sure this is what's wanted.

Using arrays isn't "messy". It's actually far "cleaner" than working with a boatload of variables.

2) dependent lines of code, should be indented 2-5 spaces to the right, from the lines of code they depend upon:
Code:

```        ave = remain/1;  //??????                 if (remain <= TIER1)           low = 1;         else           low = 0;         if (remain > TIER1 && remain <= TIER2)           mid = 1;         else           mid = 0;         if (remain > TIER2)           high = 1;         else           high = 0;```
As you continue programming, your brain begins to automatically recognize common patterns of C syntax, which is a BIG plus in debugging your errors. The sooner you start using this indentation style, the sooner your brain will start learning it - and all of us will cheer, because your code becomes instantly easier for us to debug, as well.

What is the first line of code in the above section, for?

3) Make your two arrays in main, and then pass them to a new input function, by name:
Code:

`input(arrayName, arrayName);`
Now, the input function definition would be:
Code:

```void input(char arrayName[SIZE], float arrayName[SIZE]) { //**********     //your input function local variables and code in here }```
The line of code with the **** after it, also goes above main(), as the function's prototype. Replace the
Code:

`{`
on the end, with a semi-colon ; , at that location.

Don't put off using functions. They have a learning curve, but give an immediate benefit of encapsulating your logic into disparate groups, which is a big help in debugging and in writing your code.
• 10-07-2012
McFail
Ok, here's where I'm at.

Code:

```#include <stdio.h> #include<limits.h> #define LOW 0.10151 #define MID 0.1411 #define HIGH 0.1680 #define TIER1 300 #define TIER2 500 #define MAX 100 double calculate_amount(double amount[], int remain[]) {     int i = 1;         amount[i] = 0;             for (i = 1; i < 4; i++)     {         if (remain[i] <= TIER1){             amount[i] = remain[i] * LOW;         }else if (remain[i] <= TIER2){             amount[i] = remain[i] * MID;         }else {             amount[i] = remain[i] * HIGH;         }     }         return amount[i]; } int cust_list(int custid[], int start[], int stop[], int remain[]) {     int i = 1;                     for (i = 1; i < 4; i++)     {         printf("Enter Customer ID %d: ", i);            scanf("%d", &custid[i]);         if (custid[i] == 0) break;         printf("Enter start read for customer %d: ", i);         scanf("%d", &start[i]);         printf("Enter stop read for customer %d: ", i);         scanf("%d", &stop[i]);     }         for (i = 1; i < 4; i++)     {            remain[i] = stop[i] - start[i];     }         return custid[i], remain[i]; } int main () {     int custid[4];     int start[4];     int stop[4];     int remain[4];         int i;         int  highest, lowest, low, mid, high, energy;         char str[MAX];         double money;       double total;     double amount[4];         highest = INT_MIN;       lowest = INT_MAX;       low = mid = high = 0;       money = energy = 0;         for(i=0;i<MAX;i++)           str[i]='\0';         printf("Enter a date [mm/dd/yyyy]: ");       scanf("%[^\n]s",str);           cust_list(custid, start, stop, remain);         printf("***************************************************************\n");     printf("Sparkie's Electric Utility Co. Billing report for %s\n", str);     printf("Customer ID\tAmount Due\n");         calculate_amount(amount, remain);         for (i = 1; i < 4; i++)     {         printf("%d. %d\t\$%.2f\n", i, custid[i], amount[i]);     }         for (i = 1; i < 4; i++)     {         if (remain[i] <= TIER1)         {             ++low;                                          }         else if (remain[i] <= TIER2)         {             ++mid;                                                }           else           {               ++high;         }     }         for (i = 1; i < 4; i++)     {         if (highest < remain[i])                  highest = remain[i];            if (lowest > remain[i])             lowest = remain[i];     }         for (i = 1; i < 4; i++)     {         energy += remain[i];     }         for (i = 1; i < 4; i++)     {         money += amount[i];     }         printf("low: %d\n", low);     printf("mid: %d\n", mid);     printf("high: %d\n", high);     printf("average usage: %d\n", energy/(low+mid+high));     printf("highest usage: %d\n", highest);     printf("lowest usage: %d\n", lowest);     printf("Total billing for month:\$%.2f\n\n", money);     printf("***************************************************************\n");         getch();    }```
Last step will be to make it more dynamic. Not quite sure how to use the while loop to get the same output.