Question

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:


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

Solutions

Expert Solution

=====================================================

#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;
}


Related Solutions

C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1); sine =...
Complete the following exercises using C programming language. Take screenshots of the code and its output...
Complete the following exercises using C programming language. Take screenshots of the code and its output where specified and paste them into in a well-labeled Word document for submission. Scenario Assume you are the CIO of an organization with three different IT department locations with separate costs. You want a program to perform simple IT expenditure calculations. Your IT expenditure target is $35,000 per site. Site expenditures: Site 1 – $35,000. Site 2 – $37,500. Site 3 – $42,500. Exercise...
In C/C++ programming language, write down the lines of codes (and figure) to show- a) assignment-by-sharing...
In C/C++ programming language, write down the lines of codes (and figure) to show- a) assignment-by-sharing b) dangling reference
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu...
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu to choose from – 1. Convert temperature input from the user in degrees Fahrenheit to degrees Celsius 2. Convert temperature input from the user in degrees Celsius to degrees Fahrenheit 3. Quit. Formulae you will need: C = (5 / 9) * (F-32) and F = (9/5) * C + 32 1. Use functions to accomplish 1 and 2 above. 2. Use at least...
Rewrite the C PROGRAMMING LANGUAGE CODE in terms of only dereferencing (*) and pointer addition (+)...
Rewrite the C PROGRAMMING LANGUAGE CODE in terms of only dereferencing (*) and pointer addition (+) AND extend the code so that allocated memory is freed properly. Thank you struct foo { int a; char b; }; int main(void) { struct foo* arr[5]; int x; for(x = 0; x < 5; x++) { arr[x] = malloc(sizeof(struct foo)); arr[x]->a = 0; arr[x]->b = 'b'; } }
how to convert Sudo Code to C language of these two functions MsgEnv * request_msg_env( )...
how to convert Sudo Code to C language of these two functions MsgEnv * request_msg_env( ) { search for free memory block in the queue of the free blocks(shown in the data structure section ); if (no memory block is available) { Block invoking process(Process_switching(); } else { update the data structure; return a pointer to the memory block; } int release_msg_env( MsgEnv * msg_env_ptr ) : { if (memory block pointer is not valid) return ErrorCode; Add memory block...
You are using ONLY Programming Language C for this: In this program you will calculate the...
You are using ONLY Programming Language C for this: In this program you will calculate the average of x students’ grades (grades will be stored in an array). Here are some guidelines to follow to help you out: 1. In your program, be sure to ask the user for the number of students that are in the class. The number will help in declaring your array. 2. Use the function to scan the grades of the array. To say another...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes the final score of a baseball game. Use a loop to read the number of runs scored by both teams during each of nine innings. Display the final score afterward. Submit your design, code, and execution result via file, if possible
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT