daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest May 16th, 2018 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <vector>
  4. #include <ctime>
  5.  
  6. int main() {
  7.     rand(time(NULL));
  8.  
  9.     int values[20];
  10.  
  11.     // Add random values to the array
  12.     for (int i = 0; i < 20; i++) {
  13.         values[i] = rand() % 100;
  14.     }
  15.  
  16.     int evenSum = 0; // suma wyrazow parzystych
  17.     int oddSum = 0; // suma wyrazow nieparzystych
  18.  
  19.     int evenIndexSum = 0; // suma wyrazow o parzystych numerach (indexach)
  20.     int oddIndexSum = 0; // suma wyrazow o nieparzystych numerach (indexach)
  21.  
  22.     int betweenSum1 = 0; // suma wyrazow o numerach (indexach) od 1 do n/2
  23.     int betweenSum2 = 0; // suma wyrazow o numerach (indexach) od (n / 2) + 1 do n
  24.  
  25.     int n = 20;
  26.  
  27.     for (int i = 0; i < 20; i++) {
  28.         if (values[i] %2 == 0) {
  29.             evenSum += values[i];
  30.         } else {
  31.             oddSum += values[i];
  32.         }
  33.  
  34.         if (i % 2 == 0) {
  35.             evenIndexSum += values[i];
  36.         } else {
  37.             oddIndexSum += values[i];
  38.         }
  39.  
  40.         if (i > 0 && i < n / 2) {
  41.             betweenSum1 += values[i];
  42.         }
  43.  
  44.         if (i > n / 2 + 1 && i < n) {
  45.             betweenSum2 += values[i];
  46.         }
  47.     }
  48.  
  49.     for (int i = 0; i < 20; i++) {
  50.         std::cout << values[i];
  51.  
  52.         if (i < 19) std::cout << ", ";
  53.         else std::cout << std::endl << std::endl;
  54.     }
  55.  
  56.     std::cout << "Suma wyrazow parzystych: " << evenSum << std::endl;
  57.     std::cout << "Suma wyrazow nieparzystych: " << oddSum << std::endl;
  58.  
  59.     std::cout << "Suma wyrazow o parzystych numerach: " << evenIndexSum << std::endl;
  60.     std::cout << "Suma wyrazow o nieparzystych numerach: " << oddIndexSum << std::endl;
  61.  
  62.     std::cout << "Suma wyrazow o numerach od 1 do n/ 2: " << betweenSum1 << std::endl;
  63.     std::cout << "Suma wyrazow o numerach od (n / 2) + 1 do n: " << betweenSum2 << std::endl;
  64.  
  65.     return 0;
  66. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top