Laborator7 Problema9

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include <stdio.

h>
#define DIM 20

float valMed(int x[], int dim);


float valMedPozitive(int x[], int dim);
float valMedNegative(int x[], int dim);

int main(void) {
int i, dim;
int x[DIM];

printf("\nIntroduceti dimensiunea tabloului: ");


scanf("%d", &dim);

if (dim > DIM) {


printf("\n Dimensiune prea mare !");
return 0;
}

printf("\n Introduceti elementele tabloului:\n");

for (i = 0; i < dim; i++) {


printf("\tx[%d] = ", i);
scanf("%d", &x[i]);
}

printf("\n valoarea medie este: %.3f\n", valMed(x, dim));


printf("\n valoarea medie a valorilor pozitive este: %.3f\n", valMedPozitive(x,
dim));
printf("\n valoarea medie a valorilor negative este: %.3f\n", valMedNegative(x,
dim));

return 0;
}
float valMed(int x[], int n) {
int i;
float sum = 0;

for (i = 0; i < n; i++)


sum += x[i];

return sum / n;
}
float valMedPozitive(int x[], int n) {
int i;
float sum = 0;
int numar_pozitive = 0;

for (i = 0; i < n; i++) {


if (x[i] > 0) {
sum += x[i];
numar_pozitive++;
}
}

return numar_pozitive > 0 ? sum / numar_pozitive : 0;


}
float valMedNegative(int x[], int n) {
int i;
float sum = 0;
int numar_negative = 0;

for (i = 0; i < n; i++) {


if (x[i] < 0) {
sum += x[i];
numar_negative++;
}
}

return numar_negative > 0 ? sum / numar_negative : 0;


}

You might also like