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

标签 c arrays parameters

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/

相关文章:

ios - 如何在 tableview 单元格内的 Collection View 中传递多维数组?

java - 将单个二进制字符串值拆分为数组

php - 通过 wordpress 中的 url 参数 (meta) 发布博客文章内容

sql - 在 WHERE 子句中使用带有 LIKE 的参数化 SQL(Pervasive SQL)

c - 编译 C 程序时,出现此错误 : hello: no such file or directory

c++ - Cygwin 1.7.28 中的 mkstemp() 和 fdopen()

c中socket编程的客户端服务器进程执行

jquery - 如何使用 jQuery 获取属性值并从结果中提取子字符串

c++ - 从另一个参数的值设置默认参数值

c - 在 C 2.0 统计错误中读取目录