Friday, July 31, 2009

C programming loop structure arrays?

I am trying to write a c program that can easily compare pizza prices. it will take in a bunch of pizza information, spit it out with the cheapest deal.





I want to use for loops to take in the different deals..because the number of deals change every night the user must enter how many deals he is inserting to the program.





then I donnt know where to go from there. how to take in the information and fill into an array? how do you take the information that is in the array and calculate and coompare the prices? I am completely lost...





could I also use strings so deal one = pizza hut and deal two = dominoes and deal three = local vendor rather than use Deal #1 and Deal #2....





the unit of comparison should be in dollar per square inch.


please help. you dont have to do all the work, I just need to know how to get started. thank you

C programming loop structure arrays?
ok here are some ideas:


first I would allocate the arrays but since this is for your example I am making it easy...





you could make a struct and then make an array of these structs use cin to get the data


so


struct PizzaDeals


{


char PizzaCompanyName[32]; // some reasonable size


float pizzaSize; // diameter in inches


float price;


};





now


PizzaDeals pizzaDeals[25]; // maybe uptp 25 deals???





and


get the data:





int i, count;


count = 0;


for(i=0; i %26lt; 25; i++)


{


printf("enter pizza name: ");


scanf("%s",pizzaDeals[i].PizzaCompanyN...


prinft("enter size: ");


scanf("%f", %26amp;pizzaDeals[i].pizzaSize);


prinft("enter price: ");


scanf("%f", %26amp;pizzaDeals[i].price);


count++;


}





now calculate the best deal





int bestIndex = 0;


float bestPrice = 99999.99; just make a number that your first cal is less than.


now loop through calculating...





for( i = 0; i %26lt;count; i++)


{


if( pizzaDeals[i].price/(pizzaDeals[i].pizza... )%26lt; bestPrice)


{


bestPrice = pizzaDeals[i].price/(pizzaDeals[i].pizza...


bestIndex = i;


}


}


printf("Best Deal = %s, at $%f",pizzaDeals[i].PizzaCompanyName,pizz...





note there is no error checking or smartness in array management...
Reply:sudo code....


struct pizzas{


char *name;


int price;


};


int main(int argc, char *argv[]){


int numofdeals, i;


struct pizzas *ptr;


fgets()


numofdeals = strtol();


ptr = malloc(numofdeals * sizeof(struct pizzas);


for(i = 0; i %26lt; numofdeals; i++){


/*fill in data*/


ptr[i].name = name;


ptr[i].price = price;


}


for(i = 0; i %26lt; numofdeals; i++){


/*process data*/


ptr[i].name;


ptr[i].price;


}


free(ptr);


return 0;


}
Reply:you could also use the statements IF AND OR ETC
Reply:When you say deal it sounds like you are talking about more than just prices. Examples would be deal 1: 12 slices for $12, 16 slices for $14, $4 slices for $6, etc.





Therefore you need your deal in a struct. You get user input from stdin in a loop because you need to fill your array.





When you have all your deals in the array, you'll need to traverse it to find the best deal. So for example, you'll have a for loop that goes through your array and finds the highest number of slices per dollar.





Once you find the best deal you'll have the function return this value and that's it.
Reply:Well, you could use structures





typedef struct pizzadef


{


char[20] vendor;


long inches;


double price;


} pizza;





In c++, you could make it a class, but you can still do lots of good stuff with C.





I'd store the deals and such in a file, so you can pull them in and calculate on the fly.


No comments:

Post a Comment