### Thread: Class assignment, a little overwhelmed/lost

Page 1 of 2 12 Last
1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
9
Rep Power
0

#### Class assignment, a little overwhelmed/lost

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.
2. No Profile Picture
bdb
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
156
Rep Power
34
pseudo code
2. loop
3. --- read data for 1 customer, if unsuccessful (no more data) exit loop
4. --- print customer row
5. --- update variables for footer
6. end loop (go to 2)
7. print footer

Notes:
for step 2 above (and 3) you can use an infinite while loop
Code:
```while (1)
{
/* ... */
if (example == test) break; /* exit loop */
/* ... */
}```
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
9
Rep Power
0
Ok here's what I've come up with. I'm still trying to figure out how to let the user input several different customer id's, along with their start/stop values, then calculate the average usage, along with the other needed stats. :confused:

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;
}```
4. No Profile Picture
bdb
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
156
Rep Power
34
Quick corrections:

char month[4]; /* space for a string with 3 letters */

double ave, highest, lowest, amount; /* always prefer double */

scanf("%s", month); /* do not use the address-of operator: month "decays" to the proper pointer to char */

if (id = 0) /* and remove the variable "one": the literal 0 is explicit enough */

/* review your if conditions with two sub-conditions: as you wrote them, they're always false */

ave = remain / 1.0; /* with just 1 you'd do integer division and get unexpected results (well, not when dividing by 1) */

~~~~~~~~

After these corrections your program will, almost certainly, let the user input several different customer id's.
Suggestion: for now worry just about inputting several customers. When you have that working, start thinking about the stats
5. No Profile Picture
I haz teh codez!
Devshed Frequenter (2500 - 2999 posts)

Join Date
Dec 2003
Posts
2,565
Rep Power
2338
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
9
Rep Power
0
Here's where I am now.

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 day;

char month[4];

double ave, highest, lowest, amount;

printf("Enter month: ");
scanf("%s", month);
printf("Enter day: ");
scanf("%d", &day);

while (1)
{

printf("enter id:");
scanf("%d", &id);

if(id == 0) 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;
}

printf("********************************\n");
printf("Electric Utility Bill %s %d\n", month, day);
printf("\nCustomer ID\tAmount Due");
printf("\n%d\t\t\$%2.2f", id, amount);
printf("\n\n");
printf("Average Usage: ");
printf("\nHighest Usage: ");
printf("\nLowest Usage : ");
printf("\n\nlow-rate users : ");
printf("\nmid-rate users : ");
printf("\nhigh-rate users: \n");
printf("********************************\n");

return 0;
}```
I removed this because it doesn't accomplish anything really.

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;```

At this point I'm getting correct "amount" for one customer entered. The Customer ID shows up as "0" now. I imagine it's because of the condition to terminate the loop, but I've not figured out a different way yet.

I've been suggested to create arrays in order to store data for multiple customers so that their data can be printed in that table. This is the 'table' I suppose:

Code:
```printf("********************************\n");
printf("Electric Utility Bill %s %d\n", month, day);
printf("\nCustomer ID\tAmount Due");
printf("\n%d\t\t\$%2.2f", id, amount);
printf("\n\n");
printf("Average Usage: ");
printf("\nHighest Usage: ");
printf("\nLowest Usage : ");
printf("\n\nlow-rate users : ");
printf("\nmid-rate users : ");
printf("\nhigh-rate users: \n");
printf("********************************\n");```
Currently it just prints whatever amount value was last calculated in the loop (because the variable 'amount' is updated to a different total each time it loops I assume).

So, I'm trying to learn arrays quickly, along with how to use them with functions and possibly structs... or... sheesh this is a lot of stuff to attempt to learn in little time. :eek:
7. I suppose I'd write the bills to a separate file rather than interspersing them with the input. Sticking to BDB's plan, we just tuck some of your output code into the loop and add some code to complete the summary, something like this:
Code:
```#include<stdio.h>
#include<limits.h>

#define LOW 0.10151
#define MID 0.1411
#define HIGH 0.1680
#define TIER1 300
#define TIER2 50

#define FOREVER for(;;)/* you who dislike FOREVER macro didn't write the second book on c I read.  Oh well. */

int main() {
int id, start, stop;
int remain;
int day;
double amount;

char month[4];

/* summary variables */
double money;
int  highest, lowest, low,mid,high,energy;

/* initial values of summary variables */
highest = INT_MIN;
lowest = INT_MAX;
low = mid = high = 0;
money = energy = 0;

printf("Enter month: ");
scanf("%s", month);
printf("Enter day: ");
scanf("%d", &day);

FOREVER {
fputs("enter id:",stdout);
scanf("%d", &id);
if(id == 0) break;
printf("enter start:");
scanf("%d", &start);
printf("enter stop:");
scanf("%d", &stop);
remain = stop - start;
if (remain <= TIER1) {
amount = remain * LOW;
++low;		      /* 5. --- update variables for footer */
} else if (remain <= TIER2) {
amount = remain * MID;
++mid;		      /* 5. --- update variables for footer */
} else {
amount = remain * HIGH;
++high;		      /* 5. --- update variables for footer */
}
printf("Electric Utility Bill %s %d\n", month, day); /* BDB's 4. --- print customer row
*/
printf("\nCustomer ID\tAmount Due"); /* BDB's 4. --- print customer row */
printf("\n%d\t\t\$%2.2f", id, amount); /* BDB's 4. --- print customer row */
energy += remain;	      /* 5. --- update variables for footer */
money += amount;	      /* 5. --- update variables for footer */
if (highest < remain)     /* 5. --- update variables for footer */
highest = remain;	      /* 5. --- update variables for footer */
/* lowest */              /* 5. --- update variables for footer */
}

printf("********************************\n");
printf("Electric Utility Bill %s %d\n", month, day);
printf("Average Usage: %d\n",energy/(low+mid+high));
printf("\nHighest Usage: %d",highest);
printf("\nLowest Usage : %d",lowest);
printf("\n\nlow-rate users : %d",low);
printf("\nmid-rate users : %d",mid);
printf("\nhigh-rate users: %d\n",high);
printf("********************************\n");
return 0;
}```
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
9
Rep Power
0
Thanks for the help again guys. I'm getting really close to where it needs to be. 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();
}```
Sorry if it's hard to follow :o

It's almost where it needs to be. The last step will be to allow for an indefinite amount of customers at a time. I imagine this will require a while loop, but I'm not quite sure how to get the same output as I have with the for loop.
9. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2004
Posts
20
Rep Power
0
Originally Posted by McFailboat
I imagine this will require a while loop, but I'm not quite sure how to get the same output as I have with the for loop.
Just convert the parameters of the FOR loop into a WHILE loop syntax.

If you have
Code:
```for (i = 10; i < 50; i++)
{
}```
what do you need for the while loop? And what is your loop exit strategy?
10. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
9
Rep Power
0
Hmm, I guess what I mean is the program must be able to accept any number of customer readings and still function properly. For example, if I enter 4 different customers, along with their readings for the month, the output (the table and all of the statistics) must be able to adapt to there being x number of customers/stats. At the moment, I have a fixed number (which is 3, my arrays are 4, but that's because I set my index to 1 instead of 0.) If I don't enter all 3, the program either dies, or outputs random numbers from memory.
11. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2004
Posts
20
Rep Power
0
I understood that. My question still stands.
12. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
9
Rep Power
0

I wrote this "test" program in order to simulate what I'm trying to do in my main program.

Code:
```#include <stdio.h>

int main()
{
int nums[10];
int total;

int i = -1;

while(nums[i++]>0)
{
printf("Enter a number: ");
scanf("%d", &nums[i]);
}

total = i - 1;

for (i=0; i<total; i++)
{
printf("Numbers entered: %d\n", nums[i]);
}

getch();
}```
13. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
9
Rep Power
0
This seems to do exactly what I need, I just have to implement it in my main program.
14. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
9
Rep Power
0
Ok, now I created a function out of it.

Code:
```#include <stdio.h>

int num_list(int nums[], int total)
{
int i = -1;

while(nums[i++]>0)
{
printf("Enter a number: ");
scanf("%d", &nums[i]);
}

total = i - 1;

for (i=0; i<total; i++)
{
printf("Numbers entered: %d\n", nums[i]);
}

return (nums[i]);
}

int main()
{
int nums[10];
int total;

printf("Enter up to 10 integers, enter 0 to quit.\n");

num_list(nums, total);

getch();
}```
However I'm not sure this is going to work when implemented in my main program. I think the 'for' loop needs to be in main()... not sure though. :chomp:
15. No Profile Picture
bdb
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
156
Rep Power
34

Code:
```#include <stdio.h>
int main(void) {
int count = 0;
while (1) {
int n;
printf("Enter an integer (0 to quit): ");
if (scanf("%d", &n) != 1) break;
if (n == 0) break;
count++;
}
printf("You entered %d numbers before the 0.\n", count);
return 0;
}```
Page 1 of 2 12 Last