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.