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

Write a C program to shuffle the elements of an array and check if there are dup

ID: 3564942 • Letter: W

Question

Write a C program to shuffle the elements of an array and check if there are duplicates in the array.

Declare an array of 50 integers and insert numbers from 0-48 in order into the first 49 positions.

(b) Read an integer from the user and insert into the last position in the array.

(c) Shuffle the array elements. You can do this by repeatedly picking two elements in the array and swapping them. You can pick the two indexes whose contents will be swapped randomly. Repeat the swap operation 25 times to get a good shuffle. Since we have 50 elements and we pick 2 at a time, doing the swap < 25 times guarantees that some elements will be in their original place after the shuffle.

(d) Check if the array has any duplicate elements in it and print the indexes and values of duplicate elements if there are any.

Write functions to shuffle the array and to check for duplicates.

Use scanf() and printf() only in main function

Explanation / Answer

// C Program to shuffle a given array

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

// A utility function to swap to integers

void swap (int *a, int *b)

{

    int temp = *a;

    *a = *b;

    *b = temp;

}

// A utility function to print an array

void printArray (int arr[], int n)

{

    for (int i = 0; i < n; i++)

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

    printf(" ");

}

// A function to generate a random permutation of arr[]

void randomize ( int arr[], int n )

{

    // Use a different seed value so that we don't get same

    // result each time we run this program

    srand ( time(NULL) );

    // Start from the last element and swap one by one. We don't

    // need to run for the first element that's why i > 0

    for (int i = n-1; i > 0; i--)

    {

        // Pick a random index from 0 to i

        int j = rand() % (i+1);

        // Swap arr[i] with the element at random index

        swap(&arr[i], &arr[j]);

    }

}

// Driver program to test above function.

int main()

{

    int n;

    scanf("%d",&n) ;

    int arr[n];

    for(int i=0;i<n;i++){

        scanf("%d",&a[i]) ;

    randomize (arr, n);

    printArray(arr, n);

    for(int i=0;i<n;i++) {

           for(int j=n-1;j>i;j--) {

               if(arr[j-1]>arr[j]) {

                      temp=arr[j];

                      arr[j]= arr[j-1]; arr[j-1]=temp; } } }

        //checking duplicate values

       printf("Duplicate values: ");

       int num=arr[0];

       for(int i=1;i<n;i++) {

              if(num==arr)

                { printf("%d ",arr);

              }

              else

                   num=arr;

           }

    return 0;

}

Hope you understand. Feel free to ask any doubts. Have a nice day :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote