I would like to declare a variable for finding an average value in the following program as a pointer to a double type and create it so that the average value is stored directly in the calling variable as well as the minimum and maximum values.

Asked 2 years ago, Updated 2 years ago, 30 views

When you call a function and pass arguments, you pass the address of the variable that stores the average value.
Integer data are entered in the form of initializing an array in the main function.

inta[12]={45,57,79,60,5,38,67,55,96,33,10,73};

Current source code:

#include<stdio.h>

#define SIZE5

void min_max(inta[], intn, int*min, int*max){
    inti;
    
    *min=*max=a[0];
    for(i=1;i<n;i++){
        if(a[i]<*min)*min=a[i];
        else if(a[i]>*max)*max=a[i];
    }
}
int main(void){
    inta[SIZE] = {45,79,60,38,55};
    int min, max;
    
    min_max(a, SIZE, & min, & max);
    
    printf("%d\n", min);
    printf("%d\n", max);

    return 0;
}

multipost:
https://teratail.com/questions/313470

c

2022-09-30 15:34

1 Answers

Considering the content of the question title, isn't it possible that we can't move on because we're slightly confused about understanding the problem?

Declare the variables to find the mean for the following programs as pointers to double types

You need to declare the parameter of the function to find the mean as a pointer to the double type and the variable to store the mean as a double type variable.
That is written in the first line of the question (=Challenge?).(By the way, it's not written as double type, is that okay?)

When calling a function and passing arguments, pass the address of the variable that stores the average value.

Also, in the multi-posted answer, the function name and parameters are added to find the mean by modifying the function to find the minimum value and the maximum value, but isn't it okay to call it by adding an independent function to find only the mean value?
If possible, please check with the person who asked the question to see if that is the details of the actual assignment.

For the average calculation itself, you can code the contents equivalent to the following article.
How to find the mean (calculation formula) and meaning, disadvantages

The mean is the total 個 number.

Also, please note that if you compare the title of the question and what is written in the two lines with the current source code, it looks like a round throw of homework without your own consideration or trial and error, as explained in the comments and help on this site.
How do I ask a good question?
Should I answer my school homework

Also, regarding the following,

Integer data are entered in the form of initializing an array in the main function.

inta[12]={45,57,79,60,5,38,67,55,96,33,10,73};

First, we will replace the following parts of the main function.

inta[SIZE]={45,79,60,38,55};

Since the original number of data is 5 and the new number of data to replace is 12, one of the following additional actions is required:

  • Change 5 from #define SIZE5 to 12 and replace a[12] with a[SIZE] too
  • Change the SIZE of min_max(a, SIZE, & min, & max); to 12 and remove #define SIZE5
  • Change the SIZE of min_max(a,SIZE,&min,&max); to (sizeof(a)/sizeof(int)) Also change a[12] to a[] and delete #define SIZE5

The third method is recommended because there are few changes to the number of data, but it may be unnecessary depending on the class/course and course curriculum.


2022-09-30 15:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.