Thread: Stuck in an inifinite loop?

1. Stuck in an inifinite loop?

I think I'm stuck within the first loop or I have no idea what the problem is.

TEXT FILE
Code:
5
3.00
3.50
4.50
5.00
6.00
3
1 1 1 1 1
0 0 2 1 6
1 0 3 2 3
CODE

Code:
<#include <stdio.h>

int main (void)
{
int numOfPizzaPrices;
int i, a;
double pizzaPrices[i];
int numOfOrders;
int day;
int pizzaOrder[i][a];
int sum = 0;

FILE *ifp;
ifp = fopen("pizza.txt", "r");

fscanf(ifp, "%d", &numOfPizzaPrices);

for(i = 0; i < numOfPizzaPrices; i++)
{
fscanf(ifp, "%.2f", &pizzaPrices[i]);
}

fscanf(ifp, "%d", &numOfOrders);
for (day = 1; day < numOfOrders; day++)
{
for(i = 0; i < numOfPizzaPrices; i++)
{
for(a = 0; a < numOfPizzaPrices; a++)
{
fscanf(ifp, "%d", &pizzaOrder[i][a]);
sum = sum + (pizzaOrder[i][a] * pizzaPrices[i]);
}
}
printf("On day %d, you will spend \$%d at Lazy Moon.", &day, &sum);
}

system("PAUSE");
return 0;
}

2. The variable i is uninitialized, so, assuming you are compiling as C99, which otherwise you can't do what you're trying to in the first place, you're ending up with some random value for the size of i. The same goes for a.

Quzah.

3. thanks, worked fine. but now my calculation is wrong on

sum = sum + (pizzaOrder[i][a] * pizzaPrices[i]);

i changed my printf line and removed the &'s.

for the calculation, it should read the text file and i asumme the use of [i][a] will add the values inside them. such as [1][4] = 6?

4. Swap A and I on your two inner loops. This:
Code:
foo[ X ][ Y ]
Is considered to be: X rows of Y elements. Also, you're starting one of your loops at 1.

Quzah.

5. Originally Posted by quzah
Swap A and I on your two inner loops. This:
Code:
foo[ X ][ Y ]
Is considered to be: X rows of Y elements. Also, you're starting one of your loops at 1.

Quzah.
i tried
Code:
for (day = 1; day < numOfOrders; day++)
{
for(a = 0; a < numOfPizzaPrices; a++)
{
for(i = 0; i < numOfPizzaPrices; i++)
{
fscanf(ifp, "%d", &pizzaOrder[i][a]);
sum = sum + (pizzaOrder[i][a] * pizzaPrices[i]);
}
}
printf("On day %d, you will spend \$%.2f at Lazy Moon.\n", day, sum);
}
if thats what you meant, sorry if i'm misunderstanding you.

6. Code:
3
1 1 1 1 1
0 0 2 1 6
1 0 3 2 3
You've already read the columns above as numofprices. So now you read the number of rows as numoforders. I guess that's what you're doing anyway. So then...
Code:
for each row
for each order
read one value into x[ row ][ column ]
Now at the end, I assume you're going to do something like:
Code:
for each row
for each column
print price[ column ] * x[ row ][ column ]
Something like that?

Quzah.

7. its kinda hard to explain without giving you the whole problem, but thats a bit to read i dont want you to do.

Each of these lines will contain n non-negative integers, representing how many of those pizzas, respectively are in the order. (For example, if n=5 and the line contains the values 0 0 6 0 9, then the order contains 6 slices of pizza #2 and 9 slices of pizza #4.)

from the problem, if that doesn't help, want me to post the whole problem?

8. I pretty much just described it in my previous post. Read a bunch of prices. Read a bunch of days, and how much of each price was ordered on each day. Figure out how much the cost was for each item on a day, or the grand total, or what not. Look at my previous post, that covers what you're wanting to do.

I find it easier if I name my row and column variables as row and column, rather than just A or I. It helps me sort it out in my head easier.

Quzah.