Facebook
From Kuba Kawalerski, 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 88
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void licz(int i, int start, int krok, int suma, int iloczyn, int iloscElementow, unsigned int elementy[]) {
  5.  
  6.         int index = (start + i * krok);
  7.  
  8.         if (index >= iloscElementow) {
  9.                 return;
  10.         }
  11.  
  12.         printf("\n");
  13.         printf("Index: %d, Wartosc elementu: %d", index, elementy[index]);
  14.  
  15.         suma += elementy[index];
  16.         iloczyn *= elementy[index];
  17.         printf("\n");
  18.         printf("Suma: %d, Iloczyn: %d", suma, iloczyn);
  19.  
  20.         licz(i + 1, start, krok, suma, iloczyn, iloscElementow, elementy);
  21.  
  22. }
  23.  
  24. void wczytajElementy(int i, int iloscElementow, unsigned int elementy[]) {
  25.  
  26.         if (i == iloscElementow) {
  27.                 int start, krok;
  28.                 printf("Podaj liczbę start z zakresu 0-1000: ");
  29.                 scanf_s (" %d", &start);
  30.  
  31.                 if (start < 0 || start > 1000) {
  32.                         printf("Podałeś element start z poza zakresu!");
  33.                         exit(0);
  34.                 }
  35.  
  36.                 printf("Podaj liczbę kroku z zakresu 1-1000: ");
  37.                 scanf_s (" %d", &krok);
  38.  
  39.                 if (krok < 1 || krok > 1000) {
  40.                         printf("Podałeś element krok z poza zakresu!");
  41.                         exit(0);
  42.                 }
  43.  
  44.                 printf("Obliczenie sumy oraz iloczynu elementów o id:");
  45.                 licz(0, start, krok, 0, 1, iloscElementow, elementy);
  46.  
  47.                 return;
  48.         }
  49.  
  50.         int t = 0;
  51.         printf("Podaj %d element z zakresu 1-1000: ", i);
  52.         scanf_s (" %d", &t);
  53.  
  54.         if (t < 1 || iloscElementow > 1000) {
  55.                 printf("Podałeś element z poza zakresu!");
  56.                 exit(0);
  57.         }
  58.  
  59.         elementy[i] = t;
  60.         wczytajElementy(i + 1, iloscElementow, elementy);
  61. }
  62.  
  63.  
  64.  
  65. int main() {
  66.  
  67.         unsigned int iloscElementow = 0;
  68.         unsigned int* elementy = new unsigned int[iloscElementow];
  69.  
  70.         printf("Podaj ile liczb chcesz wprowadzic: ");
  71.         scanf_s (" %d", &iloscElementow);
  72.  
  73.         wczytajElementy(0, iloscElementow, elementy);
  74.  
  75.         return 0;
  76. }