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];
- }
- // Count the positive and negative numbers
- int numPositives = 0;
- int numNegatives = 0;
- for (int num : numbers) {
- if (num > 0) {
- numPositives++;
- } else if (num < 0) {
- numNegatives++;
- }
- }
- // Print the results
- std::cout << "Sum: " << sum << std::endl;
- std::cout << "Average: " << average << std::endl;
- std::cout << "Median: " << median << std::endl;
- std::cout << "Number of positive numbers: " << numPositives << std::endl;
- std::cout << "Number of negative numbers: " << numNegatives << std::endl;
- std::cout << "Prime numbers: ";
- for (int num : numbers) {
- if (isPrime(num)) {
- std::cout << num << " ";
- }
- }
- std::cout << std::endl;
- // Close the file
- inputFile.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement