Question
#include stdafx.h #include arrays.h #include int main() { int Array[] = { -15, 25, 5, -4, 10, 3, 100, 50, -20 }; // 9 elements
#include "stdafx.h"
#include "arrays.h"
#include
int main()
{
int Array[] = { -15, 25, 5, -4, 10, 3, 100, 50, -20 }; // 9 elements of array
int size;
int Array2[10];
int Array3[5] = {};
int Array4[10] = {};
size = sizeof(Array) / sizeof(Array[0]); // size of array is 9 divided by array element 0
randomizeArray(Array2, 10, 50, 100); // array2 contains 10 elemts of array
printf("Randomized Array ");
printArray(Array2, 10);
printArray(Array, size);
countElements(Array, size, -20, 20);
getMax(Array, size);
getMin(Array, size);
sortAscending(Array, size);
printf("Ascending order of Array ");
printArray(Array, size);
sortDescending(Array, size);
printf("Descending order of Array ");
printArray(Array, size);
setArray(Array3, 5);
printf("Set Array");
printArray(Array3, 5);
getMedian(Array, size);
getAverage(Array, size);
copyArray(Array4, Array, size);
printf("Array copy ");
printArray(Array4, size);
searchArray(15, Array, size);
searchArray(25, Array, size);
return 0;
}
#include "stdafx.h" #include "arrays.h" #include
// Function body/declarations // method to print all the elements of the array // size represents number of elements in 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 elements 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; }
An in depth explanation of the code in paragrpah.Ffor example, what is the for loop used for and how all the other variables work and what its used for and also the equations.
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