An in depth explanation of the code in paragrpah. For example, what is the for l
ID: 3847729 • Letter: A
Question
An in depth explanation of the code in paragrpah. For example, what is the for loop used for and how all the other variables work and what its used for and also the equations.
#include "stdafx.h"
#include "arrays.h"
#include <stdio.h>
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 <stdio.h>
#include <time.h>
#include <stdlib.h>
// 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;
}
Explanation / Answer
This is plane explanation for second example:
#include "stdafx.h"
#include "arrays.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
// Function body/declarations
// method to print all the elements of the array
// size represents number of elements in the array
/* this function is used to print the respected array
-first parameter is passed array
-second parameter is size of respected 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++) //loop to print array starting from 0 to array size
{
printf("a[%d] = %d ", i, a[i]); // output for this line (where n represents elements in array): a[0]=n
}
return; //return to function
}
// method to copy src array elements one by one to dest array
/* This function used to copy one array element to another
- First parameter is source array
- Second parameter is destination array where the elements will be copied from src array
- Third parameter is size of the 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++) // Loop will be executed from 0 to defined size
{
dest[i] = src[i]; //This line will assign source array member to destination array members
}
return;//return to function
}
// It puts the random values between a low and high value range into the array
/* This function fills random numbers to array nums[] between a specified range
- First parameter is the array
- Second parameter is array size
-Third parameter is value to be starting number for a range
-Fourth parameter is value to be ending number for a range
*/
void randomizeArray(int nums[], int array_size, int low_val, int high_val)
{
srand(time(NULL)); //This function is used to seed for random function and passed parameter represents
//time(NULL) -- This is an integer value to be used as seed by the pseudo-random number generator algorithm and the value will be used as seconds
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;
/*
For example to print numbers between(10 -100) we will define it as follows where consider random value is 6716:
6716 % (100 + 1 - 10) + 10
50 = 6716%101
*/
nums[i] = random; // 50 will be stored in num[i]
printf("nums[%d] =%d ", i, nums[i]); // This will print-> nums[0] = 50
}
return;
}
// To count number of elements in array which lie in the range of low to high
/*
-First parameter The array to be counted
-Second parameter is size of array
- Third parameter is starting value size of number to be counted
- Fourth parameter is highest value size of number to be counted
*/
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
//- First parameter is array
//- Second is array size
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) // if nums[x] greater than max value
{
max = nums[x]; // store number in max variable
}
}
printf("The maximum value of the array is: %d ", max);
return max; // return the max value
}
// get the minimum element of array
//- First parameter is array
//- Second is array size
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) // if nums[x] is less than min
{
min = nums[x]; // store number to min
}
}
printf("The minimum value of the array is: %d ", min);
return min; //return min value
}
// 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
/*
Selection sort algorithm starts by compairing first two elements of an array and swapping if necessary,
i.e., if you want to sort the elements of array in ascending order and if the first element is
greater than second then, you need to swap the elements but, if the first element is smaller than second,
leave the elements as it is. Then, again first element and third element are compared and swapped if necessary.
This process goes on until first and last element of an array is compared.
This completes the first step of selection sort.
If there are n elements to be sorted then, the process mentioned above should be repeated n-1 times to
get required result. But, for better performance, in second step,
comparison starts from second element because after first step, the required number is automatically placed at
the first (i.e, In case of sorting in ascending order, smallest element will be at first and
in case of sorting in descending order, largest element will be at first.). Similarly, in third step,
comparison starts from third element and so on.
*/
//This will arrange the numbers in ascending order
/*For example consider following numbers:
For first loop Cycle
19,12,10,15,5 elements indexes swiped are: 0,1 (19,12)
12,19,10,15,5 elements indexes swiped are: 0,2 (12,10)
10,19,12,15,5 elements indexes swiped are: 0,3 (10,15)
10,19,12,15,5 elements indexes swiped are: 0,4 (10,5)
5,19,12,15,10 final result for first loop cycle
For Second loop Cycle
5,19,12,15,10 elements indexes swiped are: 1,2 (19,12)
5,12,19,15,10 elements indexes swiped are: 1,3 (12,15)
5,12,19,15,10 elements indexes swiped are: 1,4 (12,10)
5,10,19,15,12 final result for second loop cycle
For Third loop cycle
5,10,19,15,12 elements indexes swiped are: 2,3 (19,15)
5,10,15,19,12 elements indexes swiped are: 2,4 (15,12)
5,10,12,19,15 final result for third loop cycle
for Fourth loop cycle
5,10,12,19,15 elements indexes swiped are: 3,3 (19,15)
5,10,12,15,19 final result*/
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
/* This function sort the array in similar mechanism as above mentioned and outputs value 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
//This function fills elements in the given size of array
/* First parameter is array
Second parameter is aray size*/
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); // get the value from console
nums[i] = x; // assign value to respective array index
}
}
// find median of given array
//This function calculate the median for given array size
int getMedian(int nums[], int array_size)
{
//for example consider values in array as 15,20,22,25,10
int a, b, c, d, e;
float median;
printf("Finding Median Array ");
b = array_size / 2 - 1; // Array size is 5 / 2 -1 this result 1.5 hence 1 will be stored in b
c = array_size / 2; // 5 / 2 here 2.5 is result and 2 will be stored in c
// If array is having odd elements, the middle element is median
if (array_size % 2 != 0) // Now 5%2 is 1 hence which is not equal to 0
{
return nums[c]; // it will return num[2] i.e. 22
printf("Array Median is %.1f ", nums[c]); // ouput is : Array Median is 22
}
else
{
// For the same above example for even number i.e for 6 elements where 30 is 6th element
// Then b will have 6/2-1 = 2
// c will have 6/2 = 3
// If array is having even elements, the middle two element avergae is median
median = ((nums[b] + nums[c]) / 2.0); // num[2] is 22 and num[c] is 25 then (22+25)/2.0 = 23.5
return median;
printf("Array median is between %.0f and %.0f therefore median is %.1f ", nums[b], nums[c], median);
// OUTPUT is
//Array median is between 22 and 25 therefore median is 23.5
}
}
// find average of given array
// This function calculate average for given array size
float getAverage(int nums[], int array_size)
{
// Consider array elements as : 10,12,8,12,12
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];
/* At 0 loop sum(10) = 10;
At 1 loop sum(22) = 10 +12;
At 2 loop sum(30) = 22+8;
At 3 loop sum(42) = 30+12;
At 4 loop sum(54) = 42+12;
Hence Sum=54 */
}
avg = ((float)sum) / array_size; // Now (54.0)/5 Then avg=10.8
printf("Array Average is %f ", avg); // OUTPUT is: Array Averge is 10.8
return avg;
}
// search if given element is present in array or not
//This function search for specific element in array of given size
//- First parameter is value to be search in array
//- Second Parameter is array
//- Third parameter is size of array
int searchArray(int search_val, int array[], int array_size)
{
// Consider array elements as : 10,50,44,22,12
// Consider 44 as search value
//Array size is 5
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++) // Loop will continue 5 times
{
// if element matches, return the index
if (array[i] == search_val) //
{
return i;
}
/* For loop at 0 array[0] == 44 i.e. 10==44 not satisfy and i=0
For loop at 1 array[1] == 44 i.e. 50==44 not satisfy and i=1
For loop at 2 array[2] == 44 i.e. 44==44 satisfy and i=2
hence value of i i.e. 2 is returned*/
}
return -1; // if any value doesnt match return -1
}
Now the outputs as per your First Example and respective calls are as follows:
randomizeArray(Array2, 10, 50, 100); // array2 contains 10 elemts of array
printf("Randomized Array ");
==> This call will fill random values between 50-100 in nums array of function randomizeArray
- Note that this call is call by value and does not change original value of array2
- This call will print any numbers between 50-100 (The procedure is already defined in above function)
printArray(Array2, 10);
==> This will print array elements in Array2 but as the array is not initialize any value it will fire error as array index out of bounds if the call is by reference and values initialize then the 10 values will be printed
printArray(Array, size);
==> As there are 9 elements hence size is 9 and will print following 9 values:
Array print
Array has 8 elements
Element Value
a[0]=-15
a[1]=25
a[2]= 5
a[3]=-4
a[4] = 10
a[5]=3
a[6]=100
a[7]=50
a[8]=-20
-----------------------------------------------------------------------
countElements(Array, size, -20, 20);
==> ouput is
Array count element
Array consists 9 elements from -20 to 20
------------------------------------------------------------------
getMax(Array, size);
==> Ouput:
Array max integer
The maximum value of the array is:100
------------------------------------------------------------------------
getMin(Array, size);
==> Output:
Array min integer
The minimum value of the array is: -20
-----------------------------------------------------------------------
sortAscending(Array, size);
==> OUPUT
-20 , -15, -4, 3 , 5, 10,25,50,100
-------------------------------------------------------------------------------
sortDescending(Array, size);
Descending order of Array
-20 , -15, -4, 3 , 5, 10,25,50,100
100,50,25,10,5,3,-4,-15,-20
setArray(Array3, 5);
==> OUPUT
Create Array
Array has 5 elements.
Enter a value for each elements
Loop will be executed 5 times and 5 values will be num[]
--------------------------------------------------------------------------------------
printArray(Array3, 5);
==> As the call is by value and not by reference this will fire error
---------------------------------------------------------------
===> getMedian(Array, size);
As for inputs -15, 25, 5, -4, 10, 3, 100, 50, -20 there are 9 elements
hence condition is not satisfied and else part will be executed
now b(3) = 9/2-1
c(4) = 9/2;
now nums[3]=-4 and nums[4]=10 then
(-4 +10 ) / 2.0 is 3
Hence output is
Array median is between -4 and 10 therefore median is 3
-------------------------------------------------------------------------------------------
getAverage(Array, size);
==> OUPUT
Array average finder
Array Average is 17.11
------------------------------------------------
copyArray(Array4, Array, size);
==> Elements of Array4 will be copied to Array
-------------------------------------------------------------------------
searchArray(15, Array, size);
==> OUPUT : As there is no item 15 is in array it returns -1
------------------------------------------------------------------------------------
searchArray(25, Array, size);
OUTPUT: As the item 25 is at index 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.