如何从一个数组中找到每个分支的百分比?在C

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从一个数组中找到每个分支的百分比?在C相关的知识,希望对你有一定的参考价值。

(问题底部的编辑。)

例如,让我们有一个数组,其中填充了一年中每个月的一些元素。用户首先输入他想输入数据的分支数量,因此程序会自行循环并将其存储在one数组中,我们制作了另一个函数,并让其计算了输入的所有数据的总和用户。现在,该怎么做,我们需要使其显示每个分支整体的百分比。

Input: Please enter the number of branches you'd like to input data about : 2
Branch 1
Enter the data for the month 1 : 123
Enter the data for the month 2 : 123
Enter the data for the month 3 : 123
Enter the data for the month 4 : 123
Enter the data for the month 5 : 123
Enter the data for the month 6 : 123
Enter the data for the month 7 : 123
Enter the data for the month 8 : 123
Enter the data for the month 9 : 123
Enter the data for the month 10 : 123
Enter the data for the month 11 : 123
Enter the data for the month 12 : 123

Branch 2
Enter the data for the month 1 : 123
Enter the data for the month 2 : 123
Enter the data for the month 3 : 123
Enter the data for the month 4 : 123
Enter the data for the month 5 : 123
Enter the data for the month 6 : 123
Enter the data for the month 7 : 123
Enter the data for the month 8 : 123
Enter the data for the month 9 : 123
Enter the data for the month 10 : 123
Enter the data for the month 11 : 123
Enter the data for the month 12 : 123


The sum of all the branches income combined is = 2952.000000 

Required output: Branch 1 has : 50%
 Branch 2 has : 50%

(是的,这是我第一次在这里问问题)

((如果需要编辑,这里是完整的代码2:感谢Armali修复了旧的破烂代码btw)

#include <stdio.h>
#include <stdlib.h>

void Data_Of_Branches(int number_of_branches, float arr_brms[number_of_branches][12])
{
  int i;
  int j;
  for (i = 0; i < number_of_branches; i++)
  {
    printf("Branch %d\n", i + 1);
    for (j = 0; j < 12; j++)
    {
      printf("Enter the data for the month %d: ", j + 1);
      scanf("%f", &arr_brms[i][j]);
    }
    printf("\n");
  }
}

float sum_of_sales(int number_of_branches ,float arr_brms[number_of_branches][12],float total_sale)
{
  total_sale = 0;
  for (int i = 0; i < number_of_branches; i++)
    for (int j = 0; j < 12; j++) total_sale += arr_brms[i][j];
  printf("The sum of all the branches income combined is = %f\n\n", total_sale);
return total_sale;}

void percentage(int number_of_branches,float arr_brms[number_of_branches][12],float total_sale)
{
int i ,j, m;
    float total = 0;
    total = total_sale;
   for(i=0;i<number_of_branches;i++){
      for(j=0;j<m;j++){
        total = total + arr_brms[i][j];}
                printf("Branch %d : ",i+1);
    float per , sum ;
    for(i=0;i<number_of_branches;i++){
      sum=0;
      per=0;
    }
      for(j=0;j<m;j++){
      sum+= arr_brms[i][j];

        per = (sum / total) * 100.0;
        printf("Percentage = %.2f%%",per);
        printf("\n") ;
    }
}
    }

int main()
{
  printf(
      "Please enter the number of branches you'd like to input data about: ");
  int number_of_branches = 0;
  scanf("%d", &number_of_branches);
  float arr_brms[number_of_branches][12];
  Data_Of_Branches(number_of_branches, arr_brms);
  printf("Hello. please enter the number of the operation you'd like to do. "
         "(please only enter the number.) \n \n");
  int choice;
    float total_sale;
  while (
    printf(" 1. Enter sales data.\n 2. Add a record for a new branch \n 3. "
           "Delete record of an existing branch \n 4. Calculate total sales \n "
           "5. Calculate percentage share of each branch \n 6. Determine the "
           "month of the peak sales \n 7. Display sales of a specific month \n "
           "8. Display sales of a specific branch \n 0. Done \n"),
    scanf("%d", &choice) > 0
        )
  {
    if (choice == 0) exit(0);
    if (choice == 1) Data_Of_Branches(number_of_branches, arr_brms);
    if (choice == 4) sum_of_sales(number_of_branches, arr_brms,total_sale);
        if (choice == 5)percentage(number_of_branches, arr_brms,total_sale);
  }
  return 0;
}

编辑:好的,所以基本上我需要的是解决sum函数的方法。因为它一直显示错误的数字。另一个函数显示总和中每个分支的百分比编辑2:好的,所以在用Armali放的新代码替换旧代码之后。我继续前进,开始失败-添加百分比函数失败。因此,如果可能的话,有一种方法可以修复空隙百分比函数?

答案

…我需要的是解决sum函数的方法。因为它一直显示错误的数字。

当然它一直显示错误的数字,因为它获取的数据不完整,正如Oli L正确解释的那样:

当您在Data_Of_Branches函数中读取每个分支的数据时,会将数据写入到arr_month相同的数组中。这意味着您将覆盖早期分支的数据。因此,您的总和是错误的,您将无法计算每个分支的百分比收入

一种解决方案是将数据存储在二维数组中,不仅每个月,而且每个分支。然后sum function可以遍历两者并计算正确的总和。除此之外,您的主循环可能无法满足您的要求。您不应该在此递归调用main。这是您函数的修改版本-因此,应该很容易实现选择5

void Data_Of_Branches(int number_of_branches, float arr_brms[number_of_branches][12])
{
  int i;
  int j;
  for (i = 0; i < number_of_branches; i++)
  {
    printf("Branch %d\n", i + 1);
    for (j = 0; j < 12; j++)
    {
      printf("Enter the data for the month %d: ", j + 1);
      scanf("%f", &arr_brms[i][j]);
    }
    printf("\n");
  }
}

void sum_of_sales(int number_of_branches, float arr_brms[number_of_branches][12])
{
  float sum = 0;
  for (int i = 0; i < number_of_branches; i++)
    for (int j = 0; j < 12; j++) sum += arr_brms[i][j];
  printf("The sum of all the branches income combined is = %f\n", sum);
}

int main()
{
  printf(
      "Please enter the number of branches you'd like to input data about: ");
  int number_of_branches = 0;
  scanf("%d", &number_of_branches);
  float arr_brms[number_of_branches][12];   // matrix for branches and months
  Data_Of_Branches(number_of_branches, arr_brms);
  printf("Hello. please enter the number of the operation you'd like to do. "
         "(please only enter the number.) \n \n");
  int choice;
  while (
    printf(" 1. Enter sales data.\n 2. Add a record for a new branch \n 3. "
           "Delete record of an existing branch \n 4. Calculate total sales \n "
           "5. Calculate percentage share of each branch \n 6. Determine the "
           "month of the peak sales \n 7. Display sales of a specific month \n "
           "8. Display sales of a specific branch \n 0. Done \n"),
    scanf("%d", &choice) > 0
        )
  {
    if (choice == 0) exit(0);
    if (choice == 1) Data_Of_Branches(number_of_branches, arr_brms);
    if (choice == 4) sum_of_sales(number_of_branches, arr_brms);
  }
  return 0;
}

请注意,Data_Of_Branches仍然容易受到格式错误的输入的影响。

我继续并开始-添加百分比函数失败-]。>

您主要忘记将sum_of_sales()的返回值分配给total_sale。 (顺便说一句,将total_sale的未初始化值作为sum_of_sales的第三个参数传递是没有意义的。)但我不会这样做,因为这需要在4之前选择5。相反,我会在sum_of_sales()函数中调用percentage,其中您对太多的代码感到困惑(并在缺少初始化12的同时将m替换为m)-如果修剪,它会起作用:] >

void percentage(int number_of_branches, float arr_brms[number_of_branches][12])
{
    int i, j, m = 12;
    float total = sum_of_sales(number_of_branches, arr_brms);
    for (i = 0; i < number_of_branches; i++)
    {
        printf("Branch %d: ", i+1);
        float per, sum = 0;
        for (j = 0; j < m; j++) sum += arr_brms[i][j];
        per = sum / total * 100.0;
        printf("Percentage = %.2f%%\n", per);
    }
}

以上是关于如何从一个数组中找到每个分支的百分比?在C的主要内容,如果未能解决你的问题,请参考以下文章

如何在 C++/Rcpp 中进行快速百分位数计算

如何在axios中迭代在每个循环中返回新的百分比+数组

如何从 GitHub 上可用的代码创建分支 [重复]

我有很多字节数组;每个都是一个字符串。我如何找到每个使用的编码?

如何从片段内的列表视图打开链接网址?

如何在 numpy / scipy 中获取特定百分位数的索引?