When a variable declaration is made, a numerical value is substituted from the beginning.

Asked 1 years ago, Updated 1 years ago, 391 views

I'm a beginner in programming.

First, we set up a program in c language that outputs the maximum, minimum, average, most frequent, and total values by entering the number of elements one by one, but after hearing the number of elements, we declared com, which compares the number of times.There are no particular code errors.

The execution environment is here.
https://play.google.com/store/apps/details?id=app.compiler

"It says ""scanf cannot be used"", but I can use it properly just because the method is complicated."

The following code

#include<stdio.h>
int maxmin (void);

int main (void)
{
    maxmin();
    return 0;
}
int maxmin (void)
{
    inta, i, l;
    double he, max = 0, momax = 0, min, mo;
    
    scanf("%d", & a);
     intcom[a];
    double suu[a], mode[a], modee[a];

    for(i=0;i<a;i++){
        scanf("%lf", & suu[i]);
          
          for(l=0;l<=i+1;l++){
            if(suu[i]==mode[l]){
                com[l]+=1;
                break;
            } else {
                if(mode[l]==0){
                    mode[l] = suu[i];
                    com[l]+=1;
                    break;
                }
            }
          }
          
         if(i==0){
            min = suu[i];
         }
        printf("%dth number: %7.3f\n", i+1, suu[i]);
        if(suu[i]>max){
            max = suu[i];
        } else {
            if(suu[i]<min){
                min = suu[i];
            }
        }
         he+=suu[i];
    }
     
     for(i=0;i<a;i++){
        if(com[i]>momax){
            for(l=0;l<a;l++){
                modee[l] = 0;
                if(modee[l]==0){
                    break;
                }
            }
            momax=com[i];
            modee[0] = mode[i];
        } else {
            if(com[i]==momax){
                for(l=0;l<a;l++){
                    if(modee[l]==0){
                        modee[l] = mode[i];
                        break;
                    }
                }
            }
        }
     }
     printf("%7.3f", modee[2]);
     printf("most frequent value");
     for(i=0;i<a;i++){
        printf("%7.3f", modee[i]);
        if(modee[i+1]!=0){
            printf(", ");
        } else {
            printf("\n");
            break;
        }
     }

    printf("maximum value %7.3f\nminimum value %7.3f\naverage value %7.3f\ntotal value %7.3f\n", max, min, he/a, he);
    return 0;
}

c

2023-02-14 18:20

2 Answers

"The first element is ""399423024"" and an unusually large number has been substituted even though it has not been substituted yet

"

Declarations alone have an indefinite value, so
Enter the initial value in the for statement (for example).


2023-02-14 18:24

The type of variable called 自動automatic variable 」 in jargon, such as intcom[a]; in this example, has an indefinite value without initialization.In other words, 0 is not always included, and 399423024 is perfectly normal.

If you want the value 0, you must substitute 0.


2023-02-14 19:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.