Write a C program that finds the largest and second largest elements in an array. Use the random number generator rand() to initialize the array with 20 numbers with values in the range of 1 to 100. Create a function for finding the maximum and second maximum values using pointers to pass the array into the function, and as function arguments.
The prototype of the function should be:
void max_max2(int a[],int n,int *max1,int *max2);
/* *max1 points to the maximum value, and *max2 points to the second maximum. */
A call of max1_max2 should have the folowing apperance:
max1_max2(b,N,&big1,&big2);
this is my code without the function, it works but i need help to create the functions.
#include <stdio.h>
#include <conio.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>
int * getRandom( ) {
static int a[20];
int i;
srand( (unsigned)time( NULL ) );
for ( i = 0; i < 20; ++i)
{
a[i] = rand() % 100;
printf("%d\n", a[i] );
}
return a;
}
int main(){
int max, secondMax;
int *p;
int i;
p = getRandom();
max = secondMax = INT_MIN;
for(i = 0; i < 20; i++){
if(p[i] > max){
secondMax = max;
max = p[i];
} else if (p[i] > secondMax
&& p[i] < max){
secondMax = p[i];
}
}
printf("Maximum Element : %d \nSecond Maximum Element: %d", max, secondMax);
getch();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire