In: Computer Science
In first year programming C language, using math.h, traditional stdio.h, bool.h, and mainly functions - taking approx. 85 lines of code:
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
=====================================================
#include
//Function prototypes
void initializeArray(int arrayElements[], int sizeOfArray);
void swapElement(int arrayElements[], int index1, int
index2);
void shuffleArray(int arrayElements[], int sizeOfArray);
int searchValue(int arrayElements[], int sizeOfArray, int
value);
int main() {
int arrayElements[15], sizeOfArray = 15, i,
searchElement, index;
//Initialize array elements
initializeArray(arrayElements, sizeOfArray);
printf("Array elements before shuffle:\n");
for (i = 0; i < sizeOfArray; i++)
{
printf("%d ",
arrayElements[i]);
}
//Shuffle array
shuffleArray(arrayElements, sizeOfArray);
printf("\n\nArray elements after shuffle:\n");
for (i = 0; i < sizeOfArray; i++) {
printf("%d ",
arrayElements[i]);
}
printf("\n\nEnter search element : ");
scanf("%d", &searchElement);
index = searchValue(arrayElements, sizeOfArray,
searchElement);
printf("Element %d is stored at index %d \n",
searchElement, index);
return 0;
}
void initializeArray(int arrayElements[], int sizeOfArray) {
int i;
for (i = 0; i < sizeOfArray; i++) {
arrayElements[i] = i;
}
}
void swapElement(int arrayElements[], int index1, int index2)
{
int temp;
//Swap logic
temp = arrayElements[index1];
arrayElements[index1] = arrayElements[index2];
arrayElements[index2] = temp;
}
void shuffleArray(int arrayElements[], int sizeOfArray) {
int i;
for (i = 0; i < sizeOfArray / 2; i++) {
swapElement(arrayElements, i,
sizeOfArray - i);
}
arrayElements[0]=0;
}
int searchValue(int arrayElements[], int sizeOfArray, int value)
{
int i, index = -1;
for (i = 0; i < sizeOfArray; i++) {
if (arrayElements[i] == value)
{
index =
i;
break;
}
}
return index;
}