Given an integer array of 30 elements. Describe the algorithm that finds and displays the sum of the largest along the length of the increasing sequence of consecutive elements. If there are several such sequences, you can output any of them.

Here is my code:

#include <stdio.h> main(){ int dig[10]; int i,sum,dlina,dlinamax,maxsum; for (i = 0 ; i <= 4 ;i++){ scanf("%d",&dig[i]); } for ( i = 0 ; i <= 9 ; i++){ dlina = 1; sum = dig[i]; if (dig[i] < dig[i + 1]){ sum += dig[i + 1]; dlina++; } if (dlina > dlinamax){ dlinamax = dlina; maxsum = sum; } } printf("Сумма самой длинной возрастающей последовательности: %d\n",maxsum); } 

Corrected:

 #include <stdio.h> main(){ int dig[10]; int i,sum,dlina,dlinamax,maxsum; for (i = 0 ; i <= 9 ;i++){ scanf("%d",&dig[i]); } maxsum = 0; dlinamax = 0; for ( i = 0 ; i <= 9 ; i++){ dlina = 1; sum = dig[i]; if (dig[i] > dig[i - 1]){ sum += dig[i]; dlina++; } if (dlina > dlinamax){ dlinamax = dlina; maxsum = sum; } } printf("Сумма самой длинной возрастающей последовательности: %d\n",maxsum); } 

now constantly writes 4

  • one
    You go beyond the bounds of the array in a loop, and you read 5 elements, and you run 10. The remaining 5 are what was in memory before that (garbage) - SergeyTsaplin
  • I can not figure out how to fix it. - rolton
  • Very simple: do not go beyond the border of the array in a loop, and read not 5, but 10 elements. Which item raises questions? - VladD
  • And where did I go beyond the bounds of the array? - rolton
  • 2
    First, you read only 5 digits, not 10. Secondly, you have a completely wrong algorithm. This problem is solved by means of dynamic programming. Read [article] [1], in which it is written. [1]: e-maxx.ru/algo/longest_increasing_subseq_log - fori1ton

2 answers 2

Something like that:

 #include <stdio.h> void main(){ int dig[10]; int i,sum,dlina,dlinamax,maxsum; for (i = 0 ; i <= 9 ;i++){ scanf("%d",&dig[i]); } maxsum = dig[0]; dlinamax = 0; dlina = 1; for ( i = 1 ; i <= 9 ; i++){ if (dig[i] <= dig[i-1]){ if (dlina > dlinamax) { dlinamax = dlina; maxsum = sum; } sum = dig[i]; dlina = 1; } else { sum += dig[i]; dlina++; } } printf("Сумма самой длинной возрастающей последовательности: %d\n",maxsum); } 

    The first thing you do only for the first 5 elements enter only the first 5 numbers

      for (i = 0 ; i <= 4 ;i++){ scanf("%d",&dig[i]); } 

    But further

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

    you run up to the 10th element, but 6-7-8-9-10 are not, you did not enter them where.

    If I correctly understand the syntax C, then the error in the second cycle!

    Well, it would be good to make mistakes that arise when working with the program!

    // upd

    the very first

     if (dig[i] > dig[i - 1]){ 

    not honored to perform, because you have an array from 0-9, and you ask him to find out about the -1 element of the array - which is wrong!

    and secondly thick.

     for ( i = 0 ; i <= 9 ; i++){ dlina = 1; sum = dig[i]; if (dig[i] > dig[i - 1]){ sum += dig[i]; dlina++; } if (dlina > dlinamax){ dlinamax = dlina; maxsum = sum; } } 

    What is the general logic of this code? He does rubbish, take a piece of paper and write all the actions with your hand. I read the code and realized that I didn’t want to think further. Here you need to change the logic of the program, crookedly written. And the fact that displays 4k is quite natural. (take a pen and count)