Home > database >  Sorting an array from Largest to smallest in C
Sorting an array from Largest to smallest in C

Time:09-16

I wanna sort an array from largest to smallest number and make a new array which has it sorted...

so here is my code:

#include <iostream>
using namespace std;
 int main()
 {
   int size, sum = 0, answer = 0,pos, max;
   int array[size];
  int array2[size];
  cin >> size;

   for (int i = 0; i < size; i  )
   {
    cin >> array[i];
    sum =array[i];
   }

   for (int i = 0; i < size; i  )
   {

    max = 0;
    pos = 0;
    for (int q = 0; q < size; q  )
    {
      if (array[q] > max)
      {
        max = array[q];
        pos = q;
      }
    }
    array2[i] = max;
    array[pos] = 0;
    
   }

   for (int i = 0; i < size; i  )
   {
    cout << array2[i] << ", ";
   }

 return 0;
 }

When I put my input:

5
1 2 3 4 5

The output I get is:

0, 0, 0, 0, 5,

but I expect it to be 5, 4, 3, 2, 1,

CodePudding user response:

First of all always initialize a variable when you create it as by default it has some garbage value in C ,

Also you are trying to assign a size variable (as size for an array) that has nothing assign to it yet which will create problems, Secondly you are initializing an array first and then you are taking the size variable from user which is completely opposite of the flow, for creating arrays with dynamic size see Output Of Code

Edit:

As Per @PaulMcKenzie method, the other way which is considered the appropriate one, uses the std::Vector method to initialize a dynamic array in C , people who use the first method in visual studio might face errors,

Second Method Updated Code:

#include <iostream>
#include <vector>
    using namespace std;
     int main()
     {
       int size=0, sum = 0, answer = 0,pos, max;
      cin >> size;
      std::vector<int> array(size), array2(size);
       for (int i = 0; i < size; i  )
       {
        cin >> array[i];
        sum =array[i];
       }
    
       for (int i = 0; i < size; i  )
       {
    
        max = 0;
        pos = 0;
        for (int q = 0; q < size; q  )
        {
          if (array[q] > max)
          {
            max = array[q];
            pos = q;
          }
        }
        array2[i] = max;
        array[pos] = 0;
        
       }
    
       for (int i = 0; i < size; i  )
       {
        cout << array2[i] << ", ";
       }
    
     return 0;
     }

Second Output

Output Using Vector Method

CodePudding user response:

The size of an array variable must be a compile-time constant. A user-supplied value at runtime is probably unknowable at compile time.so I recommend using std::vector instead of array.

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

int main()
{
    
    int size=0, input=0;
    std::cout << "enter size :";
    std::cin >> size;
    std::vector<int> vec;

    for (size_t i{ 0 }; i < size;   i)
    {
        std::cout << "enter "<<i<< ".input:";
        std::cin >> input;
        vec.push_back(input);
    }

    // Sort the elements of the vector in descending order
    for (const auto& i : vec)
        std::sort(vec.begin(), vec.end(), std::greater <>());

    //Print the elements of the vector
    for (const auto& i : vec)
       std::cout << i << " ,";

    return 0;
}

Output:

enter size :5
enter 0.input:1
enter 1.input:2
enter 2.input:3
enter 3.input:4
enter 4.input:5
5 ,4 ,3 ,2 ,1 ,
  • Related