0
#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
using namespace std;

typedef long long int ll;

int main()
{
    istream in("input.txt", "r", stdin);
    ofstream out("output.txt", "w", stdout);
    int t;
    in >> t;
    while (t--)
    {
        ll n;
        in >> n;
        ll arr[n];
        for (int i = 0; i < n; i++)
        {
            in >> arr[i];
        }
        sort(arr, arr + n);
        double sum = 0;
        for (int i = 0; i < n - 1; i++)
        {
            sum += arr[i];
        }
        double ans = (sum / (n - 1)) + arr[n - 1];
        out << setprecision(9) << ans << endl;
    }
    return 0;
}

Note that I also changed my tasks.json file.

I changed my code according to what @john and @PepjinKramer said, but my inputs are not being read from the input.txt file and so nothing is shown in output.txt.

How do I solve this problem?

V.G
  • 103
  • 4
  • @john If I change `cin` to `in` and `cout` to `out` , and when I pass the inputs in my terminal I get a 'ParserError' , 'Unexpected token ' ' in expression or statement'. And still the code is not being read from `input.txt` file and shown in `output.txt` file. – V.G Jan 07 '23 at 11:43
  • 1
    @V.G `istream in("input.txt", "r", stdin);` should be `istream in("input.txt");` ditto for `output.txt` – john Jan 07 '23 at 12:15
  • 1
    [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Jesper Juhl Jan 07 '23 at 14:16
  • 1
    [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Jesper Juhl Jan 07 '23 at 14:17
  • 1
    `typedef long long int ll;` - No, no, no, please don't do crap like that. Just use the proper type names. – Jesper Juhl Jan 07 '23 at 14:18

1 Answers1

-1

Some feedback to show how to reduce the "cargo cult" content of much C++ code out there :

// #include <bits/stdc++.h> <== NO not standard C++
#include <algorithm> // for sort
#include <iostream>
#include <fstream>
#include <vector> // for resizable arrays
#include <numeric> // for accumulate
#include <iomanip> // for setprecission

// using namespace std; <== NO will become a problem in big projects (nameclashes0

// typedef long long int ll; <== NO just use standard types in code e.g. std::size_t or std::int64_t

int main()
{
    //istream in("input.txt", "r", stdin);      // reading from file doesn't need anything from stdin
    //ofstream out("output.txt", "w", stdout);  // writing to a file doesn't need anything from stdout

    // std::ifstream input{ "input.txt" };
    // std::ofstream output{ "output.txt" };

    // for testing manual input
    auto& input = std::cin;
    auto& output = std::cout;
    
    std::size_t number_of_averages; // give variables more meaningful names then just 't'

    std::cout << "input number of averages to calculate = ";
    input >> number_of_averages;

    for(std::size_t n = 0; n < number_of_averages; ++n )
    {
        //ll n; NO sizes of containters should be std::size_t not long long
        //ll n;
        //in >> n;

        std::size_t number_of_values;
        std::cout << "input number of values = ";
        input >> number_of_values;

        // ll arr[n]; // NO C++ doesn NOT have variable length arrays, use std::vector
        std::vector<double> values(number_of_values);

        // for (int i = 0; i < n; i++) // Do NOT use index based loops if you don't have to (out of bound bugs)
        //{
        // use range based for loops and learn about (const) references
        for(auto& value : values)
        {
            std::cout << "input number : ";
            input >> value;
        }

        // sort(arr, arr + n); // No use iteraters
        std::sort(values.begin(), values.end());

        // NO for summing collections use <algorithm> accumulate
        /*
        double sum = 0;
        for (int i = 0; i < n - 1; i++)
        {
            sum += arr[i];
        }
        */
        auto sum = std::accumulate(values.begin(), values.end(), 0.0);
        auto average = sum / static_cast<double>(values.size());

        std::cout << "average = ";
        output << std::setprecision(9) << average << std::endl;
    }
    return 0;

}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19