-2

I am trying to make a program that reads files with numbers and sort the numbers with different algorithms and there is multiple files that are going to be read and each file has a different amount of integers. so what i need to do is read those integers and cast them into a array.

but for some reason in c++ you cant have a array with an undefined size so what is a solution that i can use? And i can't use vectors (school project)

Here is my program

#ifndef SORT_H
#define SORT_H
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


// =============================================================================
// FILE READER
// =============================================================================
void fileReader(string fileName, int arr[]){

    ifstream myfile(fileName);

    int pos = 0;
    string number;
    if(myfile.is_open()){
        while (getline(myfile, number))
        {
            arr[pos] = stoi(number);
            pos++;
        }
        
    }
}
// =============================================================================
//  SWAPER
// =============================================================================

void swap(int* a, int* b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

// =============================================================================
// QUICKSORT
// =============================================================================



int partition(int arr[], int beg, int end){

    int pivot = arr[end];
    int index = beg - 1;
    
    for(int j = beg; j < end; j++){
    
        if(arr[j] <= pivot){
        
            index++;
            swap(&arr[index], &arr[j]);
            
        }
        
    }
    
    swap(&arr[index+1], &arr[end]);
    
    return (index+1);
}

void quicksortAlgo(int arr[], int beg, int end){
    
    if(beg < end){
        
        int pi = partition(arr, beg, end);
        
        quicksortAlgo(arr, beg, pi-1);
        quicksortAlgo(arr, pi+1, end);
        
    }
    
}

template <typename T>
void quicksort(T arr[], int n)
{
    quicksortAlgo(arr, 0, n-1);

    for(int x = 0; x < n; x++)
        cout << arr[x] << endl;
}


// =============================================================================
// HEAPSORT
// =============================================================================

void heapify(int arr[], int n, int i){
    int max = i;
    int left = 2*i + 1; // Left side
    int right = 2*i + 2;    // Right side
    
    cout << "max: " << arr[max] << endl;


    // if there is a left child for root and if is
    // bigger then root
    if(left < n && arr[max] < arr[left]){
        max = left;
    }
    
    // If there is a right child of root and if is
    //  bigger then root
    if(right < n && arr[max] < arr[right]){
        max = right;
    }
    
    if(max != i){
        swap(&arr[i], &arr[max]);
        heapify(arr, n, max);
    }


}


void heapsortAlgo(int arr[], int n){

    
        for (int i = n/2 - 1; i >= 0; i--){
            heapify(arr, n, i);
        }
        for (int i = n-1; i >= 0; i--){
            swap(&arr[0], &arr[i]);
            heapify(arr, i, 0);
        }
}

template <typename T>
void heapsort(T arr[], int n)
{
    heapsortAlgo(arr, n);
    for (int i = 0; i < n; i++){
        cout << arr[i] << " ";
    }
}

// =============================================================================
// INTSERTIONSORT
// =============================================================================
template <typename T>
void insertionsort(T arr[], int n)
{
    int holePostion;
    int valueToInsert;

    for (int i = 1; i < n; i++){
        valueToInsert = arr[i];
        holePostion = i;

        while(holePostion > 0 && arr[holePostion-1] > valueToInsert){
            arr[holePostion] = arr[holePostion - 1];
            holePostion = holePostion - 1;
        }

        arr[holePostion] = valueToInsert;
    }
}

int main(){
    string filedest;
    string arrSize;
    cout << "enter file destenation: ";
    cin >> filedest;

    int arr[];

    int n = sizeof(arr) / sizeof(arr[0]);

    fileReader(filedest, arr);

    quicksort(arr, n);


    return 0;
}

#endif
abod sakka
  • 47
  • 9
  • 11
    [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) – MikeCAT Apr 14 '21 at 22:52
  • Save yourself some code and implement swap with references instead of pointers: `void swap(int & a, int & b) { int temp = a; a = b; b = temp; }` – user4581301 Apr 14 '21 at 23:07
  • 3
    Note that if you have a `swap` function and `using namespace std;` you are at risk of calling `std::swap` from the C++ Standard Library by mistake. I recommend removing `using namespace std;`, but if you can't so that for some silly reason, rename `swap` to something that cannot conflict. You can have the same problem with `partition` conflicting with `std::partition`. This is one of the many ways [`using namespace std;` is toxic.](https://stackoverflow.com/questions/1452721) – user4581301 Apr 14 '21 at 23:09
  • @MikeCAT its a uni assingment and i cant change the function headers for the functions quicksort, heapsort and inserationsort, so i have to work with arrays – abod sakka Apr 14 '21 at 23:19
  • @user4581301 thanks so much! – abod sakka Apr 14 '21 at 23:20
  • It would be convenient pass the `std::vector` around, but if you cannot, you can get the array that `std::vector` is managing for you with [`std::vector`'s `data` method](https://en.cppreference.com/w/cpp/container/vector/data). If you are forced to compile to the older C++ Standards where `data` is not present, I've never seen a `vector` implementation ([excluding that weirdo `vector`](https://en.cppreference.com/w/cpp/container/vector_bool)) where taking the address of the first element of the `vector`, `&vec[0]`, doesn't work, 100% legal code or not. – user4581301 Apr 14 '21 at 23:24
  • Recommendation: Put some thought into how you will get the size of the `vector` because currently you are not getting the array size AT ALL. I have a feeling that you are supposed to infer the size of the array from the contents of the file, and in this case you will find it hard to use `std::vector` without changing the function prototypes. You may need to consult [the section on Dynamic arrays](https://stackoverflow.com/a/4984228/4581301) in the duplicate. Unfortunately I'm not sure how you would get the size of the array out without changing the function prototype. – user4581301 Apr 14 '21 at 23:35
  • Ahhh wait... Your comment doesn't say you can't change `fileReader`. You may be able to use `std::vector fileReader(string fileName)`, and from that you can employ [Remy's excellent answer here](https://stackoverflow.com/a/36659103/4581301) and write something like `std::vector fileReader(string fileName) { std::ifstream infile(fileName); return std::vector( std::istreambuf_iterator(infile), std::istreambuf_iterator() );}` – user4581301 Apr 14 '21 at 23:40

1 Answers1

0

As @MikeCAT mentioned, you can use std::vector<T>.

You can add push elements at the back of the vector by std::vector<T>::push_back().

Alternatively, you can also resize the vector array by using std::vector<T>::resize() and then add elements at a specific location similar to what you are doing in your fileReader() function. You can also insert elements at a specific location by calling std::vector<T>::insert() .

Do have a look at the time complexities if you have a time constraint in your programming task.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Jalansh
  • 31
  • 3
  • This is a uni assignment and I have to use an array because i cant change the function heads for functions (quicksort, heapsort, inserationsort) and in the function heads the params are arrays and not vectors. – abod sakka Apr 14 '21 at 23:41
  • @abodsakka A `std::vector` contains an array that you can pass into the functions. I discuss in the comments above. – user4581301 Apr 14 '21 at 23:43