# How To Wiki

### Site Tools

cs:c_language:array_2

# Differences

This shows you the differences between two versions of the page.

 cs:c_language:array_2 [2019/02/26 14:35] cs:c_language:array_2 [2020/11/26 23:18] (current) Line 1: Line 1: + ====== Array (Example 2) ====== + **Concepts:**\\ + Arrays, initialization and utilization + **Text:**\\ + Implement a C program that: + * given an array of dimension ''DIM_VETT'' equal to 20 and filled with integer numbers. + * find the equal adjacent values in the array that are repeated more than one time, indicating the first element of the repetition and the number of adjacent repeated elements. + + As an example, given the array: + + int v[DIM_VETT] = {2, 2, 3, 3, 3, 4, 2, 2, 4, 3, 3, 3, 3, 5, 5, 6, 1, 2, 3, 3}; + + the program must provide the following output: + + Value 2 repeated 2 times, starting form the position 0 + Value 3 repeated 3 times, starting form the position 2 + Value 2 repeated 2 times, starting form the position 6 + Value 3 repeated 4 times, starting form the position 9 + Value 5 repeated 2 times, starting form the position 13 + Value 3 repeated 2 times, starting form the position 18 + + + **Solution:**\\ + + /******************************************* + Given the vector, for instance: + v:      2   2   3   3   3   4   2   2   4   3   3   3   3   5   5   6   1   2   3   3 + whose elements are in the positions + POS :   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19 + + find the equal adjacent values that are repeated more than one time + in the vector, indicating the first element of the repetition + and the number of adjacent repeated elements. + + In the case of the array of the example, + the output of the program should be: + + Value 2 repeated 2 times, starting form the position 0 + Value 3 repeated 3 times, starting form the position 2 + Value 2 repeated 2 times, starting form the position 6 + Value 3 repeated 4 times, starting form the position 9 + Value 5 repeated 2 times, starting form the position 13 + Value 3 repeated 2 times, starting form the position 18 + + *******************************************/ + + #include + + + #define DIM_VETT 20 + + int main(){ + + int v[DIM_VETT] = {2, 2, 3, 3, 3, 4, 2, 2, 4, 3, 3, 3, 3, 5, 5, 6, 1, 2, 3, 3}; + int i, j, n_rep, pos; + + /* Print the array */ + printf("VETT: "); + for(i=0; i1){ + printf("Value %d repeated %d times, starting form the position %d\n", v[i], n_rep, pos); + } + n_rep = 1; + pos = i+1; + } + } + + if(n_rep>1){ + printf("Value %d repeated %d times, starting form the position %d\n", v[i], n_rep, pos); + } + + /* Second solution */ + printf("\nSOLUTION 2\n"); + i=0; + while(i1){ + printf("Value %d repeated %d times, starting form the position %d\n", v[i], n_rep, pos); + i = j; + }else{ + i++; + } + } + } +