In: Computer Science
We often need to shuffle data in many applications. Consider a case of a game that plays cards for example or a system that does a draw for lottery. The purpose of this lab is to write functions that will help us shuffle elements of an array.
Create a function that takes an array of integers and its size as parameters and populates the array with the values: 0, 1, 2, ..., size-1, where size is the size of the array.
Create a function that takes an array of integers and two indices (integers) and swaps the two elements. For example if I pass the array and 3 and 7 then the array will swap the element at index 3 with the element at index 7.
Create a function that takes an array of integers and its size as parameters and shuffles the array. You can accomplish this by going through the elements and exchanging each one with a randomly selected element (hint: use the method you created in step 2.
Create a function that takes an array of integers, its size and a value as parameters and returns the index at which the value is stored. If the array doesn't contain the value then the function returns -1. For example if the array is 1 4 6 2 10 11 12 and the value is 11 then the function returns 5 because 11 is stored at index 5. If the value is 20 then the function returns -1 because 20 is not in the array.
Create a function that takes an array of integers and its size as parameters and prints the contents of the array.
In main write code to test your function. Namely, creates an array of 15 integers, populate it, print it, shuffle it, print it again and print the index of an element in the array and the index of an element not in array .
Here is sample output:
Elements before shuffle: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Elements after shuffle: 9 8 10 6 5 12 14 2 7 3 4 1 13 11 0 What value are you searching for? 7 Element 7 is stored at index 8 What value are you searching for? 30 Element 30 is stored at index -1
Answer:
Program code to copy:
Sample Output:
Program code to to copy:
//Header files
#include<stdio.h>
//print() function takes an array of integers and
// two indices (integers) and swaps the two elements
void print(int *arr, int size)
{
int i;
for (i = 0; i < size; i++)
{
printf(" %d ", arr[i]);
}
printf("\n");
}
//swap() function takes an array of integers and
//two indices (integers) and swaps the two elements
void swap(int *arr, int index1, int index2)
{
int temp;
temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
//shuffle() function takes an array of integers
//and its size as parameters and shuffles the array
void shuffle(int *arr, int size)
{
int i;
int random;
for (i = 0; i < size; i++)
{
//get a random number
random = rand()%size;
//suffle each with a random number
swap(arr, i, random);
}
}
//getIndex() function takes an array of integers,
//its size and a value as parameters and returns the
//index at which the value is stored
int getIndex(int arr[], int size, int value)
{
int i = -1;
for (i = 0; i < size; i++)
{
if (arr[i] == value)
{
return i;
}
}
return i;
}
//populateArray() function takes that takes an array of
//integers and its size as parameters and populates
//the array with the values: 0, 1, 2, ..., size-1
void populateArray(int *arr, int size)
{
int i = 0;
for(i = 0; i < size; i++)
{
arr[i] = i;
}
}
//main function
int main()
{
srand(time(0));
//array declared
int arrEle[15];
int size = 15;
int index, value;
printf("Fill the array... \n");
//call populateArray() function to fill the
//array
populateArray(arrEle, size);
//call print() function to print the array elements
printf("\nPrinting array elements before shuffling: \n");
print(arrEle, size);
//call shuffle() function to shuffle the elements in the
//array
printf("\nShuffling the entire array with random positions... \n");
shuffle(arrEle, size);
//call print() function to print the array elements
printf("\nPrinting array elements after shuffling: \n");
print(arrEle, size);
//prompt the user to enter the search element
printf("\nWhat value are you searching for? ");
scanf("%d", &value);
//call getIndex() function to search the value in the function
index = getIndex(arrEle, size, value);
printf("\nElement %d is stored at index = %d \n", value, index);
//prompt the user to enter the search element
printf("\nWhat value are you searching for? ");
scanf("%d", &value);
//call getIndex() function to search the value in the function
index = getIndex(arrEle, size, value);
printf("\nElement %d is stored at index = %d \n", value, index);
return 0;
}