# Thread: sorting array

1. ## sorting array

hello, i need a function which gets integers array, and prints it in ascending order, the point is that it's not allowed to change the array. loops allowed-maximum 3!
thanks,

2. Originally Posted by bJudy
hello, i need a function which gets integers array, and prints it in ascending order, the point is that it's not allowed to change the array. loops allowed-maximum 3!
thanks,
it's also not allowed to use another array

3. Welcome aboard.

Please read the three sticky notes (Announcements).
The first two:

C Board - Announcements in Forum : C Programming
C Board - Announcements in Forum : General Programming Boards

Tim S.

4. Oh, I am sorry would you like champagne while you wait as well sir?

5. What did your instructor suggest in class, that you use to complete this assignment?

Show what you've done, and tell us what the instructor recommended for this problem. That will save a lot of time, and get you off to a good start.

6. Originally Posted by Adak
What did your instructor suggest in class, that you use to complete this assignment?

Show what you've done, and tell us what the instructor recommended for this problem. That will save a lot of time, and get you off to a good start.
Oh, Adak, you are too nice sometimes. His instructor probably suggested they do their own work.

7. I've got a bet going on how the student winds up doing this.

Money, money, money, money -- \$money\$!

It's a sing-along with Pink Floyd! < ROFL >

8. ## array sorting

well, i thank all nice people for being such nice,
any way, i am a new user on this forum,
i wrote
Code:
```{
int i,x;
int min,tmp;
int A[SIZE];
for(i=0;i<SIZE;i++)
{
printf("Please insert to array grade to idx %d\n",i);
fflush(stdin);
scanf("%d",&A[i]);
}
min=A[0];
for(x = 0; x < SIZE; x++)
{
if(A[x]<min )						    min=A[x];
}
printf("%d\n",min);
}```
the problem is that i can't find a way to complete the function, i know that i should have another loop that compares other numbers and find next minimum, can i get some ideas??
thanx

9. Code:
```{
int i,x;
int min,tmp;
int A[SIZE];
//add int max variable
//add int variable sum equals 0, here
for(i=0;i<SIZE;i++)
{
printf("Please insert to array grade to idx %d\n",i);
fflush(stdin);
scanf("%d",&A[i]);
getchar();  //removes left over newline char from the enter key
//}
if(i equals zero) {
//assign min to A[i]
//assign max to A[i]
}
if(A[i]  is less than min)
//what is min now equal to?
if(A[i] is greater than max)
//what is max now equal to?

sum equals sum plus A[i]; //get your sum going

}

//for(x = 0; x < SIZE; x++)
//{
//if(A[x]<min )						    min=A[x];
//}
printf("%d\n",min);
}```
You may need to use more parameters, but that's not prohibited by the instructor. Any variable that you want to change, in a local function, and have it stayed changed after the function returns, needs to have an & "address of" operator used in the calling function, and a * used in the receiving function:

Code:
```getData(A, &min, &max, &sum); //<== the call to the function

void getData(int A[50], int *min, int *max, int *sum) { //<==the function receiving it
//etc.
}```
If I just wrote:
Code:
`getData(A, min, max, sum);`
then any changes made to min, max, or sum, would be lost when control returned from getData, since here, all these variables are not addresses, they're just copies being sent to getData().

10. Also, never fflush(stdin).

Popular pages Recent additions