class=”markdown_views prism-atom-one-dark”>
Input a set of data and store it in the array, and dynamically complete the number of data elements by the input function. Find the maximum value and minimum value of the input data, and return the result through the function parameter;
Problem Analysis: Input a set of data and store it in an array, and the dynamic input of data indicates the need for dynamic space allocation (the use of pointers, the use of malloc, and the use of free functions), and to find the maximum and minimum values, you may need to use for loops and if statements judge;
//Enter a set of data and put it in the array, the number of data elements is dynamically completed by the input function,
//Find the maximum value and minimum value of the input data, and return the result through the function parameter
#include
#include
int* DyArray(int **pDyArray,int n)
{
*pDyArray=(int*)malloc(n*sizeof(int));
return *pDyArray;
}
void InputDyArray(int *pDyArray,int n )
{
int i;
for(i=0;in;i++)
{
printf("please input number %d: ",i);
scanf("%d",&pDyArray[i]);
}
return;
}
void MaxMin(int *pDyArray,int n ,int *Max,int *Min)
{
int i;
*Max=*pDyArray;
*Min=*pDyArray;
for(i=1;in;i++)
{
if(*MaxpDyArray[i])
{
*Max=pDyArray[i];
}
if(*Min>pDyArray[i])
{
*Min=pDyArray[i];
}
}
return;
}
void Output(int *pDyArray,int n ,int Max,int Min)
{
printf("max=%d ",Max);
printf("min=%d ",Min);
return;
}
int* Destroy(int *pDyArray)
{
if(pDyArray)
{
free(pDyArray);
}
return NULL;
}
int main()
{
int i,n,min,max,*pDyArray;
printf("please input n: ");
scanf("%d",&n);; span>
DyArray(&pDyArray,n);
InputDyArray(pDyArray,n);
MaxMin(pDyArray,n,&max,&min);
Output(pDyArray,n,max,min); span>
Destroy(pDyArray);
return 0;
}
>&n);
DyArray(&pDyArray,n);
InputDyArray(pDyArray,n);
MaxMin(pDyArray,n,&max,&min);
Output(pDyArray,n,max,min); span>
Destroy(pDyArray);
return 0;
}