Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*Program is in C* Write a function to insert numbers 0..n - 1 into an array of s

ID: 3797569 • Letter: #

Question

*Program is in C*

Write a function to insert numbers 0..n - 1 into an array of size n randomly. Each number should appear in the array only once and the order of numbers in the array should be random. Prototype of the function is given below, data is an integer array and n is the size of the array. void insertrandom(int data[], int n) Write a function issorted() that returns 1 if the array passed as a parameter is sorted in increasing order and returns 0 otherwise. Prototype of the function is given below. data is an integer array and n is the size of the array. int issorted(int data[], int n)

Explanation / Answer

Program 1:

Here is code:

void insertrandom(int data[],int n)
{
int i;
int array[n];

for (i = 0; i < n; i++) { // fill array
array[i] = i;
}

for (i = 0; i < n; i++) { // shuffle array
int temp = array[i];
int randomIndex = rand() % n;
array[i]= array[randomIndex];
array[randomIndex] = temp;
}


for (i = 0; i < n; i++) { // print array
printf("%d ",array[i]);
}
}

Sample program to test:

#include <stdio.h>

void insertrandom(int data[],int n);
int main()
{
int N = 10;
int array[N];
insertrandom(array,N);
printf(" ");

return 0;
}

void insertrandom(int data[],int n)
{
int i;
int array[n];

for (i = 0; i < n; i++) { // fill array
array[i] = i;
}

for (i = 0; i < n; i++) { // shuffle array
int temp = array[i];
int randomIndex = rand() % n;
array[i]= array[randomIndex];
array[randomIndex] = temp;
}


for (i = 0; i < n; i++) { // print array
printf("%d ",array[i]);
}
}

Output:

3 8 2 4 5 0 1 7 9 6

Program 2:

int issorted(int data[],int n)
{
int i;
int array[n];

for (i = 0; i < n - 1; i++) {   
if(data[i] > data[i+1]) //check if any element is greater the next element
return 0;
}
return 1;

}

Sample program to test:

#include <stdio.h>

int issorted(int data[],int n);
int main()
{
int N = 10;
int array[] = { 0,1,2,3,4,5,6,7,8,9};
int result = issorted(array,N);
printf("%d ",result);

return 0;
}

int issorted(int data[],int n)
{
int i;
int array[n];

for (i = 0; i < n - 1; i++) {   
if(data[i] > data[i+1]) //check if any element is greater the next element
return 0;
}
return 1;

}

Output:

1