Good day.

There is a one-dimensional array of 10 elements (positive and negative) that are generated randomly.

It is required to find the product of identical negative elements, i.e. check the elements for negativity, get the values ​​of the elements that are repeated 2 or more times, build this number in the number of repetitions of this number, output.

For example, an array: -1 2 -3 -5 -3 4 -3 -5 2 6

It turns out: -3 is repeated 3 times, -5 is repeated 2 times. It is necessary to calculate -3 ^ 3 and -5 ^ 2 and output: what number is repeated, how many times and the result (-27 and 25, respectively).

Initial array generation:

#include <stdio.h> #include <windows.h> #include <stdlib.h> #include <time.h> int main() { system("cls");; int ARR[10], i, j, num=0, P=1; srand(time(NULL)); printf("Array:\n"); for(i=0; i<10; i++) { ARR[i]=rand() % 11 - 7; printf("%i\t", ARR[i]); } /* */ } 

I studied several similar topics, but none of the codes worked correctly. Which way to dig?

Thank.

  • Well, the easiest option is in the direction of cycles =) Smarter - in the direction of sorting and / or trees. - pavel
  • just considered a variant with odds, right down to the ones invested, but it did not work - ALEVER
  • qsort - sorted. We go from the minimum. If there are identical negative ones next to each other - we consider, we deduce (just note that (-5) ^ 2 is 25, not -25 :)). Run out of negative numbers - stopped ... - Harry
  • it is forbidden to use additional functions, only cycles, but still, first sort the array, and then count the elements? - ALEVER
  • Most importantly, if there are several pairs of paired elements, then how to remember both the number and number of repetitions in each pair. - ALEVER

2 answers 2

If you use only cycles without changing the source array itself, then one of the approaches may look like this, as shown in the demo program.

 #include <stdio.h> #define N 10 int main(void) { int a[N] = { -1, 2, -3, -5, -3, 4, -3, -5, 2, 6 }; for ( size_t i = 0; i < N; i++ ) { if ( a[i] < 0 ) { size_t j = 0; while ( j < i && a[j] != a[i] ) j++; if ( j == i ) { size_t n = 0; for ( size_t k = i + 1; k < N; k++ ) { if ( a[k] == a[i] ) ++n; } if ( n != 0 ) { long long int product = a[i]; for ( size_t k = 0; k < n; k++ ) product *= a[i]; printf( "%d is repeated %zu times and the result is equal to %lld\n", a[i], n + 1, product ); } } } } return 0; } 

Output of the program to the console

 -3 is repeated 3 times and the result is equal to -27 -5 is repeated 2 times and the result is equal to 25 

    In PHP, this problem is solved quite simply.

     // Production of matched negative values $arr = [ -1, 2, -3, -5, -3, 4, -3, -5, 2, 6 ]; var_dump($arr); $arr_cnt = array_count_values($arr); var_dump($arr_cnt); foreach ($arr_cnt as $key => $value) { if(($key < 0) && ($value > 1)) { $power = abs($key) ** $value; if($value & 1) $power = - $power; echo "<br>($key)<sup>$value</sup> = $power"; } } 

    Results:

    enter image description here

    We will analyze the main points of this decision.

    1. The array_count_values() function counts the number of repetitions of a number in an array. For an array of 10 numbers, this can be done head-on, walking along with each negative number encountered in the array. To avoid repetitions, it is enough to change the numbers found by one
    2. In the given example, the modulus of the element was raised to the power, and the sign was calculated by checking for parity. But if we still go through the array, then we can count the degree through multiplication and not think about the sign.

    So:

     // Production of matched negative values $arr = [ -1, 2, -3, -5, -3, 4, -3, -5, 2, 6 ]; var_dump($arr); $b = $arr; $n = count($b); for($i = 0; $i < $n; $i++) { if($b[$i] < 0){ $k = $b[$i]; $pow = $k; $deg = 1; for($j= $i+1; $j < $n; $j++) { if ($b[$j] == $k) { $deg++; $b[$j] = 1; $pow *= $k; } } if($deg>1) echo "<br>($k)<sup>$deg</sup> = $pow"; } } 

    with the same result:

    enter image description here

    I think that the translation of the second variant into C will not cause difficulties.