1

I'm working on a program for class. We are using pointers. We had to generate an array with the number of elements being determined by the user (using malloc). I got that part all working. Second we had to sort the array in descending order. I have no clue why I cant get this to work. This code flips the whole array, so that 3 4 5 12 5 becomes 5 12 5 4 3, but that is not what I want. I'm sure it's something small, but for the life of me I cant figure out what I'm doing wrong.

void main()
{
    int *p, *sizearray, *q;
    int i, siz;
    printf("How large do you want the array? Enter a number between 0 and 50\n");
    scanf("%d", &siz);
    if (siz <= 50)
    {
        p = genarr(siz);
        for (i = 0; i <siz; i++)
            printf("%i\n", *(p + i));

        arrsort(p,siz);

        for (i = 0; i <siz; i++)
            printf("%i\n", *(p + i));
    }
    else
        printf("That number was not in the given range");

    while(1);
}




#include "stdafx.h"
#include <time.h>           // required for the time_t structure
#include <stdlib.h>         // Reqwuired for the srand() and rand() functions
#include "ArrEdit.h"

int* genarr(int size)
{
    time_t t;
    int i, m;
    int *sizearr;

    sizearr = (int*)malloc(sizeof(int)*size);
    srand((unsigned)time(&t));

    for (i = 0; i<size; i++)
        *(sizearr + i) = rand() % 50;

    return sizearr;
    free(sizearr);
}


int *arrsort(int*prt, int si)
{
    int k, j;
    int temp;   // holding variable
    for (k = 0; k< (si - 1); k++)    // element to be compared
    for (j = (k + 1); j < si; j++)   // rest of the elements
    {

        swap(&prt[k], &prt[j]);
    }
    return prt;
}

void swap(int *s, int *r)
{
        int pSwap = *r;
        *r = *s;
        *s = pSwap;

}
7
  • Your code in arrsort looks like it's supposed to be a bubble sort, but you never actually check if the elements are already in order, and your description makes it sound like you only want to reverse the array, which this code won't do. Commented Mar 24, 2014 at 23:43
  • 1
    Not part of your question, but free(sizearr); is never called because you have already exited the function. However, since you are returning a pointer anyway you don't want to free it, since that will delete the memory you are trying to use. Commented Mar 24, 2014 at 23:45
  • 2
    Correct me if I'm wrong, but this actually seems like 100% C code which happens to also be valid C++ code? Commented Mar 24, 2014 at 23:46
  • What it is doing is reverseing the array, that is not what I want it to do. Commented Mar 24, 2014 at 23:46
  • And dont I have to free(sizearr) to prevent memory leaks? Commented Mar 24, 2014 at 23:47

1 Answer 1

1
for (j = (k + 1); j < si; j++)   // rest of the elements
{

    swap(&prt[k], &prt[j]);
}

This should only swap if k > j, so you need an if statement:

for (j = (k + 1); j < si; j++)   // rest of the elements
{
    if (prt[k] > prt[j])
        swap(&prt[k], &prt[j]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Seems likely the asker wants to compare prt[k] and prt[j], not just k and j.
@DrC Probably, woops.
AHA I got it! Thanks so much! I knew it was somnething little I was overlooking

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.