-1
\$\begingroup\$

I made this program which sorts the elements of an array in an ascending order and then prints out the sorted array.

#include <stdio.h>

int main()
{
    int size, array[1000];
    scanf("%d", &size); 

    for (int i=0; i<size; i++)
    {   
        scanf("%d", array[i]);
    }

    int count, max, pos;
    count = size-1;
    max = array[0];

    for (int j=0; j<size; j++)
    {
        for (int k=0; k<=count; k++)
        {
            if (array[k]>max) 
            {
                max=array[k];
                pos=k; 
            }

            array[count]=array[pos];
            array[pos]=array[count];
            count--;
        }
    }

    for (int l=0; l<size; l++)
    {
        printf("%d ", array[l]);    
    }   

    return 0;
}
\$\endgroup\$
5
  • 2
    \$\begingroup\$ This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does \$\endgroup\$ – Zeta Aug 13 '17 at 7:50
  • 1
    \$\begingroup\$ While your edit says that you're going to sort an array, your actually sorting an array of daily temperatures that are getting read in from standard input. Details like this should be embedded in your question, together with your rationale, e.g. why do you store 1000 days, what algorithm did you try, why did you use said algorithm, are you interested in better algorithms. If you say more about your code you will get better* code reviews. (*better in terms of more specific to you, than to a general audience) \$\endgroup\$ – Zeta Aug 13 '17 at 9:38
  • \$\begingroup\$ @Zeta Haha. Lol. It has literally absolutely nothing do with temperatures. temp, day[n]. Sorry. It's all part of a more complex problem from Codeforces. tempis a commonly used variable name in C/C++; it's like a count variable. That array named day is for a wholly different purpose. That is, how long it takes to learn a particular instrument. All of this is completely irrelevant to my actual focus. So, I decided to leave it out. :-) \$\endgroup\$ – Soha Farhin Pine Aug 13 '17 at 18:09
  • \$\begingroup\$ What makes you think this program sorts the array? As far as I can tell, it iterates only through the first half of the array, setting the elements to the maximum found so far, as well as includes some code that efficiently does nothing (the line array[pos]=array[count]; - I can guess that meant to swap the elments, but it does not work that way. Seriously though, use the qsort function for Codeforces. \$\endgroup\$ – kfx Aug 13 '17 at 19:11
  • \$\begingroup\$ As kfx said, does this program actually work? \$\endgroup\$ – Zeta Aug 14 '17 at 12:29
4
\$\begingroup\$

This element swap won't work:

array[count]=array[pos];
array[pos]=array[count];

You are assigning b to a, and then immediately a to b. In C, you will need to use a temporary variable for this operation, like so:

int buffer;
buffer=array[count];
array[count]=array[pos];
array[pos]=buffer;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.