0

I need to create a program that asks the user for N unique values and print the values in descending order for every number entered. The problem is it only outputs the number 0. Can anyone point out what is wrong with my code?

#include <iostream>

using namespace std;

int main(){
    //declare the variables
    int N, j, i, k, z, desc, temp, sorter[N];
    bool found;
    //Ask the user for N size array
    cout << "Enter array size: ";
    cin >> N;
    while(N<5){
        cout << "Invalid value. N must be greater than five(5)" << endl;
        cout << "Enter array size: ";
        cin >> N;
    }
    int list[N];
    //Printing how many values they need to enter
    cout << " " << endl;
    cout << "Please enter " << N << " values" << endl;
    //Code of the program
    for (int i = 0; i < N; i++){
        do{
            found = false;
            cout << "\n" << endl;
            cout << "Enter value for index " << i << ": ";
            cin >> temp;
            for (int j = 0; j < i; j++)
                if (list[j] == temp)
                    found = true;
            if (found == true)
                cout << "Value already exist";
            else{
                for(int k = 0; k < i; k++){
                    int key = sorter[k];
                    j = k - 1;
                    while(j >= 0 && key >= sorter[j]){
                        sorter[j + 1] = sorter[j];
                        j--;
                    }
                    sorter[j + 1] = key;
                }
                cout << "\nValues: ";
                for(int z = 0; z <= i; z++){
                    cout << sorter[z] <<" ";
                }
            }
        } while(found == true);
        sorter[i] = temp;   
    }
  • 2
    Your compiler wants to tell you what is wrong with your code. [Enable warnings and let it.](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings) – JaMiT Mar 05 '22 at 05:33
  • sounds like you need a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – kesarling He-Him Mar 05 '22 at 06:27
  • Here, using `std::set` will ease everything. – Damien Mar 05 '22 at 06:40

2 Answers2

0

You shouldn't be defining the array 'sorter[N]', the position, where you are currently, because you don't have the value of 'N', during compilation the arbitrary amount of space will be allocated to the the array.

solve the other compiling errors in your code.

slashtab
  • 1
  • 1
0

What you want is just a 3-line code:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::vector<int> vecOfNumbers  = { 1,3,5,7,9,8,6,4,2 }; // scan these numbers if you want
    std::sort(vecOfNumbers.begin(), vecOfNumbers.end());
    std::copy(vecOfNumbers.rbegin(), vecOfNumbers.rend(), std::ostream_iterator<int>(std::cout, " "));
    return 0;
}

Make sure you understand it before using it

kesarling He-Him
  • 1,944
  • 3
  • 14
  • 39
  • It doesn't do what OP is asking, detect duplicates are print sorted array for each new value. – Damien Mar 05 '22 at 06:41