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
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...
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.
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...
Given the following function in C++: int main(void) { int a = 2; int b =...
Given the following function in C++: int main(void) { int a = 2; int b = myFunction(a); a = b + 1; b = myFunction(a); cout << ”b = ” << b << endl; return 0; } int z = myFunction(int x) { static int n = 0; n = n + 1; int z = x + n; return z; } What is printed by the cout on the screen?
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int...
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int array[] = { 1, 1, 1, 3 }; unsigned long long r = sum(array, sizeof(array) / sizeof(*array)); printf("Result is: %llu (%s)\n", r, r == 6 ? "correct" : "incorrect"); return 0; } Complete the code for the line that calls the sum function. Write the sum function.
For the following code in C, I want a function that can find "america" from the...
For the following code in C, I want a function that can find "america" from the char array, and print "america is on the list" else "america is not on the list" (Is case sensitive). I also want a function to free the memory at the end of the program. #include <stdio.h> #include <stdlib.h> struct Node { void *data; struct Node *next; }; struct List { struct Node *head; }; static inline void initialize(struct List *list) { list->head = 0;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • The supergopher is a device invented to drill through arctic pack ice, which uses heated water...
    asked 22 minutes ago
  • What ethical theory or framework would you apply to our relation to the environment? Explain why...
    asked 43 minutes ago
  • A 4.3 kg particle is projected with an initial speed of 3.7 m/s along a surface...
    asked 2 hours ago
  • /************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void Ge
    asked 2 hours ago
  • Problem: Our Armstrong number Please write code for C language So far we have worked on...
    asked 2 hours ago
  • In space, a 4.0kg metal ball moving 30m/s has a head-on collision with a stationary 1.0kg...
    asked 2 hours ago
  • When a governing body like a state or the national legislature passes a law, it’s forever...
    asked 3 hours ago
  • In classical conditioning, the tendency for a conditioned response to be elicited by stimuli that are...
    asked 3 hours ago
  • A 2.2 KVA, 440---220 V, 60 Hz, step-down transformer has the following parameters: Rp= 3.0 Ω,...
    asked 3 hours ago
  • how can you view a list of users currently signed in to the computer
    asked 4 hours ago
  • A man stands on the roof of a building of height 14.4 m and throws a...
    asked 4 hours ago
  • Cited sources such as the economist, WSJ, NYT..... Differentiate between purchasing power parity and interest rate...
    asked 5 hours ago