动态数组
1.用动态数组编程实现从键盘输入N个整数,求最大值和最小值,并返回它们在数组中的位置。输入:(1)N; (2)N个整数。输出:max=?.pos1=?(换行)min=?, pos2= ?#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int * d , i , min , max , n ;
scanf("%d" , & n) ;
if(d = (int *) malloc(sizeof(int) * n)) {
for(i = 0 ; i < n ; i ++) scanf("%d" , & d) ;
for(max = min = 0 , i = 1 ; i < n ; i ++) {
if(d > d) max = i ;
else if(d < d) min = i ;
}
printf("max = %d , pos = %d\n" , d , max) ;
printf("min = %d , pos = %d\n" , d , min) ;
free(d) ;
} else {
fprintf(stderr , "Failure of memory allocate .\n") ;
}
}
页:
[1]