The program should prompt the user to enter the number of digits to generate, or
ID: 3803194 • Letter: T
Question
The program should prompt the user to enter the number of digits to generate, or 0to exit the program. The program should randomly generate the specified number of digits. After each digit is randomly generated, the program should display the count of the digits generated and the new random digit. Next, the program should construct a string consisting of the digits generated so far in non-decreasing order separated by spaces, and then output the string. Be sure to test your program thoroughly. Here is an example run of the program.Please enter the number of digits to generate (or 0 to exit): 4 Count: 1. Random digit: 3. Digits so far in non-decreasing order: 3. Count: 2. Random digit: 1. Digits so far in non-decreasing order: 1 3. Count: 3. Random digit: 6. Digits so far in non-decreasing order: 1 3 6. Count: 4. Random digit: 3. Digits so far in non-decreasing order: 1 3 3 6.
Please enter the number of digits to generate (or 0 to exit): 2 Count: 1. Random digit: 9. Digits so far in non-decreasing order: 9. Count: 2. Random digit: 5. Digits so far in non-decreasing order: 5 9. Please enter the number of digits to generate (or 0 to exit): 0 Have a nice day! The program should prompt the user to enter the number of digits to generate, or 0to exit the program. The program should randomly generate the specified number of digits. After each digit is randomly generated, the program should display the count of the digits generated and the new random digit. Next, the program should construct a string consisting of the digits generated so far in non-decreasing order separated by spaces, and then output the string. Be sure to test your program thoroughly. Here is an example run of the program.
Please enter the number of digits to generate (or 0 to exit): 4 Count: 1. Random digit: 3. Digits so far in non-decreasing order: 3. Count: 2. Random digit: 1. Digits so far in non-decreasing order: 1 3. Count: 3. Random digit: 6. Digits so far in non-decreasing order: 1 3 6. Count: 4. Random digit: 3. Digits so far in non-decreasing order: 1 3 3 6.
Please enter the number of digits to generate (or 0 to exit): 2 Count: 1. Random digit: 9. Digits so far in non-decreasing order: 9. Count: 2. Random digit: 5. Digits so far in non-decreasing order: 5 9. Please enter the number of digits to generate (or 0 to exit): 0 Have a nice day! The program should prompt the user to enter the number of digits to generate, or 0to exit the program. The program should randomly generate the specified number of digits. After each digit is randomly generated, the program should display the count of the digits generated and the new random digit. Next, the program should construct a string consisting of the digits generated so far in non-decreasing order separated by spaces, and then output the string. Be sure to test your program thoroughly. Here is an example run of the program.
Please enter the number of digits to generate (or 0 to exit): 4 Count: 1. Random digit: 3. Digits so far in non-decreasing order: 3. Count: 2. Random digit: 1. Digits so far in non-decreasing order: 1 3. Count: 3. Random digit: 6. Digits so far in non-decreasing order: 1 3 6. Count: 4. Random digit: 3. Digits so far in non-decreasing order: 1 3 3 6.
Please enter the number of digits to generate (or 0 to exit): 2 Count: 1. Random digit: 9. Digits so far in non-decreasing order: 9. Count: 2. Random digit: 5. Digits so far in non-decreasing order: 5 9. Please enter the number of digits to generate (or 0 to exit): 0 Have a nice day!
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
void nonDecreasingOrder(int number[], int n) {
int i,j,a;
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
char s ;
const char *decOrder;
for(i=0; i<n; i++) {
int a = number[i];
s = a + '0';
printf("%c ",s);
}
}
int main()
{
printf("Please enter the number of digits to generate (or 0 to exit): " );
int choice,i,val;
scanf("%d",&choice);
if(choice != 0) {
int array[choice];
for(i=1; i<=choice; i++) {
val = rand()%10;
array[i-1]=val;
printf("Count: %d. Random digit: %d. Digits so far in non-decreasing order: ",i,val);
nonDecreasingOrder(array,i);
printf(". ");
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.