Trouble using pointers to sort array in C -


i'm working on program class. using pointers. had generate array number of elements being determined user (using malloc). got part working. second had sort array in descending order. have no clue why cant work. code flips whole array, 3 4 5 12 5 becomes 5 12 5 4 3, not want. i'm sure it's small, life of me cant figure out i'm doing wrong.

void main() {     int *p, *sizearray, *q;     int i, siz;     printf("how large want array? enter number between 0 , 50\n");     scanf("%d", &siz);     if (siz <= 50)     {         p = genarr(siz);         (i = 0; <siz; i++)             printf("%i\n", *(p + i));          arrsort(p,siz);          (i = 0; <siz; i++)             printf("%i\n", *(p + i));     }     else         printf("that number not in given range");      while(1); }     #include "stdafx.h" #include <time.h>           // required time_t structure #include <stdlib.h>         // reqwuired srand() , 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));      (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     (k = 0; k< (si - 1); k++)    // element compared     (j = (k + 1); j < si; j++)   // rest of elements     {          swap(&prt[k], &prt[j]);     }     return prt; }  void swap(int *s, int *r) {         int pswap = *r;         *r = *s;         *s = pswap;  } 

for (j = (k + 1); j < si; j++)   // rest of elements {      swap(&prt[k], &prt[j]); } 

this should swap if k > j, need if statement:

for (j = (k + 1); j < si; j++)   // rest of elements {     if (prt[k] > prt[j])         swap(&prt[k], &prt[j]); } 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -