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

all Verizon 55% 1:12 PM eecs.wsu.edu Take-Home Quiz 6 (15 pts) Arrays NOTE: Plea

ID: 3726205 • Letter: A

Question

all Verizon 55% 1:12 PM eecs.wsu.edu Take-Home Quiz 6 (15 pts) Arrays NOTE: Please submit your hard copy solution in lab this week 0 1. (6 pts) Write a function called find max that accepts an array of integers and the number of items in the array as parameters, and returns the largest number in the array. 2. (9 pts) Write a function called largest sum sequence ) that accepts an array of signed integers and the number of items in the array as parameters, and returns the largest sum of a sequence of numbers in the array. A sequence is defined as a single item or multiple items that are in consecutive adjacent memory locations. Example 1:93-1 7 -12 largest sum in sequence is 18 [9, 3,-1,7 Example 2: -77 3 2 largest sum in sequence is 3 [3] Instructor: Andrew S. O. Fallon

Explanation / Answer

Solution 1:

#include <iostream>

using namespace std;

// Function to find out the maximum of given list

int find_max(int arr[],int n){

// Difining the first element of the array to be max

int large=arr[0];

for(int i=0; i<n; i++){

// Checking the values in the list

if(large<arr[i]){

large=arr[i];

}

}

//returning the max value to the called function

return large;

}

int main() {

int arr[100], n, i;

// Taking inputs from the user

printf("Enter Array Size : ");

scanf("%d",&n);

printf("Enter array elements : ");

for(i=0; i<n; i++){

scanf("%d",&arr[i]);

}

// Printing the max value

printf("Max value is: %d",find_max(arr,n));

}

Solution 2:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
// function to find the largest_sum_sequence
int largest_sum_sequence(int a[], int size, int *start_index, int *end_index){
int current_max = 0, maximum_end = 0;
int i, current_index = 0;
for (i = 0; i < size; i++){
maximum_end = maximum_end + a[i];
if (maximum_end <= 0){
maximum_end = 0;
current_index = i + 1;
}else if (current_max < maximum_end){
current_max = maximum_end;
*start_index = current_index;
*end_index = i;
}
}
return current_max;
}
//Driver function to test the function
int main(){
int arr[] = {9,3,-1,7,-12};
int start_index = 0, end_index = 0;
int size = sizeof(arr) / sizeof(arr[0]);
printf(" The max sum is %d [", largest_sum_sequence(arr, size, &start_index, &end_index));
for(int i=start_index; i<end_index; i++){
printf("%d,",arr[i]);
}
printf("%d]",arr[end_index+1]);
getchar();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote