Open Side Menu Go to the Top
Register
A C program. A C program.

10-10-2009 , 02:23 PM
Hi guys,

I have two arrays:

slices_ordered[15]

and

price_of_slice[5]

I am trying to multiply them into a new array cost_of_pizza[3].

To better explain we want to know the cost of three orders of pizza. There are 5 distinct slices offered by the place, and each has it's own price. For order one the slices_ordered array looks like:

1 1 1 1 1

for order two:

0 0 2 1 6

and for order three:

1 0 3 2 3

Each number corresponds with how many slices of that pizza you want in your order. So in order one we would want one slice of each pizza offered.

The prices of the pizza slices are

3.00, 3.50, 4.50, 5.00, 6.00

So the total cost for pizza one would be 22$.

The thing is though that how many slices are offered, the prices of each slice, and how many slices we want are being inputted by the teacher so the program has use arrays without numbers and instead variables that are listed in the input file. I have that working fine, I just need to know how to put my elements into the final array correctly. I have tried a nested loop to no avail and I am unsure of what to do next. Any help?
A C program. Quote
10-10-2009 , 03:58 PM
shouldve just posted your entire code and comment where its broke and your desired result. how could we know why doesnt the nested loop wont work?

theres probably a better way to do this than youre doing, but since you cant differentiate between the diff orders when youre storing the values in the arrays, youre gonna have to do the calculation right after its inputted, and then start with fresh arrays.
A C program. Quote
10-11-2009 , 11:59 AM
Quote:
Originally Posted by greg nice
shouldve just posted your entire code and comment where its broke and your desired result. how could we know why doesnt the nested loop wont work?

theres probably a better way to do this than youre doing, but since you cant differentiate between the diff orders when youre storing the values in the arrays, youre gonna have to do the calculation right after its inputted, and then start with fresh arrays.
Good idea.

Quote:

#include <stdio.h>

int main()
{
FILE *ifp;

ifp = fopen("pizza.txt", "r"); //Opens the file specified in the red text, the r stands for read as opposed to w for write

int number_of_slices_offered;

fscanf(ifp, "%d", &number_of_slices_offered);
float price_of_slice[number_of_slices_offered];

int i;

for(i = 0; i < number_of_slices_offered; i++)
{
fscanf(ifp, "%f", &price_of_slice[i]);
}

printf("%d\n\n", number_of_slices_offered);

for(i = 0; i < number_of_slices_offered; i++)
{
printf("%.2f\n", price_of_slice[i]);
}

int orders_wanted;
fscanf(ifp, "%d", &orders_wanted);

printf("\n%d\n\n", orders_wanted);

int slices_ordered[number_of_slices_offered * orders_wanted];

for(i = 0; i < number_of_slices_offered * orders_wanted; i++)
fscanf(ifp, "%d", &slices_ordered[i]);

for(i = 0; i < number_of_slices_offered * orders_wanted; i++)
{
printf("%d ", slices_ordered[i]);
}


system("PAUSE");
return 0;
}
Quote:
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
First file is the .c file, the second is pizza.txt. The problem I am having is I need to create three different orders and calculate their costs. The last 4 lines of pizza.txt give me that info, the "3" stands for orders wanted, each line below that is one order. It reads:

1 slice of slice1, 1 slice of slice 2, 1 of 3, 1 of 4, 1 of 5.
0 of 1, 0 of 2, 2 of 3, 1 of 4, 6 of 5
1 of 1, 0 of 2, 3 of 3, 2 of 4, 3 of 5.

The thing is that the input changes so the loops have to use the variable corresponding with the three and five instead of the numbers 3 and 5 themselves. Does that make sense?
A C program. Quote
10-11-2009 , 01:23 PM
ok. well. first, does the code work as expected how it is right now? i assume it does, and the output on the screen shows the slices_ordered[] array as 1 1 1 1 1 0 0 2 1 6 1 1 0 3 2 3? yes? i was wondering why in the OP you had slices_ordered[15], i didnt understand where the 15 came from. i wouldnt have done it like this.

firstly, i dropped out of uni years ago, so my CS skills arent sharp. solving these little problems were fun for me. i think theres gotta be better ways to do this. but i dont know if you have limitations as to what the input txt file has to be or if you have to use the arrays as it is, or what.

if you want to keep your structure and setup, then what you could do is this (forgive syntax, i forgot C):

Quote:
for (i=0; i < orders_wanted; i++) {

float order_price = 0;
printf("Total price for order # ", i , "is: ");

for (x=0; x < number_of_slices_offered; x++) {
float cost_of_each_slices = 0;
int pos = (i * number_of_slices_offered) + x; //calc the position we want in array
cost_of_each_slices = slices_ordered[pos] * price_of_slice[x];
order_price = order_price + cost_of_each_slices;
}

printf(order_price, \n);

}
now this seems kinda crude way to do it to me, i mean it has no error checking. what if the input file has mistakes in it? then this code will surely break.

this is how i wouldve done the nested loop, if I even structed my code like you in the first place. you probably made it harder on yourself by clumping all 15 into one array, and then having to go back and retrieve. so the tricky part is in getting the correct position. thats why perhaps theres a better way, but that depends on the limitations of the inputs etc. i have no compiler and dont care to test it. so if this doesnt work then maybe i screwed up. what do you think?

Last edited by greg nice; 10-11-2009 at 01:40 PM.
A C program. Quote

      
m