``````#include <stdio.h>
#include <stdlib.h>

int main()
{
int numbers[10] = {12,33,51,1,-1,97,7,23,0,-99};

sort(numbers,10);
display(numbers,10);
return 0;
}

void sort(int *numbers, int size)
{
/*
Hop you could help me provide codes to sort the numbers. after calling this function, the numbers array in main should be sorted.

hint: use the size for looping
*/
}

void display(int *numbers, int size)
{
/*
this will print the array of integers that has been called.
*/
}``````

pls help me, i am still learning..:( hop you can help me..tnx :)

There are several (many) sort algorithms to choose from. Did your instructor tell you which one to use? The simplest one to code is called the bubble sort, but its also the most time consuming to run. Just google for "bubble sort" and you will find example code that you can use.

BTW: No one here will write the program for you. Its your job to learn how to write the programs. We just help out by answering specific questions you might have.

``````#include <stdio.h>
#include <conio.h>
void display(int *, int);
void sort(int *, int);
int main()
{
int numbers[10] = {12,33,51,1,-1,97,7,23,0,-99};
sort(numbers,10);
printf("\nThe sorted list is\n\n");
display(numbers,10);
return 0;
}

void sort(int *numbers, int size)
{
int i,j,t;

for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(*(numbers+i)>=*(numbers+j))
{
t=*(numbers+i);
*(numbers+i)=*(numbers+j);
*(numbers+j)=t;
}
}
}

}

void display(int *numbers, int size)
{
int i;
for(i=0;i<size;i++)
printf("%d ",*(numbers+i));
}``````
commented: What part of [CODE] tags did you not understand? -4

Muthmuth, it's far better if you:

1) Wait until the poster has made at least one solid attempt to write the code themselves, before you write code for them.

2) Use code tags around your code, ALWAYS. Just highlight your code and click on the [ CODE] icon at the top of the editor window, and it's done.

Appreciate your enthusiasm, and welcome to the forum, Muthmuth. ;)

Another tricky fact i found out while learning is determining the max index based on the length of the array and it's type.

- might come handy someday :

``````for(i=0;i<sizeof(a)/sizeof(*a);i++){
// do stuff here

}``````

When you are sorting, you always need to know how many items you need to sort. It's very common to have an array of 100 elements, but only have 68 data items in that array, that are valid. The rest are just holding junk values or zeroes.

Another tricky thing is that your sizeof() function for arrays, only works for global arrays, and arrays that are declared in the current function.

So any array that you bring into a function as a parameter, you can't use sizeof() on. If you try, you'll only get the sizeof() a pointer to that data type - because arrays degrade to a pointer when passed to a function. So you need to always pass the size of the valid data around (or the size of the entire array if it's completely full of good data), when you pass an array to a function.

Trippy stuff!

commented: good advice :) +17

I used this thread to fix my own code for a similar problem, so thanks. But the problem I'm having now is for some reason the max values the user can enter is 6 (below); if anyone could help that would be great.

edit: I also just noticed if you enter the same value more than twice in a row, the sort or something messes up and the array loses numbers.

``````#include <stdio.h>

void swap(int *x, int *y);

int main(void)
{
int num_grades;
int grades[num_grades];
int i;
int j;

printf("How many grades to sort?: ");
scanf("%d", &num_grades);

printf("\nEnter the %d grades to sort:\n", num_grades);
for(i = 0; i < num_grades; i ++)
{
printf("%d> ", i + 1);
scanf("%d", &grades[i]);
}

printf("\n The entered values were:\n\n");
for(i = 0; i < num_grades; i ++)
{
printf("%d, ", grades[i]);
}

for(i = 0; i < num_grades; i ++)
{
for(j = i + 1; j < num_grades; j ++)
{
if(grades[i] < grades[j])
{
swap(&grades[i], &grades[j]);
}
}
}

printf("\n\n The sorted values are:\n\n");
for(i = 0; i < num_grades; i ++)
{
printf("%d, ", grades[i]);
}

return(0);
}

// Swaps values.
void swap(int *x, int *y)
{
int temp;

temp = *x;
*x = *y;
*y = temp;
}``````

line 8. That declaration won't work because the value of num_grades is unknown when that line is executed. And it may not compile with most compilers because most compilers do not yet support the upcoming version of C standards. What you should do is declare it as a pointer then call malloc() to allocate the array after the value of num_grades is known (after line 13)

Hmm, I have compiled it and it works for any array smaller than 6.
For example it rearranges 1, 4, 2, 3, 5 correctly as 5, 4, 3, 2, 1

Also I'm pretty new to c so if you could explain what malloc() does or how it allocates an array I would appreciate it.

edit: i just moved line 8 to line 14 and it seems to be working correctly for larger values now

Ok, here goes

``````#include <stdio.h>

int main(void)
{
int num_grades;
int *grades = NULL;
int i;
int j;

printf("How many grades to sort?: ");
scanf("%d", &num_grades);
grades = malloc(num_grades * sizeof(int));``````

Ok, here goes

``````#include <stdio.h>

int main(void)
{
int num_grades;
int *grades = NULL;
int i;
int j;

printf("How many grades to sort?: ");
scanf("%d", &num_grades);
grades = malloc(num_grades * sizeof(int));``````

Thank you very much; as a side note is there any advantage to using your code as opposed to the code below? Both work correctly as far as I can tell.

And thanks again in advance.

``````int main(void)
{
int num_grades;
int i;
int j;

printf("How many grades to sort?: ");
scanf("%d", &num_grades);

int grades[num_grades];``````

many compilers do not support that construct -- the compilers will require num_grades to be a constant. g++ is one of the few compilers that recognize that construct, which is new to c++0x, which is not yet an official version of c++ standards. Use a different compiler and you will most likely get an error on that line.

Some compilers (like the older Turbo C) will "give" you 5 to 9 elements of a failed array, even though the request for the array, failed. At least, it appears that way. The failed array will work for up to 9 elements, maximum. Try size 20 though, and it will fail every time.

That's what you had.

Stick with the standard for C, and you'll be fine.

many compilers do not support that construct -- the compilers will require num_grades to be a constant. g++ is one of the few compilers that recognize that construct, which is new to c++0x, which is not yet an official version of c++ standards. Use a different compiler and you will most likely get an error on that line.

C++0x doesn't support variable length arrays. C99 does (which gcc supports), but I don't recommend using them.