Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 5a)
- typedef struct
- {
- char pName[31];
- float cost, retail;
- }product_t;
- 5b)
- // Read the number of stores and details of each store
- // into array storeList.
- // Return the number of stores.
- int readStores(store_t storeList[])
- {
- int numStores, i, j;
- FILE * fp;
- fp = fopen("stores.in", "r");
- /* Read number of stores */
- fscanf(fp, "%d", &numStores);
- /* For each Store */
- for(i = 0; i < numStores; i++)
- {
- fscanf(fp, "%s %d %d %f %d", storeList[i].sname, &storeList[i].x, &storeList[i].y, &storeList[i].radius, &storeList[i].numProduct);
- /* For each product */
- for(j=0; j<storeList[i].numProduct; j++)
- fscanf(fp, "%s %f %f", storeList[i].products[j].pName, &storeList[i].products[j].cost, &storeList[i].products[j].retail);
- }
- fclose(fp);
- return numStores;
- }
- 5c)
- // Return 1 if user is within store's radius of influence,
- // or 0 otherwise.
- int withinRadius(int x, int y, store_t store)
- {
- float distance = sqrt(pow(store.x - x, 2) + pow(store.y - y, 2));
- return (store.radius >= distance);
- }
- 5d)
- // Print the names of stores where the user's current
- // location is within their circles of influence.
- void printNearbyStores(int x, int y, store_t storeList[], int numStore)
- {
- int i;
- for(i = 0; i < numStore; i++)
- if(withinRadius(x, y, storeList[i]))
- puts(storeList[i].sname);
- }
- 5e)
- // Find and label the products that are good buys.
- // You are to decide on the return type and parameters of
- // this function.
- /* Return "void" as functions objective is to just label */
- void labelGoodBuys(store_t storeList[], int numStore)
- {
- int i, j;
- float difference;
- /* For each store */
- for(i = 0; i < numStore; i++)
- {
- /* For each product */
- for(j = 0; j < storeList[i].numProduct; j++)
- {
- difference = storeList[i].products[j].retail - storeList[i].products[j].cost;
- if(difference <= (0.05 * storeList[i].products[j].cost))
- strcat(storeList[i].products[j].pName, "***Must Buy***");
- }
- }
- }
- 5f)
- // Determine which store has the lowest retail price
- // for a given product. You are to decide on the
- // return type and parameters of this function.
- int findCheapestStore(char productName[], store_t storeList[], int numStore)
- {
- int i, j, storeIndex;
- char newProductName[31];
- float lowestRetail = 1001;
- /* If product name has label.. */
- strcpy(newProductName, productName);
- strcat(newProductName, "***Must Buy***");
- /* For each store */
- for(i = 0; i < numStore; i++)
- {
- /* For each product */
- for(j = 0; j < storeList[i].numProduct; j++)
- {
- /* Check for matching product name with or w/o label */
- if(strcmp(productName, storeList[i].products[j].pName) == 0 || strcmp(newProductName, storeList[i].products[j].pName) == 0)
- {
- if(storeList[i].products[j].retail < lowestRetail)
- storeIndex = i;
- }
- }
- }
- return storeIndex;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement