In: Computer Science
how would i change the for loops to while loops in the code below
#include<stdio.h>
#include<stdlib.h>
int main()
{
int seed;
// Taking seed value as input from the user
printf("Enter a seed value (0 to quit): \n");
scanf("%d", &seed);
// Running the loop until user enters 0 to quit
// count array will count frequency of 0 , 1 , 2 ,3
int count[4];
for (int i = 0; i < 4; i++) count[i] = 0;
while (seed != 0)
{
// Passing seed value to the function srand()
srand(seed);
// Printing 5 random values
for (int i = 0; i < 5; i++) {
// generae a random number
// and take it modulo with 4
int rand_num = rand() % 4;
printf("%d ", rand_num);
count[rand_num]++;
}
printf("\n");
// Taking next seed value as input from the user
printf("Enter a seed value (0 to quit): \n");
scanf("%d", &seed);
}
// print count of each element [0-3]
for (int i = 0; i < 4; i++) {
printf("Count of %d = %d\n", i , count[i]);
}
return 0;
}
#include<stdio.h> #include<stdlib.h> int main() { int seed; // Taking seed value as input from the user printf("Enter a seed value (0 to quit): \n"); scanf("%d", &seed); // Running the loop until user enters 0 to quit // count array will count frequency of 0 , 1 , 2 ,3 int count[4]; int i = 0; while (i < 4) { count[i] = 0; i++; } while (seed != 0) { // Passing seed value to the function srand() srand(seed); // Printing 5 random values i = 0; while ( i < 5 ) { // generae a random number // and take it modulo with 4 int rand_num = rand() % 4; printf("%d ", rand_num); count[rand_num]++; i++; } printf("\n"); // Taking next seed value as input from the user printf("Enter a seed value (0 to quit): \n"); scanf("%d", &seed); } // print count of each element [0-3] i = 0; while (i < 4) { printf("Count of %d = %d\n", i , count[i]); i++; } return 0; }