Hello
I kinda stuck right now
I need to make a program in C with those instructions: the problem is its kinda easy to do with arrays but without them,I cant think anything else.
we need to write a function that takes int n and then takes the sequences of numbers, the function need to return us the sum of the biggest sequence. for n = 8 and the sequence = 2,4,6,8,11,14,17,9' (there is 2 long sequences of 4, (when 8 starts the new sequence and closes the current sequence) here we will get the print 50.`
for n = 2 and the sequence 7,19 the function will print 26. for n = 8 and the sequence 8,6,4,2,11,14,15,16 the longest length is 4 so the function will print 20.
can't find any solution here without arrays
here what i did so far but its not working
can you suggest me something?
I kinda stuck right now
I need to make a program in C with those instructions: the problem is its kinda easy to do with arrays but without them,I cant think anything else.
we need to write a function that takes int n and then takes the sequences of numbers, the function need to return us the sum of the biggest sequence. for n = 8 and the sequence = 2,4,6,8,11,14,17,9' (there is 2 long sequences of 4, (when 8 starts the new sequence and closes the current sequence) here we will get the print 50.`
for n = 2 and the sequence 7,19 the function will print 26. for n = 8 and the sequence 8,6,4,2,11,14,15,16 the longest length is 4 so the function will print 20.
can't find any solution here without arrays
here what i did so far but its not working
C:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int
main(void)
{
int d1, d2;
// get count of sequence numbers
int n;
scanf("%d", &n);
// length of current sequence
int curlen = 0;
// sum of current sequence
int cursum = 0;
// value of current number
int curval;
// value of previous number
int oldval;
// get the first number
scanf("%d", &oldval);
// add it to the current sequence
curlen += 1;
cursum += oldval;
// initialize the best sequence from the current one
int bestsum = cursum;
int bestlen = curlen;
// get all remaining numbers in the sequence
for (int i = 1; i < n; ++i, oldval = curval) {
// get new current number
if (scanf("%d", &curval) != 1) {
fprintf(stderr, "too short\n");
break;
}
d1 = curval - oldval;
scanf("%d", &oldval);
d2 = oldval - curval;
// reset the current sequence if we're not increasing
if (d1 != d2)
{
cursum = 0;
curlen = 0;
}
// add to the current sequence
cursum += curval;
curlen += 1;
// save off the longest sequence we've seen
if (curlen > bestlen) {
bestlen = curlen;
bestsum = cursum;
continue;
}
// save off a sequence that is the same length as the best but has a
// larger sum
if ((curlen == bestlen) && (cursum > bestsum)) {
bestsum = cursum;
continue;
}
}
// print the final/best sum
printf("%d\n", bestsum);
return 0;
}
can you suggest me something?