简体   繁体   English

使用c中的用户输入和函数计算数组的总和和平均值

[英]Calculating sum and average of an array with user input and functions in c

beginner C coder here and I'm trying to figure out what exactly is going wrong with my code. 初学者C编码器,我试图找出我的代码到底出了什么问题。 I'm using two functions to calculate the sum and average of an array that a user inputted. 我正在使用两个函数来计算用户输入的数组的总和和平均值。 The size of the array is determined by the user. 数组的大小由用户确定。 I've tried looking up examples but am not finding many helpful tips. 我尝试查找示例,但是找不到很多有用的提示。 It's more likely that I'm just not understanding what I'm doing wrong. 我更有可能只是不了解自己在做什么错。 Can anyone please grant me some constructive criticism and guidance. 谁能给我一些建设性的批评和指导。 Thank you! 谢谢!

#include<stdio.h>

/*function declarations*/
int addNumbers(int number[]); 
float avgNumbers(int sum, int n);

int main(){
    int i, n, number[100], sum, result;
    float average;

    printf("How many numbers do you want to enter?\n");
    scanf("%d",&n);

    for (i = 0; i < n ; i++)  
   {
       printf("Enter the numbers %d:\n", i+1);
       scanf("%d",&number[i]);
   }
    for (i = 0; i < n ; i++)  
    {
       sum = 0;
       sum = addNumbers(&number);
       average = avgNumbers(sum,n);
    }

    printf("Sum: %d\n",sum);
    printf("Average: %f\n",average);

    return 0;
}

/*function returning the sum of the numbers*/
int addNumbers(int number[])
{
    int i, n, sum;
    for (i = 0; i < n ; i++)
    sum += number[i]; 
    return sum;
    }

/*function returning the average of the numbers*/
float avgNumbers(int sum, int n)
{
    average = sum/n
    return average;
    }

There is no need of for loop while calling addNumbers() and avgNumbers(). 调用addNumbers()和avgNumbers()时不需要for循环。 Also you are sending the address in place of value in method. 同样,您正在发送地址代替方法中的值。 Replace your code with this code. 用此代码替换您的代码。

sum = 0;
sum = addNumbers(number);
average = avgNumbers(sum,n);

You no need loop in the main function because of you already a loop in your addNumber(int number[]) but you need to pass the number of elements in the array as an argument to addNumbers(int number[], int n) like this. 您不需要在main函数中进行循环,因为您已经在addNumber(int number[])了循环,但是您需要将数组中的元素数量作为参数传递给addNumbers(int number[], int n)例如这个。 And you are assigning sum = 0 in the for loop every time. 并且您每次都在for循环中分配sum = 0 In avgNumbers(int sum, int n ) you need to declare average and assign 0 to it. avgNumbers(int sum, int n )您需要声明average并为其分配0。

Your code should be like this. 您的代码应如下所示。

#include<stdio.h>

/*function declarations*/
int addNumbers (int number[], int n);
float avgNumbers (int sum, int n);

int main (){
    int i, n, number[100], sum, result;
    float average;

    printf ("How many numbers do you want to enter?\n");
    scanf ("%d", &n);
    for (i = 0; i < n; i++){
        printf ("Enter the numbers %d:\n", i + 1);
        scanf ("%d", &number[i]);
    }
    sum = 0;
    sum = addNumbers(number, n);
    average = avgNumbers(sum, n);


    printf ("Sum: %d\n", sum);
    printf ("Average: %f\n", average);
    return 0;
}

/*function returning the sum of the numbers*/
int addNumbers (int number[], int n){
    int i, sum=0;
    for (i = 0; i < n; i++)
        sum += number[i];
    return sum;
}

/*function returning the average of the numbers*/
float avgNumbers (int sum, int n){
    float average = 0;
    average = sum / n;
    return average;
}

First of all you are using return type float while calculating average but the parameters are int . 首先,您在计算平均值时使用返回类型float,但参数为int。 In this case when you divide sum by n only the integer part is stored and hence the average value you will obtain be incorrect. 在这种情况下,将和除以n时,仅会存储整数部分,因此得出的平均值不正确。 You must typecast your value before actually performing division. 在实际执行除法之前,您必须输入值。 Typecasting is very necessary else incorrect results may be produced. 类型转换是非常必要的,否则可能会产生错误的结果。

The next thing is you donot need for loop in the main function 接下来的事情是您不需要在主函数中进行循环

#include<stdio.h>
/*function declarations*/
int addNumbers (int number[], int n);
float avgNumbers (int sum, int n);

int main (){
    int i, n, number[100], sum, result;
    float average;

    printf ("How many numbers do you want to enter?\n");
    scanf ("%d", &n);
    for (i = 0; i < n; i++){
        printf ("Enter the numbers %d:\n", i + 1);
        scanf ("%d", &number[i]);
    }
    sum = 0;
    sum = addNumbers(number, n);
    average = avgNumbers(sum, n);


    printf ("Sum: %d\n", sum);
    printf ("Average: %f\n", average);
    return 0;
}

/*function returning the sum of the numbers*/
int addNumbers (int number[], int n){
    int i, sum=0;
    for (i = 0; i < n; i++)
        sum += number[i];
    return sum;
}

/*function returning the average of the numbers*/
float avgNumbers (int sum, int n){
    float average = 0;
    average = (float)sum / n;
    return average;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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