The smallest sum of a sequence of numbers in an array. No code needed

Write a function smallest_sum_sequence (), which takes an array of signed integers and the number of elements in the array as arguments, and returns the smallest sum of the sequence of numbers in the array. a sequence is defined as a single element or several elements that are in adjacent memory locations.

This is obviously homework, I don’t need anyone to write the code for me, just an explanation of what they are actually looking for, since it is, in my opinion, strangely written.

I think they want:

Given an array and the total items in the array. Have the user input a sequence of values for the array ( array[7] -> array[9] ) return smallest sum 

Then determine the smallest amount? Should it be the smallest value or the smallest combination of items? The first sounds too simple, and the second does not make sense, even if you have negatives.

We are looking for some kind of enlightenment.

+7
c
source share
5 answers

You must sum each int with the following and find the minimum amount you can walk this way

 int min = INT_MAX; for (i = 0; i < len; i++) { sum = array[i]; min = MIN(min, sum); for (j = i + 1; j < len ; j++ ) { sum += array[j]; min = MIN(min, sum); } } 
+3
source share

Edited as the previous answer was wrong!

This is how I understand it. Suppose you have an array of signed integers called A , consisting of, say, <3, 4, 5>. So n = 3, the length of the array.

Your sequence is defined as one (or several) elements in neighboring memory cells. Thus, A [0] and A [1] will be a sequence, since they are in neighboring memory cells, but A [0] and A [2] will not.

You call your function: smallest_sum_sequence (A, n) with A and n, as described above.

So your sequences are:
+ length 1) <3 <4 <5> + length 2) <3.4 <4.5>
+ lengths 3) <3,4,5>

Therefore, your function should return 3 in this case.

+3
source share

Thus, a sequence is a set of any number of neighboring numbers in an array. In type set

 [ABCDE] 

Anyone can be the answer. Or [AB] might be the answer. Or [ABC] . Or [CDE] . Or even [ABCDE] . But, definitely not [ADE] , since A not adjacent to D in the original set. Easy.

Now you need to write a code that will compare the sum of the values ​​in all possible adjacent sequences, in any set of numbers (given the size of this set in advance).

+2
source share

With an array with a sign of integers, it is possible that a large sequence creates a smaller amount than a single number or pair.

To find out that you need to create all possible sequences:

 Start with first number alone, then first and second, then first, second and third. Then start with second number ... Then the sum of each sequence. 

Returns the smallest amount (and probably the corresponding sequence)

+1
source share

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; /* largest possible minimum number */ int ssum = INT_MAX; /* largest possible smallest sum */ for (i = 0; i < n; i++) { min = a[i] < min ? a[i] : min; if (i > 0) { int tssum = 0; /* temporary smallest sum */ /* test adjacent values { if adjacent: tssum += a[i]; { if no longer adjacent { compare tssum < ssum, if so ssum = tssum; } } } */ } } 

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.

+1
source share

All Articles