gashink_t

рекурсия (умма отрицательных чисел расп на нечетных позиция)

Feb 11th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #define N 50
  5. int* newmass(int n) { //cоздает и заполняет массив случайными числами
  6.     int i;
  7.     int *A=(int*)malloc(n*sizeof(int));
  8.     for (i=0;i<n;i++){
  9.         A[i]=rand()%10-5;
  10.         }
  11.         return A;
  12. }
  13. int vivod(int A[N], int n) { //вывода маccива на экран
  14.     int i;
  15.     for (i=0; i<n; i++) {
  16.         printf("%4d", A[i]);
  17.                 }
  18.         printf("\n");
  19.         return 0;
  20. }
  21. int suma(int A[N], int i, int n) { //сумма отрицательных чисел расп на нечетных позициях массива
  22.         if (i<n) {
  23.          if (A[i]<0) {
  24.             return A[i]+suma(A,i+2,n);
  25.                 }
  26.          else return suma(A,i+2,n);
  27.         }
  28.     else return 0;
  29. }
  30.  
  31. int main() {
  32.     int n;
  33.     printf("vvedite razmer massiva n: \n");
  34.     scanf("%d", &n);
  35.     int* a=newmass(n);
  36.     printf("vash massiv: \n");
  37.     vivod(a,n);
  38.     printf("suma = %d\n", suma(a,1,n));
  39.     return 0;
  40. }
Add Comment
Please, Sign In to add comment