Question

In: Computer Science

We often need to shuffle data in many applications. Consider a case of a game that...

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

Solutions

Expert Solution

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;

}


Related Solutions

Consider the Ultimatum Game, a two-player game often played in experimental economics labs. In the Ultimatum...
Consider the Ultimatum Game, a two-player game often played in experimental economics labs. In the Ultimatum Game, one player is given an amount of money and then instructed to give some arbitrary portion of it to an anonymous second player. The second player has the option of accepting the offer or rejecting it. If the second player rejects the offer, neither player gets anything. Now answer the following question if the First Player is given $100: (a) According to traditional...
There are many practical applications of biological knowledge that we mostly accept and take for granted....
There are many practical applications of biological knowledge that we mostly accept and take for granted. Many (though not all) modern techniques in medicine, agriculture and sanitation are examples of widely accepted practices. However, our knowledge about biology has enabled us to do things that raise ethical and social issues. Cloning is one example of such a technology, but there are many others. Choose one such example, then: Describe the technology. Describe the ethical and social issues that are raised...
“We know that how we present data often can affect the audience’s conclusions about the data....
“We know that how we present data often can affect the audience’s conclusions about the data. In a recent article from Travel Weekly on the Department of Transportation’s on-time stats for airline arrivals, the argument is made that the airline industry pads its own statistics in order reap cost benefits, which may or may not be passed on to consumers. Is the reporting difference an ethical breach of confidence between the consumer and the industry?”
Web-based application designers often make a fundamental mistake in their applications regarding the source of data....
Web-based application designers often make a fundamental mistake in their applications regarding the source of data. Identify and explain this mistake, including how applications handle this common error.
Question 4: Jar Game Consider the following game: Players: 2 - We designate player #1 to...
Question 4: Jar Game Consider the following game: Players: 2 - We designate player #1 to be the one that starts with the jar. Actions: - Each round at the same time both players deposit between 1 to 4 pennies into the jar. - Then both players are able to count the pennies in the jar. - If there are 21 or more pennies, the person with the jar is the winner. - If there are 20 or less pennies,...
Consider today's applications. Can it be that we are designing systems that are extremely complex, making...
Consider today's applications. Can it be that we are designing systems that are extremely complex, making it difficult to place an assignment on a class or object? Is it better for support to have something simple or complicated for an organization?
Case 2.2 Business Case: Data Chaos Creates Risk Data chaos often runs rampant in service organizations,...
Case 2.2 Business Case: Data Chaos Creates Risk Data chaos often runs rampant in service organizations, such as health care and the government. For example, in many hospitals, each line of business, division, and department has implemented its own IT applications, often without a thorough analysis of its relationship with other departmental or divisional systems. This arrangement leads to the hospital having IT groups that specifically manage a particular type of application suite or data silo for a particular department...
NEED ANSWER ASAP (Hypothesis testing) What do we need to consider when we try to select...
NEED ANSWER ASAP (Hypothesis testing) What do we need to consider when we try to select a test? Choose one of the tests; discuss your understandings of that test. Use some examples to demonstrate your understanding. NEW ANSWER NEVER USED BEFORE !!!!!!!!!!!!!!!!! ANSWER THROUGHLY, COPY AND PASTE PLEASE
In case of big data there is a need of new data architecture, new storage approaches,...
In case of big data there is a need of new data architecture, new storage approaches, new tools, new analytics methods and technique. Select one: a. True b. False 2- The Shopping Mall wants to group the products based on the data about their similar features, which of the following data mining approaches can be applied Select one: a. Association b. Regression c. Clustering d. Classificiation 3- A Excell spreedshed containing id number, names and marks of the student is...
In Nonlinear regression analysis, if we have a data set and if we need to model...
In Nonlinear regression analysis, if we have a data set and if we need to model nonlinear, once we enter the data either in R or SPSS software we observe the results, but we cannot see the exact equation as we can write for linear regression.as an example if we want to see the relationship between Sales the response variable and the advertising budget for TV media and News paper we have to independent variables,once the data we entered how...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT