Thread: please see my program

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    Red face please see my program

    there is some problem in my code please help me to find it


    Code:
    #include <stdio.h>
    #include <iostream>
    #include <stdlib.h>
    int c;
    void insertionsort(int x[],int length)
    {
    int key,i;
    for(int j=1;j<length;j++)
    {
    key=x[j];
    i=j-1;
    while(x[i]>key && i>=0)
    {
    x[i+1]=x[i];
    i--;
    }
    x[i+1]=key;
    }
    printf("After  insertionsort: ");
    for(i = 0; i < 10; i++) {
    printf(" %d ", x[i]);
    }
    printf("\n");
    }
    
    void recursive_quicksort(int arr[], int low, int high ) {
    int i = low;
    int j = high;
    
    /* compare value */
    int z = arr[(low + high) / 2];
    
    /* partition */
    do {
    /* find member above ... */
    while(arr[i] < z) i++;
    
    /* find element below ... */
    while(arr[j] > z) j--;
    int y;
    if(i <= j) {
    /* swap two elements */
    y = arr[i];
    arr[i] = arr[j];
    arr[j] = y;
    i++;
    j--;
    }
    } while(i <= j);
    
    /* recurse */
    if(low < j)
    recursive_quicksort(arr, low, j);
    
    if(i < high)
    recursive_quicksort(arr, i, high);
    }
    
    void Swap(int x, int y){
    int temp = x;
    x = y;
    y = temp;
    }//end swap
    void choosePivot(int theArray[], int first, int last){
    int pivot;
    
    //find middle of array
    int middle = last - first;
    //compare first middle and last item in array
    if((first < last)&&(last > middle)){
    if(first < middle){
    //middle is median number
    pivot = middle;
    }else{
    //first is median number
    pivot = first;
    }
    }else if((first < middle)&&(middle > last)){
    if(first < last){
    //last is median number
    pivot = last;
    }else{
    //first is median number
    pivot = first;
    }
    }else{
    if(last < middle){
    //middle is median number
    pivot = middle;
    }else{
    //last is median
    pivot = last;
    }
    }
    }
    void partition(int theArray[], int first, int last, int pivotIndex){
    
    choosePivot(theArray, first, last);
    
    int pivot = theArray[first];
    
    int lastS1 = first;
    
    int firstUnknown = first + 1;
    
    for(; firstUnknown <= last; ++firstUnknown){
    
    if(theArray[firstUnknown] < pivot){
    
    ++lastS1;
    Swap(theArray[firstUnknown], theArray[lastS1]);
    }//end if
    
    Swap(theArray[first], theArray[lastS1]);
    pivotIndex = lastS1;
    }// end partition
    
    }
    
    void quicksort(int theArray[], int first, int last){
    int pivotIndex;
    
    if(first < last){
    //create the partition: S1, pivot, S2
    partition(theArray, first, last, pivotIndex);
    
    }
    }
    
    
    int main(void) {
    int array[10] ;
    int i = 0;
    for(i = 0; i < 10; i++)
    array[i] = rand() % 100;
    int ch;
    printf("\n\t***** MAIN MENU *****\n");
    printf("\n1. insertion sort\n");
    printf("\n2. recursive quick sort\n");
    printf("\n3. non_recursive quick sort\n");
    printf("\n4. Exit Program");
    printf("\n----------------------------------");
    printf("\nSelect any one of the above==>");
    scanf("%d",&ch);
    /* print the original array */
    switch(ch){
    case 1:
    printf("Before sorting the array: ");
    for(i = 0; i < 10; i++) {
    printf(" %d ", array[i]);
    }
    printf("\n");
    insertionsort(array, 10);
    break;
    case 2:
    printf("Before sorting the array: ");
    for(i = 0; i < 10; i++) {
    printf(" %d ", array[i]);
    }
    printf("\n");
    recursive_quicksort(array, 0, (10 - 1));
    printf("After  non_recursive quicksort: ");
    for( i = 0; i < 10; i++) {
    printf(" %d ", array[i]);
    }
    printf("\n");
    break;
    case 3:
    printf("Before sorting the array: ");
    for(i = 0; i < 10; i++) {
    printf(" %d ", array[i]);
    }
    printf("\n");
    quicksort(array,0,9);
    break;
    case 4:
    printf("you are out of the menue");
    }
    
    c=getchar();
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    It's not surprising you can't find any problems in your code - you have absolutely no structure! The compiler doesn't care how your code is formatted, but other programmers do. You'll find you'll get a lot more help from others when you provide neatly formatted code (and, oddly enough, you might find you need less help from others if you format your code!)

    Assuming this is supposed to be a C program, the first thing you need to do is remove the <iostream> library - that's a C++ library.

    Now, can you be a bit more explicit about what problem you're having? If I had to guess, I'd say it was with your choosePivot function. It appears to do nothing. Look there first.

    Kevin

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,200
    To follow up what kmess said.
    Read this -> SourceForge.net: Indentation - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Location
    Karachi, Pakistan
    Posts
    19
    I thought there was no programme without a "main()" function....???
    this programme doesn't have one so isn't it a basic mistake...??

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Lord Slytherin View Post
    I thought there was no programme without a "main()" function....???
    this programme doesn't have one so isn't it a basic mistake...??
    It's there... look again.

    The OP needs to do several very important things...

    1) learn how to indent code so that it is readable... Indentation sould reflect levels of loops and if statements... Salem has provided a very good link for this.

    2) Read the actual error messages from his compiler and linker and eliminate them one by one.

    3) if the program compiles he needs to test run it, knowing behaviorally where it acts up provides powerful hints where the problems are.


    In any event... coming in here and asking us to fix his broken code without even ha hint of what's wrong is very unlikely to work, even at the most generous of times.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    thank you everyone for your comments
    i will follow them
    thanks a gain

  7. #7
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    Any luck, Susu?

    Kevin

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kmess View Post
    Any luck, Susu?

    Kevin
    LOL... Ok, I get why some of the people asking questions are impatient... but when answering ????

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by CommonTater View Post
    LOL... Ok, I get why some of the people asking questions are impatient... but when answering ????
    Admitted, LOL.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    I guess we have different motivations in helping people. For instance, I like to see that people are making progress toward solving their problems. What's your motivation?

    Kevin

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kmess View Post
    I guess we have different motivations in helping people. For instance, I like to see that people are making progress toward solving their problems. What's your motivation?

    Kevin
    Old saying.... "You can't help anyone who's not asking for help"...

    The best motivation here --given that a huge majority of posts are from people taking classes-- is to educate.
    The best way to educate is to motivate people toward self-actualization... point them in the right direction and hope they can read a compass.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help converting array program to link list program
    By hsmith1976 in forum C++ Programming
    Replies: 0
    Last Post: 02-14-2010, 09:50 PM
  2. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  3. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  4. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  5. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM