Sorting via pointers

In our assignment, we have to make a sort function using pointers! However, I am having a strange error with mine and I can't seem to figure it out! The populate function generates random numbers for the code, and it works fine.

The sort seems to work okay, but it will take the randomly generated numbers and change them into something like:

89
89
89
89
89
34
34
34
34
17


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

void populate(int []);
void print(int []);
void sort(int []);

main()
{
int num[9];
 
populate(num); 
sort(num);

printf("\n\nHere is the updated value chart.\n\n");
print(num); 
 
printf("\n\n"); 
 
system("PAUSE");
return 0;
      
}


//FUNCTION TO ADD RANDOM VALUES TO ARRAY
void populate (int str[])
{

int num;
int *ptr=0;
srand((unsigned)time(NULL));  

for(int count=0; count<10; count++)
   {
     num=(rand()%100)+1;
     ptr=&num;
     str[count]=*ptr;
   }
     
}

//FUNCTION TO PRINT ARRAY
void print (int str[])
{
printf("TABLE OF VALUES\n");     
for(int z=0; z<10; z++)
   {
    printf("Value %d: %d\n", z+1, str[z]); 
   } 

}
Call srand()unsigned)time(NULL)) inside of main(), not the random function. Otherwise, a new seed is initialized each time you call a random number, meaning that they will end up being the same numbers over and over. A simple rule of thumb is that you only need srand called once, so never put it into a function that calls it over and over. I also recommend that you read the first stickied thread in this subforum as to why using system("PAUSE") is bad practice.
Last edited on
Thank you, I have implemented what you suggested but it's still having the same trouble?

And thank you for the advice about system("PAUSE"), i will definitely look into that!
Well... a quick test, and it would appear that the problem lies in your sort function. Otherwise, it works fine. All of the numbers that are generated are random.
Do you have any advice on how to fix the sort function?
main() needs to return an int.

You haven't posted the code for sort(), so we have no idea what it is, let alone what's wrong with it.
Oh, I meant to post sort instead of populate, ahaha, that was dumb of me.

Here is the code for sort:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void sort(int str[])
{
  
printf("\n\nThe function is now being sorted in descending order!\n");     
      
int x=0; //initalizes variables
int y=1; 
int temp;
int *ptr1=0;
int *ptr2=0;
 
for(int x=0; x<10; x++) //for loop for number of passes
	{
		for(int y=0; y<9; y++) //for loop for each pass
		{ 
			if(str[y]<str[y+1]) //if statements that switches numbers if not in proper order
			{
                               
                ptr1=&str[y];
                ptr2=&str[y+1];
                str[y]=*ptr2;       
                str[y+1]=*ptr1;
			}
			
		}
	}
}
Note that the x, y, and temp on lines 6, 7, 8 are unused and can be deleted. I would recommend just using that temp int to perform the swap instead of using two pointers, though.

In any case, I don't see anything logically wrong with the sort. Have you tested it to see what it does?
Topic archived. No new replies allowed.