Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use a For Loop to generate 100 random numbers. Determine the most current maximu

ID: 3589850 • Letter: U

Question

Use a For Loop to generate 100 random numbers. Determine the most current maximum and minimum number as the random numbers are being generated. This is sometimes referred to as a "running" maximum and minimum. Display the running maximum and minimum values as well as the current random num- ber on the front panel. P5.2 Be sure to include the Time Delay Express VI so the user is able to watch the values update as the For Loop executes. Figure P5.2 shows the Time Delay Express VI configuration dialog box (accessed by double-clicking on the VI) with a recommended delay of 0.1 second. Double-click on Express Vi Set Time delay D Configure Timp Oelay (Time Delay] 23 |. | Time delay(seconds) 0.100 Tirne Delay OK Cancel Help FIGURE P5.2 Time Delay Express VI with configuration dialog box showing 0.1 second delay Place the Time Delay Express VI within the For Loop.

Explanation / Answer

/* Purpose : To find the minimum and maximum of 100 random numbers generated.*/

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int minimum; //variable to store the maximum of the generated random numbers.
int maximum; //variable to store the minimum of the generated random numbers.
int number; // variable to store every number generated by the rand function.
int i; //variable to keep iteration count.
int initNum; //variable to store the first number generated by the rand function.
initNum = rand(); //generate the first random number.
printf(" Number 1 = %d", initNum); // Print the first random number generated.
minimum = initNum; // initNum is assumed to be both the minimum and maximum to begin with.
maximum = initNum; // initNum is assumed to be both the minimum and maximum to begin with.
for(i = 2;i<=100;i++) { //starting the iteration from 2 as already 1 number is read.
number = rand(); //generation of random numbers using rand function.
sleep(1); // delays the generation by 1 second for user to observe
printf(" Number %d = %d",i, number); // prints the numbers generated by the rand function
  
if(maximum < number) //logic to find max, checks with the next number to see if
maximum = number; //current max is less than the next generated number, if so swap
if(minimum > number) //logic to find min, checks with the next number to see if
minimum = number; //current min is more than the next generated number, if so swap
}
printf(" Minimum = %d", minimum); // print the minimum
printf(" Maximum = %d", maximum); // print the maximum
return 0;
}