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

-Computes the sum of all even integers in the array and displays the result on t

ID: 3919810 • Letter: #

Question

-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

-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.

Comment the code Please!

roblem Assuming that the following integer array AnayInts[ ] = {54.99. 160.-8. 3. i 42, 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 withi 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>

int main()

{

//array declaration

int ArrayInts[]={54, 99, 160, -8, 3, 142, 29, -13, 260, 27, -4, 10, 12, 15, 58, 480, 60 };

//finding the length of array using sizeof method.

//sizeof array would be total bytes occupied by the array

//sizeof int is bytes occupied by each int

//their quotient gives the number of integers in the array

int length = sizeof(ArrayInts)/sizeof(int);

//2 variables, one for sum of odd ints, one for sum of even ints

int even_sum,odd_sum=0;

//looping over the array and checking if even or odd

//if even, add that number to even sum

//if odd, add that number to odd sum

for(int i=0;i<length;i++)

{

if(ArrayInts[i]%2==0)

even_sum+=ArrayInts[i];

else

odd_sum+=ArrayInts[i];

}

//print the odd sum and even sum

printf("Sum of all odd integers is %d ",odd_sum);

printf("Sum of all even integers is %d ",even_sum);

//sorting the array using bubble sort algorithm

//sorting has to be done, because binary search works only on sorted arrays

for(int i=0;i<length;i++)

{

for(int j=i+1;j<length-1;j++)

{

if(ArrayInts[i]>ArrayInts[j])

{

int temp=ArrayInts[i];

ArrayInts[i]=ArrayInts[j];

ArrayInts[j]=temp;

}

}

}

//printing the array after sorting

printf("Array after sorting: ");

for(int i=0;i<length;i++)

{

printf("%d ",ArrayInts[i]);

}

printf(" ");

/*

Binary search logice is exactly same as working of dictionary.

if we want to search for a word, say "horse" in dictionary, we randomly open a page and see if we are in the right page or not,

This makes the dictionary into two parts, partition on left and partition on right.

if we are in the right page, just see the meaning and close it

if the page has entered the words starting with "i", that means we crossed our intentional page, and we limit our search only to the left partition

if the page has entered the words starting with "c" that means we need to search in the right partition..

we continue the same, until we find the word.

*/

//same way, marking starting and ending points of array

int first=0, last=length-1,middle;

//loops as long as first less than last

//if first is greater than last, that means we didn't find our element

while(first<=last)

{

//calculating middle index

middle=(first+last)/2;

//checking if element is found and breaks the loop if found

if(ArrayInts[middle]==odd_sum)

{

printf("Element %d found at index %d ",odd_sum,middle);

break;

}

//if array's element is greater than search element, we need to search in left partition hence, updating the last

else if(ArrayInts[middle]>odd_sum)

{

last = middle-1;

}

//if array's element is less than search element, we need to search in right partition hence, updating the first

else

{

first=middle+1;

}

}

if(first>last)

{

printf("Target was not found");

}


}

Output: