1. (6 pts) Write a function called find min) that accepts an array of integers a
ID: 3596543 • Letter: 1
Question
1. (6 pts) Write a function called find min) that accepts an array of integers and the number of items in the array as parameters, and returns the smallest 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:9 3 -1 7 -12 Example 2: -77 3 -21 largest sum in sequence is 18 [9, 3, -1, 7] largest sum in sequence is 3 [3]Explanation / Answer
Question 1
#include<iostream>
using namespace std;
int find_min(int a[],int s) //function to calculate min
{
int min,i;
min=a[0];
for(i=0;i<s5;i++)
{
if(a[i]<min)
min=a[i]; //storing the min elements
}
return min; //returning the min value
}
int main()
{
int arr[10],k,i;
cout<<"Enter the elements in the array "; //accepting number from user
for(i=0;i<10;i++)
{
cin>>arr[i];
}
k=find_min(arr,10); //calling the function
cout<<"The minimum number is "<<k; //displaying the result
}
Question 2
#include<iostream>
using namespace std;
int largest_sum_sequence(int a[],int s) //function to calculate max sum
{
int mf = 0, mh= 0;
for (int i = 0; i < s; i++)
{
mh = mh + a[i];
if (mf< mh)
mf = mh;
if (mh < 0)
mh = 0;
}
return mf; //retruning the max sum
}
int main()
{
int arr[5],k,i;
cout<<"Enter the elements in the array "; //accepting number from user
for(i=0;i<5;i++)
{
cin>>arr[i];
}
k=largest_sum_sequence(arr,5); //calling the function
cout<<"Largest Sum Sequence is "<<k; //displaying the result
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.