View the step-by-step solution to:

Exercise 4: Use template program lab11d. This code has a number of problems, and it is not clean, easy to work with code. Here's what it is supposed...

● Exercise 4: Use template program lab11d.c. This code has a number of problems, and it is not clean, easy ­to ­work ­with code. Here's what it is supposed to do: ○ It should generate REP lines of output. ○ Each line of output should contain SIZE_OF_SAMPLES"random" numbers, and the numbers in that line should all be unique, i.e., there should be no duplicate values in any one line of output. ○ Each random number should be in the range of 0 to MAX(inclusive). Note that the random number generator is given a constant seed via the call to srand(9808), so each time the program runs, the same sequence of "random" numbers will be generated. With REP having value 5, SIZE_OF_SAMPLES having value 3, and MAX having value 6, all as given to you in the program, the final output should be:

>>> Running program

6 5 1

0 2 6

2 4 3

2 0 3

6 5 0

>>> Program exited with exit code 0

When you initially run this program, it seems to run forever, with no output. There is more than one bug in this program.

○ Identify and fix these bugs.

○ Complete the Exercise 4 questions on the lab report form. 

The code given in lab11d.c is :

/* 

 * This program takes a sample of the specified size (SIZE_OF_SAMPLES)

 * from integers between 0 and a MAX value (ends inclusive) without

 * replacement. Contains bugs to practice debugging.

 */


#include <stdio.h>

#include <stdlib.h>


#define MAX       6

#define SIZE_OF_SAMPLES 3

#define REP       5


int MemberQ(int *array, int arrSize, int x);

void UniqRandInts(int max, int howmany, int * resultPtr);


int main(void) {

 int arr[SIZE_OF_SAMPLES], i, j, max, size;

  

 srand(9808);

 max = MAX;

 size = SIZE_OF_SAMPLES;


 for (i = 0; i < REP; i++) {

  UniqRandInts(max, size, arr);

  for(j = 0; j < size; j++) {

   printf("%d ", arr[i]);

  }

  printf("n");

 }

 return 0;

}


/*

 * resultPtr[] will be initialized with a sample of the specified size

 * (n) from integers between 0 and a max value (ends inclusive)

 * without replacement.

 */

void UniqRandInts(int max, int n, int *resultPtr) {

 int cntr = 0, r;

  

 while(cntr < n) {

  r = rand();

  r = r % (max + 1);

  if (MemberQ(resultPtr, cntr, r)) {

   resultPtr[cntr] = r;

   cntr++;

  }

 }

 return;

}


/*

 * Check if an integer x is included in array[] of arrSize.

 * Returns: 1 if x is a member

 *     0 if x is not a member

 */

int MemberQ(int *array, int arrSize, int x) {

 int i;


 for (i = 0; i < arrSize; i++) {

  if (array[i] == x) {

   return 1;

  }

 }

 return 0;

}

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question