help! :(

H

honeygrl33

i accidentially erased something someone helped me write and i am soooo
in trouble if i dont fix this, i really need the help!!!!!

heres what i have:
/*
*/

#include <stdio.h>
#include <stdlib.h>

#define SIZE 12

void printResults (char months[][15], double sales[]);
void storeSales (char months[][15], double sales[]);

int main ( void )
{
system ("clear");
int i;
double sales[ SIZE ];
char months [SIZE][15] = {"January", "February", "March", "April",
"May",
"June", "July", "August", "September", "October",
"November", "December"};

for (i = 0; i < SIZE; i++)
{
printf("%-9s: %9.2f/n", months[ i ], sales[ i ]);
}
storeSales (months, sales);
printResults (months, sales);
return 0;

}
void storeSales ( char months[][15], double sales[] )
{
int i = 0;
for ( i = 0; i < SIZE; i++ )
{

do
{
printf("Enter the store's sales for the month of %s:",
months[ i ]);
scanf( "9.2%lf", &sales [ i ]);
printf("\n");
}
while ((sales[ i ] < 0.00 ) || ( sales[ i ] > 100000.00));
}
}
 
K

karl malbrain

honeygrl33 said:
heres what i have:

#define SIZE 12

void printResults (char months[][15], double sales[]);
void storeSales (char months[][15], double sales[]);

You need to traverse your sales array and find the average, which is the
total sum divided by the number of elements. You'll want a "for" loop for
this.

While you're traversing the array, you'll also want to capture the lowest
and highest values. You'll need 2 "if" statements for this.

Are you on-track so far?

karl m
 
T

Tatu Portin

honeygrl33 said:
i accidentially erased something someone helped me write and i am soooo
in trouble if i dont fix this, i really need the help!!!!!

heres what i have:
/*
*/

/* Here is my implementation: */
#include <stdio.h>
#include <stdlib.h>

#define SIZE 12

void printResults (char months[][15], double sales[]);
void storeSales (char months[][15], double sales[]);

/* Problems with 'storeSales', you should fix them.*/

int main ( void )
{
system ("clear");
int i;
double sales[ SIZE ];
char months [SIZE][15] = {"January", "February", "March", "April",
"May", "June", "July", "August", "September", "October",
"November", "December"};

for (i = 0; i < SIZE; i++)
{
printf("%-9s: %9.2f\n", months[ i ], sales[ i ]);
}
storeSales (months, sales);
printResults (months, sales);

return 0;
}

void storeSales ( char months[][15], double sales[] )
{
int i = 0;
for ( i = 0; i < SIZE; i++ )
{

do
{
printf("Enter the store's sales for the month of %s:",
months[ i ]);
scanf( "9.2%lf", &sales [ i ]);
printf("\n");
}
while ((sales[ i ] < 0.00 ) || ( sales[ i ] > 100000.00));
}
}

void printResults (char months[][15], double sales[])
{
int i;
double lowest = 0.0, highest = 0.0, average = 0.0;

for (i = 0 ; i < SIZE ; i++)
average = average + (sales / SIZE);

printf ("Average: %lf\n", average);


lowest = sales[0];
for (i = 0 ; i < SIZE ; i++) {
if (lowest > sales) {
lowest = sales;
}
}

printf ("Lowest: %lf\n", lowest);

highest = sales[0];
for (i = 0 ; i < SIZE ; i++) {
if (highest < sales) {
highest = sales;
}
}

printf ("Highest: %lf\n", highest);
}
/* The End */
 
K

karl malbrain

Tatu Portin said:
honeygrl33 said:
i accidentially erased something someone helped me write and i am soooo
in trouble if i dont fix this, i really need the help!!!!!

heres what i have:
/*
*/

/* Here is my implementation: */
#include <stdio.h>
#include <stdlib.h>

#define SIZE 12

void printResults (char months[][15], double sales[]);
void storeSales (char months[][15], double sales[]);

/* Problems with 'storeSales', you should fix them.*/

int main ( void )
{
system ("clear");
int i;
double sales[ SIZE ];
char months [SIZE][15] = {"January", "February", "March", "April",
"May", "June", "July", "August", "September", "October",
"November", "December"};

for (i = 0; i < SIZE; i++)
{
printf("%-9s: %9.2f\n", months[ i ], sales[ i ]);
}
storeSales (months, sales);
printResults (months, sales);

return 0;
}

Why are you printing the sales array without INSTANTIATING any values in it?
What do you expect beyond GIGO? karl m
 
H

honeygrl33

I really wish I could tell you... also, I really wish I knew what that
meant.. my friend said it meant I didn't put anything in the values?
 
K

karl malbrain

honeygrl33 said:
I really wish I could tell you... also, I really wish I knew what that
meant.. my friend said it meant I didn't put anything in the values?

So it's premature to output values from sales at that point in the program's
execution. Do you think the "print loop" is needed at all, and if so where
does it belong. karl m
 
K

karl malbrain

Ben Pfaff said:
Cut it out with the multiple posts. Usenet is not chat.

I think that usenet is suffering a feed-back loop. There are many posts
coming up dups in my newsreader right now. karl m
 
T

Tatu Portin

honeygrl33 said:
I really wish I could tell you... also, I really wish I knew what that
meant.. my friend said it meant I didn't put anything in the values?

If you do not initialize variables, they can have any value.
For example

int apples;

printf ("%d", apples);

would print out "-135468" or any other number.

but

int apples = 5;

printf ("%d", apples);

would print out "5".

Same holds for arrays. You can initialize arrays with zeros with code:

#define DAYS 30

int i;

int tomatoes_per_day[30];

for (i = 0 ; i < DAYS ; i++)
tomatoes_per_day = 0;


Now every value in array 'tomatoes_per_day' has value which is zero.
 
T

Tatu Portin

Here is the whole code working.
If you are using gcc to compile your programs, you may use the line 'gcc -Wall
-pedantic a.c' where 'a.c' is the source file. This prints a lot warnings if
something is wrong.


/* Start Of File */

#include <stdio.h>
#include <stdlib.h>

#define SIZE 12

void printResults (char months[][15], double sales[]);
void storeSales (char months[][15], double sales[]);

int main ( void )
{
double sales[ SIZE ];
char months [SIZE][15] = {"January", "February", "March", "April",
"May", "June", "July", "August", "September", "October",
"November", "December"};

system ("clear");
storeSales (months, sales);
printResults (months, sales);

return 0;
}

void storeSales ( char months[][15], double sales[] )
{
int i = 0;
for ( i = 0; i < SIZE; i++ )
{

do
{
printf("Enter the store's sales for the month of %s:",
months[ i ]);
scanf( "%lf", &sales); /* scanf: "%lf" for double*/

/* User sees if he or she has entered the correct * value. */
printf("%f\n", sales); /* printf: "%f" for double*/

}
while ((sales[ i ] < 0.00 ) || ( sales[ i ] > 100000.00));
}
}

void printResults (char months[][15], double sales[])
{
int i;
double lowest = 0.0, highest = 0.0, average = 0.0;


for (i = 0; i < SIZE; i++)
{
printf("%-9s: %9.2f\n", months[ i ], sales[ i ]);
}


for (i = 0 ; i < SIZE ; i++)
average = average + (sales / SIZE);

printf ("Average: %f\n", average);


lowest = sales[0];
for (i = 0 ; i < SIZE ; i++) {
if (lowest > sales) {
lowest = sales;
}
}

printf ("Lowest: %f\n", lowest);

highest = sales[0];
for (i = 0 ; i < SIZE ; i++) {
if (highest < sales) {
highest = sales;
}
}

printf ("Highest: %f\n", highest);
}

/* End Of File */
 
T

Tatu Portin

honeygrl33 said:
That still has the endless loop, just tried it..... why wont tyhe loop
end?

while ((sales[ i ] < 0.00 ) || ( sales[ i ] > 100000.00));

You have to enter a number that is between 0.00 and 100000.00.
If it still does not work, I usually play around with the code, e.g. try
removing the
do
{

/* ... */

}
while (/* ... */)
loop.
Also you may try copy & paste my code and compile it. You may have a typing
error if you have copied the examples presented by hand.
 
H

honeygrl33

i created a new file w/ur code -- still looped over and over! youre
awesome thanks for all the help, youre so nice!
 
H

honeygrl33

i created a new file w/ur code -- still looped over and over! youre
awesome thanks for all the help, youre so nice!
 
H

honeygrl33

Enter the store's sales for the month of January:0.000000
Enter the store's sales for the month of January:0.000000
Enter the store's sales for the month of January:0.000000
Enter the store's sales for the month of January:0.000000
Enter the store's sales for the month of January:0.000000
Enter the store's sales for the month of January:0.000000
Enter the store's sales for the month of January:0.000000
Enter the store's sales for the month of January:0.000000
Enter the store's sales for the month of January:0.000000
Enter the store's sales for the month of January:0.000000
Enter the store's sales for the month of January:0.000000

That's what yours does, and mine does too -- I fixed discrepancies (ie:
if it says lf% instead of f%)
 
K

karl malbrain

honeygrl33 said:
i created a new file w/ur code -- still looped over and over! youre
awesome thanks for all the help, youre so nice!

Pay no attention to the "error" message that googlegroups is giving you
about "not posting", or "queueing" your messages -- it's causing duplicate
postings. karl m
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,405
Latest member
DavidCex

Latest Threads

Top