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

Assuming that the following integer array Array Ints[ ] = {54.99. 160.-8. 3. 142

ID: 3919852 • Letter: A

Question

Assuming that the following integer array Array Ints[ ] = {54.99. 160.-8. 3. 142. 29.-13. 260, 27-4. 10. 12, 15, 58, 480, 603: is stored in the memory. Write a complete C language program that: computes the sum of all even integers in the array and displays the result on the console, computes the sum of all odd integers in the array and displays the result on the console, and Searches the array for the target value of the sum of all odd integers using Binary Search. If the target value was found within the array, a message indicating array index at which the target value was found should be displayed on the console otherwise "Target was not found" should be displayed

Explanation / Answer

#include <stdio.h

//function to swap two elements

void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}

/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition (int arr[], int index[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element

for (int j = low; j <= high- 1; j++)
{
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
swap(&index[i], &index[j]);
}
}
swap(&arr[i + 1], &arr[high]);
swap(&index[i + 1], &index[high]);
return (i + 1);
}

/* The main function that implements QuickSort
arr[] --> Array to be sorted,
index[] --> Array to hold the original index value of the array elements
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int index[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, index, low, high);

// Separately sort elements before
// partition and after partition
quickSort(arr, index, low, pi - 1);
quickSort(arr, index, pi + 1, high);
}
}

int binarySearch(int arr[], int index[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l)/2;

// If the element is present at the middle
// itself
if (arr[mid] == x)  
return index[mid]; //returns the original index value of that array element in unsorted array

// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, index, l, mid-1, x);

// Else the element can only be present
// in right subarray
return binarySearch(arr, index, mid+1, r, x);
}

// We reach here when element is not
// present in array
return -1;
}

int main()
{
int array[]={54,99,160,-8,3,142,29,-13,260,27,-4,10,12,15,58,480,60};
int index[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
int i,sumOfEven=0,sumOfOdd=0;

  
for(i=0;i<17;i++)
{
if(array[i]%2==0)
sumOfEven=sumOfEven+array[i];
else
sumOfOdd=sumOfOdd+array[i];
}
printf(" Sum of Even Numbers = %d",sumOfEven);
printf(" Sum of Odd Numbers = %d",sumOfOdd);
quickSort(array,index, 0,16);
int result = binarySearch(array, index, 0, 16, sumOfOdd);
(result == -1)? printf(" Target was not found in array")
: printf(" Target was found at index %d",result);   
return 0;
}

Output:

Sum of Even Numbers = 1224                                                                                                     

Sum of Odd Numbers  = 160                                                                                                      

Target was found at index 2