Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C编程:在for循环中保存值并找到最小值_C - Fatal编程技术网

C编程:在for循环中保存值并找到最小值

C编程:在for循环中保存值并找到最小值,c,C,在过去的5个小时里,我一直坐在这里试图弄明白这一点。我是C编程新手 我需要能够比较for循环中的值并存储它们,最后打印出哪个商店与相关商店的价格最便宜。这就是我所拥有的,它不断给我奇怪的输出,并且没有正确地计算订单: #include <stdio.h> int main() { int shops = 0, ingredients = 0, i=0, j=0, cstore=0; double temp = 0.0, ingredientPrice = 0.0,

在过去的5个小时里,我一直坐在这里试图弄明白这一点。我是C编程新手

我需要能够比较for循环中的值并存储它们,最后打印出哪个商店与相关商店的价格最便宜。这就是我所拥有的,它不断给我奇怪的输出,并且没有正确地计算订单:

#include <stdio.h>
int main() {

    int shops = 0, ingredients = 0, i=0, j=0, cstore=0;
    double temp = 0.0, ingredientPrice = 0.0, total, cprice=0.0;

    printf("How many shops will you be visiting?\n");
    scanf("%d",&shops);

    for(i = 0; i < shops; i++){
        printf("You are at shop # %d\n",i+1);
        printf("How many ingredients do you need at shop #%d?\n", i+1);
        scanf("%d",&ingredients);

        for(j = 0; j < ingredients; j++) {
            printf("How much is ingredient #%d?\n", j+1);
            scanf("%lf", &temp);
            ingredientPrice += temp;
            total = ingredientPrice;

            if (shops == 1) {
                cstore = shops;
                cprice = total;
            }
            else if(total < ingredientPrice) {
                cstore = i+1;
                cprice = total;
            }
        }
        printf("Your total at shop #%d is $%lf\n", i+1, total);
    }
    printf("Your cheapest order was at shop #%d and cost $%lf.\n", cstore, cprice);
    return 0;
}
#包括
int main(){
int shops=0,配料=0,i=0,j=0,cstore=0;
双温=0.0,IngCreditPrice=0.0,总计,cprice=0.0;
printf(“您将参观多少家店铺?\n”);
scanf(“%d”、&shops);
对于(i=0;i
作为补充说明,我们不能使用数组,因为我们还不在这一章中。任何帮助都将不胜感激!谢谢你

此外,这是一份作业,以防有人需要澄清我的问题:

你的导师让你去几家不同的商店下单。此程序应询问将访问的商店总数。在每家商店,询问需要购买的配料数量。对于每种成分,询问价格。记录下订单的总金额,以便在离开商店前将其记录下来。这个节目 还应跟踪订单最便宜以及订单最便宜的商店。这将帮助你的导师决定与谁继续做生意

输入规范 1.商店的数量将是一个正整数。 2.配料的数量将是一个正整数。 3.每种成分的成本将是一个正实数

输出规格 将每个车间的成本输出到小数点后两位

您在#Y的总金额为X.XX美元

在程序结束时,打印最便宜的订单

您最便宜的订单是在Y店,价格为X.XX美元

输出样本

你将参观多少家商店? 3. 你在1号店。 1号店需要多少原料? 2. 一号配料多少钱? 3.5 2号配料多少钱? 10 你在1号店的总收入是13.50美元。 你在2号店。 2号店需要多少原料? 3. 一号配料多少钱? 2.75 2号配料多少钱? 3. 3号配料多少钱? 1.25 你在2号店的总收入是7美元。 你在3号店。 3号店需要多少原料? 2. 一号配料多少钱? 4. 2号配料多少钱? 5. 你在3号店的总收入是9美元。
您最便宜的订单是在2号店,价格为7.00美元。

只需将
cprice
初始化为一个相当大的值,然后在每次订单后将该变量与总数进行比较,如果较大,则进行更新

一个简单而安全的技巧是,由于弹药必须是正数,因此将
cprice=-1
设置为启用检查它是否已初始化

关于您的另一个问题,您的代码调用未定义的行为很可能是由于错误使用了
scanf()
函数

这是一个如何做到这一点的例子

#include <stdio.h>

int main() {

    int shops = 0, ingredients = 0, i = 0, j = 0, cstore = 0;
    double temp = 0.0, ingredientPrice = 0.0, total = 0.0, cprice = -1.0;

    printf("How many shops will you be visiting?\n");
    scanf("%d", &shops);

    for (i = 0; i < shops; i++){
        printf("You are at shop # %d\n", i + 1);
        printf("How many ingredients do you need at shop #%d?\n", i + 1);

        scanf("%d", &ingredients);
        for (j = 0; j < ingredients; j++) {
            printf("How much is ingredient #%d?\n", j + 1);
            scanf("%lf", &temp);
            ingredientPrice += temp;
            total = ingredientPrice;
            if (shops == 1) {
                cstore = shops;
                cprice = total;
            } else if (total < ingredientPrice) {
                cstore = i + 1;
                cprice = total;
            }
        }

        if ((cprice < 0.0) || (cprice > total)) {
            cprice = total;
            cstore = i;
        }
        printf("Your total at shop #%d is $%lf\n", i + 1, total);
    }
    printf("Your cheapest order was at shop #%d and cost $%lf.\n", cstore, cprice);
    return 0;
}
#包括
int main(){
int shops=0,配料=0,i=0,j=0,cstore=0;
双温=0.0,IngreditPrice=0.0,总温=0.0,cprice=-1.0;
printf(“您将参观多少家店铺?\n”);
scanf(“%d”、&shops);
对于(i=0;itotal)){
cprice=总数;
cstore=i;
}
printf(“您在店铺#%d的总计为$%lf\n”,i+1,总计);
}
printf(“您最便宜的订单在#%d店,价格为$%lf.\n”,cstore,cprice);
返回0;
}

我并不认为您的
scanf()
会导致未定义的行为,因为您仔细地初始化了所有内容。但这并不意味着这是正确或正确的方法,相反,您应该检查scanf()的成功情况,以确保输入有效。

仔细阅读此可能的解决方案。我没有检查
scanf()
的返回值,但是我很懒,并且无论如何都不会将其用于生产代码

#include <stdio.h>

int main() {

    int    i = 0, shops = 0;
    int    cstore = 0;          // current minimum store
    double cprice = 0.0;        // total for the current minimum store

    printf("How many shops will you be visiting?\n");
    scanf("%d", &shops);

    for (i = 1; i <= shops; i++){
        int    j = 0, ingredients = 0;
        double total = 0.0;      // total for the current store.

        printf("You are at shop # %d\n", i);
        printf("How many ingredients do you need at shop #%d?\n", i);
        scanf("%d", &ingredients);

        // get the prices of the ingredients at this store and total them up.
        for (j = 1; j <= ingredients; j++) {
            double ingredientPrice = 0.0;

            printf("How much is ingredient #%d?\n", j);
            scanf("%lf", &ingredientPrice);
            total += ingredientPrice;
        }
        if (i == 1) {
            // this is the first store so it is automatically the lowest thus far being the only one.
            cstore = i;
            cprice = total;
        }
        else if (total < cprice && ingredients > 0) {
            // the total for this store is less than the last known smallest total so lets update our variables.
            // if there are zero ingredients from this store then it does not count.
            cstore = i;
            cprice = total;
        }
        // the currency is US dollars with two decimal places.
        printf("Your total at shop #%d is $%.2lf and the lowest store is %d for $%.2lf.\n", i, total, cstore, cprice);
    }
    printf("Your cheapest order was at shop #%d and cost $%.2lf.\n", cstore, cprice);
    return 0;
}

在未检查返回值的情况下,您不应使用
scanf()
。对不起,您能详细说明一下吗?我不明白你说的是哪个值。因为你没有阅读scanf()
的文档,你的指导老师也没有告诉你它确实返回了一个值,它有非常重要的意义,你应该小心使用它。sscanf()在线文章。(提示:打印您认为刚刚扫描的值)“相当大的值”。不要那样做。不管怎样,它都不起作用
How many shops will you be visiting?
3
You are at shop # 1
How many ingredients do you need at shop #1?
2
How much is ingredient #1?
3
How much is ingredient #2?
4
Your total at shop #1 is $7.00 and the lowest store is 1 for $7.00.
You are at shop # 2
How many ingredients do you need at shop #2?
3
How much is ingredient #1?
1
How much is ingredient #2?
3
How much is ingredient #3?
4
Your total at shop #2 is $8.00 and the lowest store is 1 for $7.00.
You are at shop # 3
How many ingredients do you need at shop #3?
3
How much is ingredient #1?
1
How much is ingredient #2?
2
How much is ingredient #3?
2
Your total at shop #3 is $5.00 and the lowest store is 3 for $5.00.
Your cheapest order was at shop #3 and cost $5.00.