Write a complete program to ask the user for a positive integer n, allocate spac
ID: 3771915 • Letter: W
Question
Write a complete program to ask the user for a positive integer n, allocate space on the heap for an array of integers that has n elements and then fill that array wish values using the function rand(). Pass the array to a function called find_min that will the index of the smallest element of your array. Your main program should print the index smallest element of the array on the screen. Remember to free up memory before the program. You can use additional space n the next page for your solution. The rand() returns a value between 0 and RAND-MAX and can be called to get random for each element of your dynamic array.Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int find_min(int a[],int num);
int main() {
int array[100],i,n;
int minimum=0;
printf(" Eneter the elements you want to be generated randomly in an array "):
scanf("%d",&n);
for (i= 0; i <n; i++) {
array[i]= rand() % 100 + 1;
}
printf("The numbers generated radomly are ");
for (i= 0; i <n; i++) {
printf("%d ",array[i]);
}
minimum=find_min(array[0],n);
return 0;
}
int find_min(int a[],int num) {
int j,temp=a[0];
for (j=1;j<num;j++)
if(temp>a[j])
temp= a[j];
return temp;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.