/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void GenerateFromArray(void); /************************************************************************************/ /************************************************************************************/ int main(void) { /* This is the main() program. It should call the functions ScanArray() and GenerateFromArray() */ GenerateFromArray(); system("pause"); return 0; } /*************************************************************************************** Define this function to scan the elements of an array from the given data file "ArrayInp.dat". If the input file is not found, or contains invalid data, the function should return a 0 and print an error message. If the input file is read successfully, the function should return a 1. Utilize the address operator & to read in the values to an array, for example scanf("%d", &myArray[i]) reads into the ith elemet of myArray. /***************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize) { return 0; } /*************************************************************************************** This function generates a random number in the range [0, RAND_MAX]; Modify this function to generate a random number in the range [0, nSize], where nSize is the size of your array. Then use it to choose mChoose elements from the array. /***************************************************************************************/ //void GenerateFromArray(int myArray[], int nSize) { void GenerateFromArray(void) { int ra, ia; /* Seed the random-number generator with current time so that the numbers will be different every time we run it. */ srand((unsigned int)time(NULL)); /* Call the random number generator */ for (ia = 0; ia<10; ia++) { ra = rand(); fprintf(stdout, "ra = %d\n", ra); } }
Q) Write a program in C to generate m random integers from an array of size n, such that the probability of selecting even elements is twice that of odd elements. The same element may be selected multiple times. As an example, let n = 100 and m = 30. Read in the array elements from the provided data file ArrayInp.dat, using the fscanf() function. Utilize the function rand() to generate a pseudo random number and the function srand() to seed the random number generator. Fill in the above program please.
Where ArrayInp.dat is just random numbers
I am assuming even element means element at the even position and odd element means element at an odd position.
we will take %3 if and is even that is 0 or 2 then even element else answer is an odd element.and inside the loop, we will use rand again to get the position of an element by taking modulo by n.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, n,m;
time_t t;
FILE *fp;
fp = fopen("ArrayInp.dat", "r");
srand((unsigned) time(&t));
fscanf(fp, "%s",n );
fscanf(fp, "%s",m);
int ans[m];
int arr[n];
fgets(arr, n, (FILE*)fp);
for( i = 0 ; i < m ; i++ )
{
int x=rand()%3;
if(x==0||x==2){
x=rand()%n;
if(x%2==0){
ans[i]=arr[x];
}
else{
ans[i]=arr[x-1];
}
}
else{
x=rand()%n;
if(x%2==1){
ans[i]=arr[x];
}
else{
ans[i]=arr[1];
}
}
}
fclose(fp);
return(0);
}
Get Answers For Free
Most questions answered within 1 hours.