Insertion sort-passing int problem

This is a discussion on Insertion sort-passing int problem within the C Programming forums, part of the General Programming Boards category; Insertion sort Hello everyone! I am begginer in this, and really need some help. It is simple programm, but i ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    12

    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. #2
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by denisa View Post
    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;
    }

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    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);
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by denisa View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-02-2008, 06:23 AM
  2. Insertion sort problem...
    By BigFish21 in forum C++ Programming
    Replies: 4
    Last Post: 05-27-2008, 12:53 AM
  3. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  4. List insertion sort Problem
    By wuzzo87 in forum C Programming
    Replies: 3
    Last Post: 09-28-2006, 07:36 AM
  5. Insertion Sort Problem
    By silicon in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2005, 12:30 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21