Home > Blockchain >  sorting int from a text file in an ascending order
sorting int from a text file in an ascending order

Time:03-20

So i have a text file that looks like this:

5
4
9
-3
0
2

first int shows how many int their are that need to be ordered in ascending order so the output looks like this:

-3
0
2
4
9

Im trying to do it without any additional libraries and just use

#include <iostream>
#include <fstream>

So far my code reads the first int but when want it to read the rest it doesnt work. Im still stuck at the reading part.

int main()
{
   ifstream input;
   int a,N,i,j;

    int temp;
   // Open the file.
   input.open("test.txt");
    if (!input) {
        cerr << "Could not open the file - '"<< endl;
        return EXIT_FAILURE;
    }
   // Read the numbers of int from the file 
   cout<<"Input fail data"<<endl;
   input>>N;
   cout<<"integers: "<<N<<endl;

    int data[N];
//
   for(i = 0; i << N; i  ){
        input>>data[i];

P.S. Shoud the rest of the integers be in an array or a linked list?

CodePudding user response:

Standard C doesn't not support VLA:s (variable length arrays). Use a std::vector<int> instead.

Example:

#include <algorithm> // copy_n
#include <cerrno>    // errno
#include <cstring>   // strerror
#include <fstream>
#include <iostream>
#include <iterator>  // istream_iterator
#include <vector>    // vector instead of VLA

int main() {
   std::ifstream input("test.txt");
   if (!input) {
       std::cerr << "Could not open the file: " << std::strerror(errno) << '\n';
       return EXIT_FAILURE;
   }

   // Read the numbers of int from the file
   int N;
   if (!(input >> N && N > 0)) {
       std::cerr << "invalid number of elements\n";
       return EXIT_FAILURE;
   }

   std::vector<int> data(N); // not int data[N];

   // copy ints from `input` to `data`:
   std::copy_n(std::istream_iterator<int>(input), data.size(), data.begin());
   // or:
   // for(int& val : data) std::cin >> val;

   std::cout << "integers: " << data.size() << '\n';

   // sort them
   std::sort(data.begin(), data.end());

   for (int val : data) std::cout << val << '\n';
}

If you'd like to do it without the support of std::vector etc., you could make a simple container class yourself.

Example:

#include <fstream>
#include <iostream>

template <class T>
class arr {
public:
    using value_type = T;
    using const_iterator = const value_type*;
    using iterator = value_type*;
    using const_reference = const value_type&;
    using reference = value_type&;

    arr(size_t x) : data(new T[x]), len(x), capacity(x) {}

    // copy ctor
    arr(const arr& rhs) : arr(rhs.x) {
        for (size_t i = 0; i < len;   i) data[i] = rhs.data[i];
    }

    // move ctor
    arr(arr&& rhs) : data(rhs.data), len(rhs.len), capacity(rhs.capacity) {
        rhs.data = nullptr;
    }

    // copy assignment operator
    arr& operator=(const arr& rhs) {
        if(this == &rhs) return *this;
        if(capacity < rhs.len) {
            delete[]data;
            data = new T[rhs.len];
            capacity = rhs.len;
        }
        len = rhs.len;
        for (size_t i = 0; i < len;   i) data[i] = rhs.data[i];
        return *this;
    }
    // move assignment operator:
    arr& operator=(arr&& rhs) {
        delete[] data;
        data = rhs.data;
        rhs.data = nullptr;
        len = rhs.len;
        capacity = rhs.capacity;
        return *this;
    }
    
    // destructor
    ~arr() { delete[] data; }

    size_t size() const { return len; }

    const_reference operator[](size_t idx) const { return data[idx]; }
    reference operator[](size_t idx) { return data[idx]; }

    const_iterator cbegin() const { return data; }
    const_iterator cend() const { return data   len; }
    const_iterator begin() const { return cbegin(); }
    const_iterator end() const { return cend(); }
    iterator begin() { return data; }
    iterator end() { return data   len; }

private:
    T* data;
    size_t len, capacity;
};

int main() {
    std::ifstream input("test.txt");
    if (!input) {
        std::cerr << "Could not open the file\n";
        return EXIT_FAILURE;
    }

    // Read the numbers of int from the file
    int N;
    if (!(input >> N && N > 0)) {
        std::cerr << "invalid number of elements\n";
        return EXIT_FAILURE;
    }

    arr<int> data(N);

    for (int& val : data) std::cin >> val;

    std::cout << "integers: " << N << '\n';

    // implement your own sorting algorithm and use it here

    for (int val : data) std::cout << val << '\n';
}

CodePudding user response:

So, I tried your code and it worked after some minor changes, which only address the ability for compilation but not the actual approach to read the data.

#include <iostream>
#include <fstream>

int main()
{
    std::ifstream input;
    input.open("test.txt");
    if (!input) {
        std::cerr << "Could not open the file - '"<< std::endl;
        return -1;
    }

    // Read the numbers of int from the file 
    int N;
    input >> N;
    std::cout << "reading " << N << " integers: " << std::endl;

    int* data = new int[N];
    for(int i = 0; i < N; i  ){
        input >> data[i];
        std::cout << data[i] << std::endl;
    }
    delete data;
}

Maybe there is something wrong in your code beyond the reading part.

  •  Tags:  
  • c
  • Related