Question

/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void Ge

/*************************************************************************************
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

Homework Answers

Answer #1

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);

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
create case 4 #include <stdio.h> int main(void) { int counter; int choice; FILE *fp; char item[100];...
create case 4 #include <stdio.h> int main(void) { int counter; int choice; FILE *fp; char item[100]; while(1) { printf("Welcome to my shopping list\n\n"); printf("Main Menu:\n"); printf("1. Add to list\n"); printf("2. Print List\n"); printf("3. Delete List\n"); printf("4. Remove an item from the List\n"); printf("5. Exit\n\n"); scanf("%i", &choice); switch(choice) { case 1: //add to list //get the input from the user printf("Enter item: "); scanf("%s", item); //open the file fp = fopen("list.txt","a"); //write to the file fprintf(fp, "\n%s", item); //close the file...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Write a function that passes an array argument, getRandomNumbers, to get a pointer to an array...
Write a function that passes an array argument, getRandomNumbers, to get a pointer to an array of random numbers in the array. The function dynamically allocates an array, uses the system clock to seed the random number generator, populates the array with random values, and then returns a pointer to the array. Function getRandomNumbers to generate a random array and return a pointer. int* getRandomNumbers(int num); // The parameter indicates the number of numbers requested. The algorithm can be described...
Your C program will do the following : Must use at least 2 function prototypes &...
Your C program will do the following : Must use at least 2 function prototypes & definitions . You can also use repetitions , control structures . You re not allowed any type of global arrays, or global variables. You are only allowed to use 2 dimensional arrays. 1. In your main program, create a array of size 7 X 7. 2. Create a function that accepts the empty array. The function will initiate the to zero. Then, the function...
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i];...
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i]; } return (double)sum/n; } Rewrite the function to eliminate the array subscripting (a[i]), using pointer arithmatic instead. Write a program that reads a line of input and checks if it is a palindrome (ignoring spaces) Write a program that takes the name of a file as a command line argument, and prints the contents of the file (similar to the linux 'cat' program). Write...
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N...
Given the following function prototypes: float asciiAvg(char mystring[], int length); Write the function definition. The asciiAvg...
Given the following function prototypes: float asciiAvg(char mystring[], int length); Write the function definition. The asciiAvg function should return the average of the ascii values for the characters in the array mystring of length characters.
C Programming 1. Write a void function that takes in a 2D array of character and...
C Programming 1. Write a void function that takes in a 2D array of character and have it print out each array on a new numbered line on the console. 2. Illustrate the stack and the heap allocation. Specify what each variable value holds and where different references are pointing to. int main() { int myarray[3]; myfunction (myarray); } int myfunction(int* in) { int i; for (i = 0; i<3; i+= 1) { in[i] = i; } // illustrate the...
Question Recursive Maximum: In this question you must develop and use a recurive function to find...
Question Recursive Maximum: In this question you must develop and use a recurive function to find the maximum value in array of integers. Complete the C program, Maximum.c, by implementing the recursive function maximumValue, and the regular function max, and completing the main function using the comments provided. Open the file Maximum.c, complete, compile and run it. When you are sure it is correct, include the c file in your final submission. Note that the function max is not recursive....
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file named data into an...