Question
I'm having some trouble with this program. I can't quite get the right output. Please help? This is a C program. The purpose of this
I'm having some trouble with this program. I can't quite get the right output. Please help?
This is a C program. The purpose of this exercise is to demonstrate one technique for randomizing an array of permutations. It also gives us some exposure to the use of the random number generator in C. In addition to the function that randomizes the permutation array, please implement three other functions: a function that prints the array, a function that sorts the array, and a function that swaps two integers. These are described below. Your program should perform the following steps:
1) In the main() function
a) Create an array of ints of size 100.
b) Initialize the array with sequential numbers from 100 down to 1. (This will create a permutation.)
c) Call the following functions in the following order:
i) printArray (int inputArr[], int size) // print original order
ii) randomizeArray (int inputArr[], int size)
iii) printArray (int inputArr[], int size) // print randomized order
iv) bubbleSortArray (int inputArr[], int size)
v) printArray (int inputArr[], int size) // print sorted order
2) Function descriptions:
a) void printArray (int inputArr[], int size )
i) This function will print the array contents, 10 numbers per line. Each number should be right justified in a field of size 5.
b) void randomizeArray (int inputArr[], int size)
i) This function will randomize the array according to the algorithm given below.
ii) Algorithm:
(1) Use srand() to seed the random number generator with a seed of 10 as we discussed in class.
(2) Set a counter variable called currPos to 0. (This will function as an index to the array. Setting it to 0 means that it accesses the first element of the array.)
(3) Loop currPos from 0 to 99, performing the following steps
(a) Randomly choose an array index between 0 and 99.
(b) Exchange the element at currPos with the randomly chosen element by using the swap() function.
(c) Increment currPos.
c) void bubbleSortArray (int inputArr[], int size)
i) This function will sort the elements in the array according to the BubbleSort algorithm given.
ii) Algorithm:
(1) outer loop index i runs from n-1 down to 1
(2) inner loop index j runs from 0 to i-1
(3) compare array[j] and array[j+1]
(4) swap if array[j]>array[j+1]
iii) Note: Again, you must use the swap() function we discussed in class to swap the two array elements.
d) void swap (int * x, int * y) i) This function will swap two integers as we discussed in class.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started