# Thread: Insertion sort-passing int problem

1. ## Insertion sort-passing int problem

Insertion sort
Hello everyone! I am begginer in this, and really need some help. It is simple programm, but i have a problem with line 39 how can i pass that number back to main? Thank you very much!!!!!
(I need numcount to count how many times data needed to be moved in order to sort 10 integers)
Code:
```int main()
{
int array[10];
int last=9;
int i;
int numcount;
int num;
for (i=0;i < 10;i++)
array[i] = rand() % 10;

insertionSort(array, last , numcount);
int t;
printf(" Integers have been sorted:");
for ( t=0; t < 10; t++)
printf(" %d ,", array[t]);
printf(" \nNumber of moves needed to sort array is: %d", numcount);
system("PAUSE");
return 0;
}
int insertionSort ( int list[], int last, int numcount ){
int hold;
int counting=0;
int walker;
int current;
for ( current = 1; current <= last; current++){
hold = list[current];
counting++;
for (walker = current - 1; walker >= 0 && hold < list[walker]; walker--){
list[walker+1] = list[walker];
counting++;
}
list [walker + 1] = hold;
}
numcount= counting;

//(line 39)
return numcount;
}```

2. Originally Posted by denisa
Insertion sort
Hello everyone! I am begginer in this, and really need some help. It is simple programm, but i have a problem with line 39 how can i pass that number back to main? Thank you very much!!!!!
(I need numcount to count how many times data needed to be moved in order to sort 10 integers)
Code:
```int main()
{
int array[10];
int last=9;
int i;
int numcount;
int num;
for (i=0;i < 10;i++)
array[i] = rand() % 10;

insertionSort(array, last , numcount);
int t;
printf(" Integers have been sorted:");
for ( t=0; t < 10; t++)
printf(" %d ,", array[t]);
printf(" \nNumber of moves needed to sort array is: %d", numcount);
system("PAUSE");
return 0;
}
int insertionSort ( int list[], int last, int numcount ){
int hold;
int counting=0;
int walker;
int current;
for ( current = 1; current <= last; current++){
hold = list[current];
counting++;
for (walker = current - 1; walker >= 0 && hold < list[walker]; walker--){
list[walker+1] = list[walker];
counting++;
}
list [walker + 1] = hold;
}
numcount= counting;

//(line 39)
return numcount;
}```

the only thing u can do is to use pointers...if u want to send numcount to main....else u can print the numcount in the insertion sort function itself...
to pass pointers...1st globally declare the function insertion sort

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int insertionSort ( int list[], int last, int *numcount ){
int hold;
int counting=0;
int walker;
int current;
for ( current = 1; current <= last; current++){
hold = list[current];
counting++;
for (walker = current - 1; walker >= 0 && hold < list[walker]; walker--){
list[walker+1] = list[walker];
counting++;
}
list [walker + 1] = hold;
}
*numcount= counting;

}
int main()
{
int array[10];
int last=9;
int i;
int numcount;
int num;
for (i=0;i < 10;i++)
array[i] = rand() % 10;

insertionSort(array, last , &numcount);
int t;
printf(" Integers have been sorted:");
for ( t=0; t < 10; t++)
printf(" %d ,", array[t]);
printf(" \nNumber of moves needed to sort array is: %d", numcount);
system("PAUSE");
return 0;
}```

3. Pointers can be used but that is certainly not the best option here.

denisa, Your insertionSort function body looks fine. The only issue that is preventing you getting the value you want back into main is that you're trying to pass something in instead of out, and are ignoring the return value.

Two fixes: Remove ", int numcount" from the function declaration:
Code:
`int insertionSort(int list[], int last) {`
and use the return value in main:
Code:
`numcount = insertionSort(array, last);`

4. Originally Posted by denisa
Insertion sort
Hello everyone! I am begginer in this, and really need some help. It is simple programm, but i have a problem with line 39 how can i pass that number back to main? Thank you very much!!!!!
(I need numcount to count how many times data needed to be moved in order to sort 10 integers)
The thing is that when you pass a variable into a function, the function receives a copy of the variable (C is strictly pass by value) so whatever happens inside the function will not change the variable of the same name outside the function.

Try this... it should get you pretty close...
Code:
```   // in main
numcount = insertionSort(array, last);

int insertionSort ( int list[], int last){
int hold;
int counting=0;
int walker;
int current;
for ( current = 1; current <= last; current++){
hold = list[current];
for (walker = current - 1; walker >= 0 && hold < list[walker]; walker--){
list[walker+1] = list[walker];
counting++;
}
list [walker + 1] = hold;
}
return counting;
}```
Note: I see iMalc beat me to this, so I'll give you some extra detail.