Skip to content

Instantly share code, notes, and snippets.

@Aliath
Created January 12, 2020 18:17
Show Gist options
  • Save Aliath/2f800276352885deca10217ce55d02d8 to your computer and use it in GitHub Desktop.
Save Aliath/2f800276352885deca10217ce55d02d8 to your computer and use it in GitHub Desktop.
WDP - zaliczenie do laborków
#include <stdio.h>
#include <stdlib.h>
#define TAB_LENGTH 8
// zadanie 4
double max_value(double tab[]) {
double result = -1;
for (int i = 0; i < TAB_LENGTH; i++) {
double current_value = tab[i];
if (current_value > 0 && current_value > result) {
result = current_value;
}
}
if (result == -1) {
printf("\n nie znaleziono dodatniego elementu w tablicy\n");
}
return result;
}
// zadanie 5
double average(double tab[]) {
double current_sum = 0;
int current_elements = 0;
for (int i = 0; i < TAB_LENGTH; i++) {
double current_value = tab[i];
if ((int)current_value % 2 == 0) {
current_sum += current_value;
current_elements ++;
}
}
return current_sum / (double) current_elements;
}
// zadanie 6
int amount_of_specified(double tab[]) {
int result = 0;
for (int i = 0; i < TAB_LENGTH; i++) {
double current_value = tab[i];
if (current_value > 5 && (int)current_value % 2 == 0) {
result ++;
}
}
return result;
}
// zadanie 7
double *add_to_all(double tab[], double value) {
static double result[TAB_LENGTH];
for (int i = 0; i < TAB_LENGTH; i++) {
double current_value = tab[i];
result[i] = current_value + value;
}
return result;
}
// funkcja pomocnicza
void dump(double tab[]) {
for (int i = 0; i < TAB_LENGTH; i++) {
printf("\ttab[%d] = %lf\n", i, tab[i]);
}
}
int main()
{
printf("1. Deklaracja tablicy\n");
double tab[TAB_LENGTH];
printf("\n====================================\n");
printf("2. Wczytanie wartosci\n");
for (int i = 0; i < TAB_LENGTH; i++) {
printf("\tPodaj wartosc dla tab[%d]: ", i);
scanf("%lf", &tab[i]);
}
printf("\n====================================\n");
printf("3. Wyswietlic wczytane dane: \n");
dump(tab);
printf("\n====================================\n");
printf("4. Najwiekszy element w tablicy: %lf\n", max_value(tab));
printf("5. Srednia parzystych elementow w tablicy: %lf\n", average(tab));
printf("6. Liczba elementow wiekszych od 5, podzielnych przez 2: %d\n", amount_of_specified(tab));
printf("7. Dodanie okreslonej wartosci do wszystkich elementow w tablicy: \n");
double* result = add_to_all(tab, 4.5);
dump(result);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment