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

Please use C programming Sample Execution Problem 2: Shuffle the list of ints en

ID: 3701187 • Letter: P

Question

Please use C programming

Sample Execution

Problem 2: Shuffle the list of ints entered by the user. Your program should Ask the user to specify the number of element swaps swapCount that will be executed to randomize the list of ints. Randomly swaps (the contents of) two nodes of the list swapCount times a) b) To obtain a random number in [O, count] (count is the length of the list) use the following function #include #include #include«nath. h> #include int rand_gen(int count) double frac; 1 frac(double)rand )/((double)RAND_MAX+1); return floor(count frac); //random number in [0, count] Include the following in the main to randomize the seed for the random function. srand((int)time (NULL)): /1 need only be applied once srand(int) randomizes the seed of the random number generator. A different seed will produce a different sequence of random numbers time (NULL)obtains the current system time. Hence it changes at every execution. rand )generates a random number in the range 0 and RAND_MAX. RAND_MAX is a system-defined upper bound on the maximum number that can be produced by rand () . · To swap the contents of two elements of the list write a function with this prototype void swap(node *pt, int i, int j); pt is a pointer to the head of the list i: the ith list element j: the jth list element

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
struct Node
{
   int data;
   struct Node *next;
}typedef node;
int rand_gen(int count)//to generate randomnumber in the list size
{
   double frac;
   frac = (double)rand()/((double)RAND_MAX+1);
   return floor(count*frac);
  
}
void swap(node *pt, int i,int j)//swapping values
{
   node *temp1 = pt,*temp2 = pt;
   while(i>0)
   {
       temp1=temp1->next;//locating ith node
       i--;  
   }
   while(j>0)
   {
       temp2=temp2->next;//locating jth node
       j--;  
   }
  
   int k = temp1->data;
   temp1->data = temp2->data;//swapping
   temp2->data = k;
  
}
//method to print list
void print_list(node *p,int n)
{
   while(n>0)
   {
       printf("%d ",p->data);
       p=p->next;
       n--;  
   }
   printf(" ");
}
//testing method...
int main()
{
   node *p=NULL,*t;
   char c;
   int n;
   int count=0;
   while(1)
   {
       printf("Enter an integer:");
       scanf("%d",&n);  
       count++;
       scanf("%c",&c);
       printf("Do you want to continue(y?n)");
       scanf("%c",&c);
       if(p==NULL)
       {
       t = (node*)malloc(sizeof(node));
       t->data = n;
       t->next=NULL;
       p=t;  
       }
       else
       {
           t->next = (node*)malloc(sizeof(node));
           t->next->data = n;
           t->next->next=NULL;
           t=t->next;  
       }
       if(c=='n')break;  
   }
   print_list(p,count);
   printf("Enter the number of swaps:");
   scanf("%d",&n);
   int i,j;
   srand((int)time(NULL));
   while(n>0)///randomizing list by swapss.
   {
       i = rand_gen(count);
       j = rand_gen(count);
       swap(p,i,j);
       n--;
   }
  
   print_list(p,count);
}

output:

Enter an integer:1
Do you want to continue(y?n)y
Enter an integer:2
Do you want to continue(y?n)y
Enter an integer:3
Do you want to continue(y?n)y
Enter an integer:4
Do you want to continue(y?n)y
Enter an integer:5
Do you want to continue(y?n)y
Enter an integer:6
Do you want to continue(y?n)n
1 2 3 4 5 6
Enter the number of swaps:1000
1 3 2 5 4 6


Process exited with return value 10
Press any key to continue . . .