Look carefully at the requirements:
Write a function ... smallest_sum_sequence (), which takes (1) an array of signed integers and (2) the number of elements in the array as arguments, and (3) returns the smallest sum of the sequence of numbers in the array.
A sequence is defined as a single element or several elements ... in adjacent memory cells .
Taking them one at a time, you know that you will write a function that takes an array of type int , and then the number of elements (it will not be negative, so size_t is a good type). Since it should return the smallest amount, the return type of the function may be int :
int smallest_sum_sequence (int *a, size_t n) { ... return ssum; }
This is the basic structure for your function. The next question to answer is smallest sum . Since you are told that you are accepting an array of signed values, you should assume that the values ββin the array can be both negative and positive . Then you are informed that the sum of the smallest sequence can be obtained from single or several neighboring values ?
The fact that I interpret this means that you have to save 2 current values. (1) the minimum value in the array; and (2) the sum of the smallest sequences.
In the arguments, you get the number of elements in the array, providing you with an easy means to iterate over the array itself:
int i = 0; int min = INT_MAX; int ssum = INT_MAX; for (i = 0; i < n; i++) { min = a[i] < min ? a[i] : min; if (i > 0) { int tssum = 0; } }
In your first iteration over the array, you found min minimum single value and ssum sum of the smallest sequence . Now all that remains is return:
return min < ssum ? min : ssum;
This is my idea of ββwhat the logic requested. You may need to tweak the pseudo-code logic and need to figure out how to determine the start / end of a sequence, but this should at least give you a diagram of one way to get closer to it. Good luck.
David C. Rankin
source share