#include \"stdafx.h\" #include \"arrays.h\" #include <stdio.h> int main() { int
ID: 3847711 • Letter: #
Question
#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;
}
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.
Explanation / Answer
1.printArray function:
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;
}
//explanation:
/*printArry function takes an array and size of array as an arguments
and declares variable i as type int.for for loop iteration.
first printf statement prints just Array print
second pritf statement prints size of arry
third pritf prints just element values
now the for loop starts from o to size of array
it prints 0'th element and
i=1 then 1st element
i=2 then 2nd element
so...on till size of array
and finally comes out of loop.
2.copying from one array to another 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;
}
//expalanation
here copyArray function takes 3 aruguments .one is array is containing elements and second array we should copy elements into it and third is size of both the array
one thing we should remember while copying ..the array sizes should be same to copy the elements of one array to another array.or else not possible
for loop starts from o to size of array.
dest[i]=sec[i]; this statement is used for coping individual elements one by one during iteration .if i=0 then src array's 0th element is copied to dest array of 0th index and so..on till array size limit occurs.
3.randomnumbers function
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;
}
//explanation
this function is used for filling array elements with random numbers generated by system within a particular range of values
randomizeArray function has 4 arguments .one is array itself (empty array).and second one is size of array and third one is starting range value and last one is end range value.
when for loop starts with 0 to size of array
for every i'th iteration the rand function will generate a random value within range and stores in random variable of type integer.
and this random variable is set to i'th index value of array....
if i=0;
random=rand()%(high_val+1-low_val)+low_val;------->this gives a random number of specified range..
4.count number of elements in array within range
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;
}
//explanation
this function countElements takes 4 arguments .first one is array itself and second one is array size and third one is variable of low value and last one is variable of high value.
when a for loop begins iteration
every ith index element is checked for 2 condition;
first one is=nums[i]>=low_value----------------------->this means the array element should be greater than low_value;
second one is=nums[i]<=high_value------------------>this means the array element should be lesser than or equal to high_val;
if both the conditions are satified then the ith element is satified and
counter variable increments by one value;
and if condition is not satisifed counter doesnot increments it remains same
finall this function returns a counter variable because it is ot type int..
5.get maximum element in 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;
}
//explanation
this function takes 2 arguments one is array itself and array size
this function is of type int so it returns a variable of type int ..i.e max
initially the max variable is stored with first element in given array.before iteration starts.
when an iteration starts
for every x'th element is checked whether it is greater than max variable.if it greater than max variable is then stores current greater element by deleting old one.
if condition is not satified than nothing is happend.
finally the max variable contains one value ..that value is considered as final max value;
and this is returned by function to main.
6.get minimum element in array
int getmin(int nums[], int array_size)
{
int x;
printf("Array min integer ");
// assume first element to be max
int min = 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] <min)
{
min = nums[x];
}
}
printf("The minimum value of the array is: %d ", min);
return min;
}
//explanation
this function takes 2 arguments one is array itself and array size
this function is of type int so it returns a variable of type int ..i.e min
initially the min variable is stored with first element in given array.before iteration starts.
when an iteration starts
for every x'th element is checked whether it is leser than min variable.if it lessser than min variable is then stores current lesser element by deleting old one.
if condition is not satified than nothing is happend.
finally the min variable contains one value ..that value is considered as final min value;
and this is returned by function to main.
7.sort elements of 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;
}
}
}
}
//explanation
this function takes an array as input also array size
here we iterate 2 for loops
first one is to itearate from start end of size
second one is to iterate from start end to size
and checks that 0th postion is greater than next position element
if it is greater than swap both the elements...and continue to do..till end of size
then array will be in ascending order.
8.sort elements of array in descending order
void sortdescending(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;
}
}
}
}
//explanation
this function takes an array as input also array size
here we iterate 2 for loops
first one is to itearate from start end of size
second one is to iterate from start end to size
and checks that 0th postion is lesser than next position element
if it is lesser than swap both the elements...and continue to do..till end of size
then array will be in descending order
9.set particular element in 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;
}
}
//
explanation
this function takes an array and array size as arguments.
here first it asks user to give input through console
and stores in variable x
then value of x is stored in all the ith position.till end of loop
9.find average of all elements in 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;
}
//explanation
this function takes array and size of array as arguments
it declares an integer variable sum to add all elements of array
for loop starts from 0 to end;
when i ==0
sum stores 0th elements
when i==1
sum stores =0th +ith element
when i==2;
sum stores =sum+ith element
so....on till end of size;
finall sum stores sum of all elements at end of for loop
and final step is to store a variabl of type float called avg which divides the sum by total elements and convert into floating value and returns the float value to main method.
10.find or search particular element in array
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
this function takes first argument search_val the element need to search and next is array itself and finally array size
this function is type int
this fucntion returns 1 or -1 (one if conditon satisfies a nd -1 if not satifies
for looop begins from o to end
every ith element is checked with search_element
if it is equal then it returns 1 or else returns -1
till the end of size;
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);
}
}
//explanation
this function takes array and size of array as arguments
variable b stores excat array size
variable c stores half size
if array size is odd then returns c
else
middle two elements average is taken as median
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.