Advertisement
Marcel12311

#C++ |dodaj,podziel i sumuj(tablica)|

Apr 10th, 2021
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h> // srand() i rand()
  3. using namespace std;
  4. void pokaz(float tab[],const int n){
  5.     cout<< "wyswietlamy wynik:\n";
  6.     for(int i=0;i<n;i++){
  7.         cout<<tab[i]<<" ";
  8.     }cout<<endl;
  9. }
  10. int dodaj(float tab[],const int n){
  11.     int a=0;
  12.     int b=0;
  13.     cout<<"wyswietlamy losowe liczby i dodajemy je:\n";
  14.     for(int i=0;i<n;i++){
  15.         a=rand()%10+1;
  16.         b=rand()%20+1;
  17.         cout << a <<" "<<b<<endl;
  18.         tab[i]=a+b;
  19.     }
  20.     return* tab;
  21. }
  22. int podziel(float tab[],const int n){
  23.     cout << "dzielimy dodane przez siebie liczby:\n";
  24.     for(int i=0;i<n;i++){
  25.         tab[i]/=2;
  26.     }
  27.     return* tab;
  28. }
  29. int suma(float tab[],float& wynik,const int n){
  30.     // uzylem typ float ze wzgledu na dokladnosc obliczeniowa
  31.     cout<<"Sumujemy wszystkie liczby i zapisaujemy wynik:\n";
  32.     for(int i=0;i<n;i++){
  33.         wynik+=tab[i];
  34.     }
  35.     return wynik;
  36. }
  37. int main(){
  38.     srand(time(NULL));
  39.     int n=10;
  40.     float tab[n];
  41.     float wynik = 0;
  42.  
  43.     dodaj(tab,n);
  44.     pokaz(tab,n);
  45.     cout<<endl;
  46.  
  47.     podziel(tab,n);
  48.     pokaz(tab,n);
  49.     cout<<endl;
  50.  
  51.     suma(tab,wynik,n);
  52.     cout << wynik;
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement