简体   繁体   English

使用数组计算 5 个整数的平均值

[英]Calculate Average of 5 Integers Using an Array

I can't seem to get it right.我似乎无法正确理解。 The question is "Calculate the Average of 5 Integers using an array"问题是“使用数组计算 5 个整数的平均值”

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

int main()
{
    int avg[5],i,total;
    int average;

    printf("Enter the marks entered in 5 subjects");
    for (i=0; i<5; ++i){
        scanf("%d",&avg[i]);
    }
    for(i=0; i<5; ++i){
        total = total + avg[i];
    }

    average= (float)total/5;
    printf("The average of 5 marks is %d",average);


    return 0;
}

1) Your answer CAN be a decimal number but you are storing it in an integer which ignores the decimal points. 1)您的答案可以是十进制数,但您将其存储在忽略小数点的整数中。 The variable average should be declared as float average;变量average应声明为float average; The line where you print the result should be changed to printf("The average of 5 marks is %f",average);打印结果的那一行应该改为printf("The average of 5 marks is %f",average);

2) Initialize the variable total as int total = 0; 2)将变量total初始化为int total = 0;

total应该初始化为0

int avg[5],i,total=0;

In the for loop's first iteration, variable total is used uninitialized, so the result is wrong since it will automatically acquire a garbage value if not initialized explicitly.在 for 循环的第一次迭代中,变量total使用未初始化,因此结果是错误的,因为如果未显式初始化,它将自动获取垃圾值。 Take this whole thing:拿这整件事:

#include <stdio.h>

int main() {
    int avg[5], i, total = 0, average;
    printf("Enter the marks entered in 5 subjects");
    for (i = 0; i < 5; ++i) {
        scanf("%d", &avg[i]);
        total += avg[i];
    }
    average = total / 5;
    printf("The average of 5 marks is %d", average);
    return 0;
}

You've just declared the variable total but didn't initialize it.您刚刚声明了变量 total 但没有对其进行初始化。 So, total contains a garbage value.所以,total 包含一个垃圾值。 For that if you add anything to total, the value will not be the correct as expected.为此,如果您将任何内容添加到总数中,则该值将不会像预期的那样正确。 It will be added to the garbage value.它将被添加到垃圾值中。 So,initialize the variable total with 0.因此,将变量 total 初始化为 0。

int total=0;

You need not to use type casting.您不需要使用类型转换。 Just declare the average variable as double.只需将平均变量声明为双精度。

double average=0;
average = total/5.0;

And you should print as printf("The average of 5 marks is %lf", average);并且您应该打印为printf("The average of 5 marks is %lf", average);

total is a local variable, hence it needs to be initialised. total是一个局部变量,因此需要对其进行初始化。 Had it been within file scope (like, a global variable), or a static variable, you might not have to initialise it to 0 .如果它在文件范围内(例如全局变量)或静态变量,您可能不必将其初始化为0

An initialisation could be simply like:初始化可以简单地像:

int avg[5], i, total = 0 ;

or或者

int avg[5], i, total;
total = 0 ;

Why do you need initialisation?为什么需要初始化? Because of this statement:因为这个声明:

 total = total + avg[i];

Here total is calculated using its previous value.这里的total是使用其先前的值计算的。 What is total 's previous value the first time this statement is encountered?第一次遇到此语句时total的先前值是多少? It could be anything, commonly referred to as garbage value, invoking undefined behaviour.它可以是任何调用未定义行为的东西,通常称为垃圾值。 Hence, you need initialisation to give this starting value to total .因此,您需要初始化以将此起始值赋予total Note, you don't need to initialise average , because its value does not depend on its previous contents.请注意,您不需要初始化average ,因为它的值不取决于其先前的内容。


Another problem is with the concepts of typecasting .另一个问题是typecasting的概念。 Here is the statement:这是声明:

average= (float)total/5;

You are right about typecasting total to float (you may also have done total/5.0 instead).您对将total类型转换为float是正确的(您也可以使用total/5.0代替)。 However, you are storing the result in an integer .但是,您将结果存储在integer This will result in a second typecasting, from the result in float to int .这将导致第二次类型转换,从float的结果到int

Hence, you need to declare average as a float .因此,您需要将average声明为float

( Note : If having a float result is not your requirement, and you really need an integral answer, you may ignore this part). 注意:如果您不需要float结果,并且您确实需要一个integral答案,则可以忽略此部分)。

You need to initialize variables before you use them in your code.在代码中使用变量之前,您需要对其进行初始化。

What goes WRONG in your code is,你的代码出了什么问题,

int avg[5],i,total;

where you have not initialized the int variable "total" Hence this code will use a garbage value您尚未初始化 int 变量“total”的地方因此此代码将使用垃圾值

for(i=0; i<5; ++i){
    total = total + avg[i];
}

You need this correction你需要这个更正

int avg[5], i, total=0;

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

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