Describe the approach to solving the problems in the code. Explanation of code o
ID: 3847474 • Letter: D
Question
Describe the approach to solving the problems in the code.
Explanation of code operation: An in depth explanation, for example what is the for used loop for and why is it returned back to its value, and why we use array or others.
void printArray(int a[], int size)
{
int i;
printf("Array print ");
printf("Array has %d elements ", size);
printf("Element Value ");
for (i = 0; i < size; i++)
{
printf("a[%d] = %d ", i, a[i]);
}
return;
}
void copyArray(int src[], int dest[], int size)
{
int i;
for (i = 0; i < size; i++)
{
dest[i] = src[i];
}
return;
}
void randomizeArray(int nums[], int array_size, int low_val, int high_val)
{
srand(time(NULL));
int i, random;
for (i = 0; i < array_size; i++)
{
random = rand() % (high_val + 1 - low_val) + low_val;
nums[i] = random;
//printf("nums[%d] =%d ", i, nums[i]);
}
return;
}
int countElements(int nums[], int array_size, int low_val, int high_val)
{
int i, count = 0;
printf("Array count element ");
for (i = 0; i < array_size; i++)
{
if (nums[i] >= low_val && nums[i] <= high_val)
count++;
}
printf("Array consists %d elements from %d to %d ", count, low_val, high_val);
return count;
}
int getMax(int nums[], int array_size)
{
int x;
printf("Array max integer ");
int max = nums[0];
for (x = 0; x < array_size; x++)
{
if (nums[x] > max)
{
max = nums[x];
}
}
printf("The maximum value of the array is: %d ", max);
return max;
}
int getMin(int nums[], int array_size)
{
int x;
printf("Array min integer ");
int min = nums[0];
for (x = 0; x < array_size; x++)
{
if (nums[x] < min)
{
min = nums[x];
}
}
printf("The minimum value of the array is: %d ", min);
return min;
}
void sortAscending(int nums[], int array_size)
{
int i, j, temp;
for (i = 0; i < array_size - 1; i++)
{
for (j = 0; j < array_size - 1; j++)
{
if (nums[j] > nums[j + 1])
{
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
}
void sortDescending(int nums[], int array_size)
{
int i, j, temp;
for (i = 0; i < array_size - 1; i++)
{
for (j = 0; j < array_size - 1; j++)
{
if (nums[j] < nums[j + 1])
{
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
}
void setArray(int nums[], int array_size)
{
int i, x;
printf("Create Array ");
printf("Array has %d elements. ", array_size);
printf("Enter a value for each elements ");
for (i = 0; i < array_size; i++)
{
scanf_s("%d", &x);
nums[i] = x;
}
}
int getMedian(int nums[], int array_size)
{
int a, b, c, d, e;
float median;
printf("Finding Median Array ");
b = array_size / 2 - 1;
c = array_size / 2;
if (array_size % 2 != 0)
{
return nums[c];
printf("Array Median is %.1f ", nums[c]);
}
else
{
median = ((nums[b] + nums[c]) / 2.0);
return median;
printf("Array median is between %.0f and %.0f therefore median is %.1f ", nums[b], nums[c], median);
}
}
float getAverage(int nums[], int array_size)
{
float avg = 0.0;
float sum = 0.0;
printf("Array average finder ");
for (int i = 0; i < array_size; ++i)
{
sum += nums[i];
}
avg = ((float)sum) / array_size;
printf("Array Average is %f ", avg);
return avg;
}
int searchArray(int search_val, int array[], int array_size)
{
int i, val = 0;
printf("Searching array for %d ", search_val);
for (i = 0; i < array_size; i++)
{
if (array[i] == search_val)
{
return i;
}
}
return -1;
}
Explanation / Answer
// method to print all the elements of the array
void printArray(int a[], int size)
{
// counter
int i;
printf("Array print ");
printf("Array has %d elements ", size);
printf("Element Value ");
// Starting from 0 index to size -1, print each element
for (i = 0; i < size; i++)
{
printf("a[%d] = %d ", i, a[i]);
}
return;
}
// method to copy src array elements one by one to dest array
void copyArray(int src[], int dest[], int size)
{
// counter
int i;
// for each index from 0 to size-1, copy the respective
// element from src to dest
for (i = 0; i < size; i++)
{
dest[i] = src[i];
}
return;
}
// It puts the random values between a low and high value range into the array
void randomizeArray(int nums[], int array_size, int low_val, int high_val)
{
// to set the seed for random function
srand(time(NULL));
int i, random;
// For each index from 0 to array_size - 1, set the random value
for (i = 0; i < array_size; i++)
{
// below line generates a random value in range from low_val to high_val
random = rand() % (high_val + 1 - low_val) + low_val;
nums[i] = random;
//printf("nums[%d] =%d ", i, nums[i]);
}
return;
}
// To count number of elmenets in array which lie in the range of low to high
int countElements(int nums[], int array_size, int low_val, int high_val)
{
int i, count = 0;
printf("Array count element ");
// For each index from 0 to array_size - 1
for (i = 0; i < array_size; i++)
{
// if current value is in between range, increment the count
if (nums[i] >= low_val && nums[i] <= high_val)
count++;
}
printf("Array consists %d elements from %d to %d ", count, low_val, high_val);
return count;
}
// get the maximum element of array
int getMax(int nums[], int array_size)
{
int x;
printf("Array max integer ");
// assume first element to be max
int max = nums[0];
// For each index from 0 to array_size - 1
for (x = 0; x < array_size; x++)
{
// if current number is greater than max, replace the value of max with this element
if (nums[x] > max)
{
max = nums[x];
}
}
printf("The maximum value of the array is: %d ", max);
return max;
}
// get the minimum element of array
int getMin(int nums[], int array_size)
{
int x;
printf("Array min integer ");
// assume first element to be min
int min = nums[0];
// For each index from 0 to array_size - 1
for (x = 0; x < array_size; x++)
{
// if current number is lesser than min, replace the value of min with this element
if (nums[x] < min)
{
min = nums[x];
}
}
printf("The minimum value of the array is: %d ", min);
return min;
}
// To sort the given array in ascending order
void sortAscending(int nums[], int array_size)
{
int i, j, temp;
// Using selection sort
// For each index from 0 to array_size - 1
for (i = 0; i < array_size - 1; i++)
{
// For each index from 0 to array_size - 1
for (j = 0; j < array_size - 1; j++)
{
// If next number is less, swap that
if (nums[j] > nums[j + 1])
{
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
}
// To sort the given array in descending order
void sortDescending(int nums[], int array_size)
{
int i, j, temp;
// For each index from 0 to array_size - 1
for (i = 0; i < array_size - 1; i++)
{
// For each index from 0 to array_size - 1
for (j = 0; j < array_size - 1; j++)
{
// If next number is more, swap that
if (nums[j] < nums[j + 1])
{
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
}
// Ask user to fill the array
void setArray(int nums[], int array_size)
{
int i, x;
printf("Create Array ");
printf("Array has %d elements. ", array_size);
printf("Enter a value for each elements ");
// For each index from 0 to array_size - 1
for (i = 0; i < array_size; i++)
{
scanf_s("%d", &x);
nums[i] = x;
}
}
// find median of given array
int getMedian(int nums[], int array_size)
{
int a, b, c, d, e;
float median;
printf("Finding Median Array ");
b = array_size / 2 - 1;
c = array_size / 2;
// If array is having odd elements, the middle element is median
if (array_size % 2 != 0)
{
return nums[c];
printf("Array Median is %.1f ", nums[c]);
}
else
{
// If array is having even elements, the middle two element avergae is median
median = ((nums[b] + nums[c]) / 2.0);
return median;
printf("Array median is between %.0f and %.0f therefore median is %.1f ", nums[b], nums[c], median);
}
}
// find average of given array
float getAverage(int nums[], int array_size)
{
float avg = 0.0;
float sum = 0.0;
printf("Array average finder ");
// For each index from 0 to array_size - 1
for (int i = 0; i < array_size; ++i)
{
// add element to sum
sum += nums[i];
}
avg = ((float)sum) / array_size;
printf("Array Average is %f ", avg);
return avg;
}
// search if given element is present in array or not
int searchArray(int search_val, int array[], int array_size)
{
int i, val = 0;
printf("Searching array for %d ", search_val);
// For each index from 0 to array_size - 1
for (i = 0; i < array_size; i++)
{
// if element matches, return the index
if (array[i] == search_val)
{
return i;
}
}
return -1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.