gpt4 book ai didi

c - 从两个数组和一个整数 C 语言中返回一个值

转载 作者:太空宇宙 更新时间:2023-11-04 01:48:29 25 4
gpt4 key购买 nike

int main()
{
double dUnitPriceM[]={19.99, 50.50, 2.10};
long lOrderQuantityM[] = {10, 2, 4};
int iItemCount = 3;
double dTotalCost;
dTotalCost = calculateTotalCost(dUnitPriceM, lOrderQuantityM, iItemCount);
printf("Total cost is %10.2lf\n", dTotalCost);
}
// code for calculateTotalCost function ??
double calculateTotalCost(double dUnitPriceM[], long lQuantityM[],
int iItemCount)
{
}

我是 C 语言编码的初学者,我无法理解如何使用数组。我想出了创建伪代码的逻辑,但我无法对其进行编码。我所知道的是,我必须从 i =1 的值开始;因为 i <= 项目数,i++。

然后将 UnitPriceM[0] * QuantityM[0] 的结果分配给 i,将它们递增到下一个数组,直到达到其最后一个值。然后将所有 i 的总和相加,例如如果 i1= 100 + i2 = 120 + i3 =45 将它们作为总成本返回。

最佳答案

#include <stdio.h>
double calculateTotalCost(double unitPrice[], long quantity[],int itemCount){
int i;
double totalCost=0.0;

for(i=0;i<itemCount;i++){
totalCost +=unitPrice[i] * quantity[i];
}
return totalCost;

}
void main() {
int i;
double dUnitPriceM[]={19.99, 50.50, 2.10};
long lOrderQuantityM[] = {10, 2, 4};
int iItemCount = 3;
double totalCost =0.0;
totalCost= calculateTotalCost(dUnitPriceM, lOrderQuantityM, iItemCount);
printf("Total cost is %f ", totalCost);
}

关于c - 从两个数组和一个整数 C 语言中返回一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48555687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com