cs:c_language:array_2
Return to Home page
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/c_language/array_2?do=diff&rev2%255B0%255D=&rev2%255B1%255D=1551188114&difftype=sidebyside
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | cs:c_language:array_2 [2024/04/08 22:35] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Array (Example 2) ====== | ||
+ | **Concepts: | ||
+ | Arrays, initialization and utilization | ||
+ | **Text:**\\ | ||
+ | Implement a C program that: | ||
+ | * given an array of dimension '' | ||
+ | * 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: | ||
+ | <code C> | ||
+ | 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: | ||
+ | <file C array_2.c> | ||
+ | / | ||
+ | Given the vector, for instance: | ||
+ | | ||
+ | whose elements are in the positions | ||
+ | POS : | ||
+ | |||
+ | 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(" | ||
+ | for(i=0; i< | ||
+ | printf(" | ||
+ | printf(" | ||
+ | |||
+ | /* Print the position of the vector elements */ | ||
+ | printf(" | ||
+ | for(i=0; i< | ||
+ | printf(" | ||
+ | printf(" | ||
+ | |||
+ | /* First solution */ | ||
+ | printf(" | ||
+ | i = 0; | ||
+ | pos = 0; | ||
+ | n_rep = 1; | ||
+ | |||
+ | for(i=0; i< | ||
+ | |||
+ | if(v[i]==v[i+1]){ | ||
+ | n_rep++; | ||
+ | }else{ | ||
+ | if(n_rep> | ||
+ | printf(" | ||
+ | } | ||
+ | n_rep = 1; | ||
+ | pos = i+1; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if(n_rep> | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | /* Second solution */ | ||
+ | printf(" | ||
+ | i=0; | ||
+ | while(i< | ||
+ | pos = i; | ||
+ | n_rep = 1; | ||
+ | |||
+ | j = i+1; | ||
+ | while(v[i]==v[j] && j < DIM_VETT){ | ||
+ | n_rep++; | ||
+ | j++; | ||
+ | } | ||
+ | |||
+ | if(n_rep> | ||
+ | printf(" | ||
+ | i = j; | ||
+ | }else{ | ||
+ | i++; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ |
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/c_language/array_2?do=diff&rev2%255B0%255D=&rev2%255B1%255D=1551188114&difftype=sidebyside
/web/htdocs/www.skenz.it/home/data/pages/cs/c_language/array_2.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1