User Tools

Site Tools


cs:c_language:array_2
Return to Home page

Differences

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

Link to this comparison view

cs:c_language:array_2 [2019/02/26 14:35]
cs:c_language:array_2 [2019/02/26 14:35] (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:
 +<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};
 +</​code>​
 +the program must provide the following output:
 +<​code>​
 +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
 +</​code>​
 +
 +**Solution:​**\\
 +<file C array_2.c>​
 +/​*******************************************
 + 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 <​stdio.h>​
 +
 +
 +#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; i<​DIM_VETT;​ i++)
 +    printf("​%3d ", v[i]);
 +  printf("​\n"​);​
 +
 +  /* Print the position of the vector elements */
 +  printf("​POS : ");
 +  for(i=0; i<​DIM_VETT;​ i++)
 +    printf("​%3d ", i);
 +  printf("​\n"​);​
 +
 +  /* First solution */
 +  printf("​\nSOLUTION 1\n");
 +  i = 0;
 +  pos = 0;
 +  n_rep = 1;
 +
 +  for(i=0; i<​DIM_VETT-1;​ i++){
 +
 +    if(v[i]==v[i+1]){
 +      n_rep++;
 +    }else{
 +      if(n_rep>​1){
 +        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(i<​DIM_VETT-1){
 +    pos = i;
 +    n_rep = 1;
 + 
 +    j = i+1;
 +    while(v[i]==v[j] && j < DIM_VETT){
 +      n_rep++;
 +      j++;
 +    }
 +
 +    if(n_rep>​1){
 +      printf("​Value %d repeated %d times, starting form the position %d\n", v[i], n_rep, pos);
 +      i = j;
 +    }else{
 +      i++;
 +    }
 +  }
 +}
 +</​file>​

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%5B0%5D=&rev2%5B1%5D=1551188114&difftype=sidebyside
/web/htdocs/www.skenz.it/home/data/pages/cs/c_language/array_2.txt ยท Last modified: 2019/02/26 14:35 (external edit)

Privacy Policy