use c progamming not c++Function void STORE_EVEN_DATA (int n, int min, int max)
ID: 3776562 • Letter: U
Question
use c progamming not c++Function void STORE_EVEN_DATA (int n, int min, int max) opens a file “LAB.txt” and writes in it even random numbers which are within min and max. This function is allowed to attempt to generate random numbers only n times
. 2. “main” gets user inputs for how many attempts (n) function STORE_EVEN_DATA (..) will be allowed to make in order to generate even random numbers, and what are the lowest (int min) and highest (int max) limits for the random numbers to be generated. The “main” then calls STORE_EVEN_DATA (..) to print randomly generated even numbers in the file , “LAB.txt”.
3. Function int GET_DATA_in_ARRAY (int x[]) is designed to open the “LAB.txt” file, scan data from it and store them in an array of integers, and return the number of data read from the file.
4. Main calls GET_DATA_in_ARRAY ( ) function to scan data from the file and store them in its own array called, even_array[max_num]. Here max_num is a large global constant integer, e.g. 100.
5. Function float Min_Max(int x[], int num, int *min, int* max) is designed to calculate and return average value of integers in an array of size num. The function can also find smallest and largest numbers in the array and make them available to the main using pointers.
6. Main calls Min_Max(int x[], int num, int *min, int* max) and using the values provided by this function prints the minimum, maximum and average of numbers in the array on computer screen.
TYPICAL SCREEN OUTPUT IS SHOWN BELOW *********************
Tell me maximum attempts I get to generate random numbers: 50
Enter minimum and maximum limits-separated by blank: 3 79
Largest even number=76
Smallest even number=6
Average=44.8889
Press any key to continue . . .
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void STORE_EVEN_DATA(int n, int min, int max){
FILE *fp1;int i;
fp1 = fopen("LAB.txt", "w");
for(i=0;i<n;i++){
int k=0;
k=rand();
if(k%2==0 && min<k && max>k){
fprintf(fp1, "%d ",k);
}
else{
k=k+1;
if(k%2==0 && min<k && max>k){
fprintf(fp1, "%d ",k);
}
}
}
fclose(fp1);
}
int GET_DATA_in_ARRAY (int x[]){
FILE *fp2;
fp2 = fopen("LAB.txt", "r");
int i=0,k=0;
fscanf (fp2, "%d", &i);
while (!feof (fp2))
{
x[k]=i;k++;
fscanf (fp2, "%d", &i);
}
fclose (fp2);
return k;
}
float Min_Max(int x[], int num, int *min, int *max){
int counter,l_max,l_min;
l_min= l_max = x[0];
for(counter = 1; counter < num; counter++){
if(x[counter] > l_max)
l_max = x[counter];
else if(x[counter] < l_min)
l_min = x[counter];
}
*min=&l_min;
*max=&l_max;
printf("Tell");
}
int max_num=100;
int main(){
int n,min,max;
int *min1=0;
int *max1=0;
printf("Tell me maximum attempts I get to generate random numbers:");//taking the inputs as asked in question
scanf("%d",&n);
printf("Enter minimum and maximum limits-separated by blank:");
scanf("%d",&min);
scanf("%d",&max);
STORE_EVEN_DATA(n,min,max);//passing those values to function
int even_number[max_num];//the maximum number of generated would be the number of times we have randomly generated varaiable
GET_DATA_in_ARRAY(even_number);
Min_Max(even_number,max_num,&min,&max);
printf("Largest even number=%d",*max1);
printf("Smallest even number=%d",*min1);
printf("Average=%d",(*max1+*min1)/2);
printf("ress any key to continue . . .");
getch();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.