# Count ascending sequence

2

I have to do an exercise that receives numbers from the keyboard until I enter a 0 and show the number of ascending sequences (Example: 15, 9, 5, 3, 4, 6 has only 1 sequence). I did it like this:

``````int i;
int a[50];
int ascendente;
printf ("Ingrese números. Presione 0 para salir.\n");
while (a[i]!=0){
for (i=0;i<50;i++){
scanf ("%d", &a[i]);
if (a[i]<a[i+1])
ascendente++;
}
}
printf ("Hay %d cantidad de secuencias ascendentes.\n", ascendente);
``````

In that case, compare only two elements of the sequence. I do not know what to do to consider an ascending sequence until a lower number is entered.

source

2

Your combo `while` / `for` will keep the user within the cycle until you write a zero by keyboard and will eventually overwrite the array you define with 50 indexes.

The main idea of your algorithm does not differ so much from an algorithm to order arrays, so the initial solution is to have the whole data set before working on it:

``````#include <stdio.h>
#define MAX_ARR (50)

int main(void) {
int Arr[MAX_ARR], Arrlen = 0, cant = 0, Asc = 0;
printf("Llenar array (0 para terminar): \n");
for (Arrlen = 0; Arrlen < MAX_ARR; Arrlen++) {
printf("A[%d]: ", Arrlen + 1);

scanf(" %d", (Arr + Arrlen));
if (!Arr[Arrlen])
break;
}

for (int i = 0; Arrlen && (i < Arrlen); i++) {
if (Arr[i] < Arr[i + 1])
cant++;
else {
if (cant) Asc++;
cant = 0;
}
}

return 0;
}
``````

Previously you tried to do everything within the same cycle, which is possible in a way that is too tedious and that probably confuses any other developer, this solution tries to separate both jobs:

• Fill the array.
• Operate in the array.

If you notice there are two `for` , the first is necessary to enter the values, the second is to compare all the data as long as there are elements.

I have tried the program above and it gives the following results:

``````Llenar array (0 para terminar):
A[1]:  1
A[2]:  2
A[3]:  1
A[4]:  2
A[5]:  1
A[6]:  2
A[7]:  1
A[8]:  2
A[9]:  0

Llenar array (0 para terminar):
A[1]:  1
A[2]:  2
A[3]:  3
A[4]:  4
A[5]:  5
A[6]:  6
A[7]:  0

Llenar array (0 para terminar):
A[1]:  1
A[2]:  2
A[3]:  4
A[4]:  1
A[5]:  3
A[6]:  5
A[7]:  2
A[8]:  0