Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <algorithm>
- bool isPrime(int num) {
- if (num <= 1) {
- return false;
- }
- for (int i = 2; i <= num / 2; i++) {
- if (num % i == 0) {
- return false;
- }
- }
- return true;
- }
- int main() {
- // Open the file
- std::ifstream inputFile("/Users/jacobe/Desktop/plik.txt");
- if (!inputFile.is_open()) {
- std::cerr << "Failed to open file" << std::endl;
- return 1;
- }
- // Read the numbers into a vector
- std::vector<int> numbers;
- int num;
- while (inputFile >> num) {
- numbers.push_back(num);
- }
- // Calculate the sum and average
- int sum = 0;
- for (int num : numbers) {
- sum += num;
- }
- double average = static_cast<double>(sum) / numbers.size();
- // Calculate the median
- std::sort(numbers.begin(), numbers.end());
- int median;
- if (numbers.size() % 2 == 0) {
- median = (numbers[numbers.size() / 2 - 1] + numbers[numbers.size() / 2]) / 2;
- } else {
- median = numbers[numbers.size() / 2];
- }
- int numPositives = 0;
- int numNegatives = 0;
- for (int num : numbers) {
- if (num > 0) {
- numPositives++;
- } else if (num < 0) {
- numNegatives++;
- }
- }
- std::cout << "Suma: " << sum << std::endl;
- std::cout << "Średnia: " << average << std::endl;
- std::cout << "Mediana: " << median << std::endl;
- std::cout << "Liczby pozytywne: " << numPositives << std::endl;
- std::cout << "Liczby negatywne: " << numNegatives << std::endl;
- std::cout << "Liczby pierwsze: ";
- for (int num : numbers) {
- if (isPrime(num)) {
- std::cout << num << " ";
- }
- }
- std::cout << std::endl;
- inputFile.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement