简体   繁体   中英

How would I find the smallest total in a for loop?

For a school assignment, I was asked to create a program that allowed the user to visit X number of shops, and from each shop, they could purchase Y number of ingredients, each was sold for Z price.
The problem I am having is, as part of the specifications for the assignment, at the end of the program, it should be able to tell the user at which shop they spent the least. For example, the ending should say: "You spent the least at Shop 2, spending $7.00." or something to that effect. I'm not sure how to do that, and retain the program's ability to tell the total cost spent at each shop.

Example

"Your total for shop 1 was $15.00." "Your total for shop 2 was $7.00." "Your total for shop 3 was $10.00." "Your cheapest order was at shop 2, and cost you $7.00."

Here is the code I have for now:

//pre processes directive
#include <stdio.h>

//main function
int main(){
    int shop_number, shop_visited, ingredient_number, ingredient_purchased;
    float price, price_total, price_total_final;

    printf("How many shops will you visit?\n\n");
    scanf("%d", &shop_number);
    for (shop_visited = 1; shop_visited <= shop_number; shop_visited++) {
        printf("How many ingredients will you purchase from shop %d?\n\n",
               shop_visited);
        scanf("%d", &ingredient_number);
        for (ingredient_purchased = 1;
             ingredient_purchased <= ingredient_number;
             ingredient_purchased++) {
            printf("What is the price for ingredient %d?\n\n",
                   ingredient_purchased);
            scanf("%f", &price);
            price_total = price_total + price;
        }
        printf("Your total price for this shop is $%.2f\n\n", price_total);
        price_total = 0;
    }

    return 0;
}

For a school assignment, I was asked to create a program that allowed the user to visit X number of shops, and from each shop, they could purchase Y number of ingredients, each was sold for Z price.
The problem I am having is, as part of the specifications for the assignment, at the end of the program, it should be able to tell the user at which shop they spent the least. For example, the ending should say: "You spent the least at Shop 2, spending $7.00." or something to that effect. I'm not sure how to do that, and retain the program's ability to tell the total cost spent at each shop.

Example

"Your total for shop 1 was $15.00." "Your total for shop 2 was $7.00." "Your total for shop 3 was $10.00." "Your cheapest order was at shop 2, and cost you $7.00."

Here is the code I have for now:

//pre processes directive
#include <stdio.h>

//main function
int main(){
    int shop_number, shop_visited, ingredient_number, ingredient_purchased;
    float price, price_total, price_total_final;

    printf("How many shops will you visit?\n\n");
    scanf("%d", &shop_number);
    for (shop_visited = 1; shop_visited <= shop_number; shop_visited++) {
        printf("How many ingredients will you purchase from shop %d?\n\n",
               shop_visited);
        scanf("%d", &ingredient_number);
        for (ingredient_purchased = 1;
             ingredient_purchased <= ingredient_number;
             ingredient_purchased++) {
            printf("What is the price for ingredient %d?\n\n",
                   ingredient_purchased);
            scanf("%f", &price);
            price_total = price_total + price;
        }
        printf("Your total price for this shop is $%.2f\n\n", price_total);
        price_total = 0;
    }

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM