Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Programming Help

Options
  • 12-03-2013 9:06pm
    #1
    Registered Users Posts: 27


    Hi there i am doing an assignment and am in need of some help, the assigmnet is to use functions and the bubble sort algorithm. The program is meant print the number of passes and the sizes of the array of 8 different arrays. I amnt to sure how to tackle this problem, should i make 8 different function (one function for each array) or is there a way to have one function but to change the value of the array before it calls upon the function in the main code? Or have i missed a simpler easy way? Any advice would be greatly appreciated.
    Thanks


Comments

  • Registered Users Posts: 7,501 ✭✭✭BrokenArrows


    haze man wrote: »
    Hi there i am doing an assignment and am in need of some help, the assigmnet is to use functions and the bubble sort algorithm. The program is meant print the number of passes and the sizes of the array of 8 different arrays. I amnt to sure how to tackle this problem, should i make 8 different function (one function for each array) or is there a way to have one function but to change the value of the array before it calls upon the function in the main code? Or have i missed a simpler easy way? Any advice would be greatly appreciated.
    Thanks

    Before you start programming make sure you understand how a bubble sort is performed.
    Get the logic of how its done straight in your head and then try and convert your understanding into code.


  • Registered Users Posts: 2,781 ✭✭✭amen


    Post some of the code that you have at the moment.


  • Registered Users Posts: 27 haze man


    i have a good understanding of the bubble sort at the moment the problem is with how to go about writing the code and what methods to use, the code i have so far just uses the bubble sort for an array size 100 and prints the array before and after sorting

    #include <conio.h>
    #include <time.h>
    #include <stdlib.h>
    #include<stdio.h>
    // Bubble sort algorithm

    int bubbleSort(int array[], int array_size)
    {
    int i, j, temp;
    int n = 100;
    int swap=0;

    for(i=0; i<100; i++)
    {
    for(j=0;j<100;j++)
    {
    if(array[j+1]<array[j])
    {
    temp=array[j+1];
    array[j+1]=array[j];
    array[j]=temp;
    swap++;
    }
    }
    }


    return n;
    }

    void main() // main program to test the algorithm
    {
    // Declare and initialise a simple array of size 15

    int myarray[100];
    int num=0;
    int i, n;

    for(i=100;i>0;i--)
    {
    myarray[num] = i;
    num++;
    }

    printf("N = %d", n);

    // Call the Bubble sort algorithm (pass variables myarray, and n)
    bubbleSort(myarray, n);

    // print out the sorted array
    printf("\n\n\nSorted Array is: ");
    // loop to print the sorted array here
    for(i=0;i<100;i++)
    {
    printf("%i,",myarray);
    }
    getch();
    }


  • Registered Users Posts: 1,414 ✭✭✭Fluffy88


    I've never done C++ so my syntax will most likely be wrong.
    In C++ it's possible to read the length of an array so you can loop through it starting from the first element and ending at the last.

    A loop to go through an array would typically look like this
    for (i=0; i<array.[B]length[/B]; i++) {
        // do something
    }
    


    Using this method means you don't have the hard code the number of times you want your bubbleSort function to loop, so you will be able to pass an array of any size into the function and it will sort it for you :)


  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic


    Arrays in C/C++ aren't classes and don't have a length method. If the array is a stack variable, you can find it's size like this:
    size_t const length = sizeof(myArray) / sizeof(myArray[0]);
    


  • Advertisement
  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    You have array_size being passed into your bubbleSort function, but you don't use it. Why not?


  • Registered Users Posts: 27 haze man


    i have looked through the code and have come up with another more efficient working way.
    Thanks for al the help anyway guys much appreciated


  • Registered Users Posts: 27 haze man


    i have an error in this code, the line is the under the first time the function is called. My syntax is wrong and i cant seem to see where i have gone wrong. here is the code, Thanks
    /*program to use bubble sort algorithm and improved bubble sort algorthm */
    #include <conio.h>
    #include <time.h>
    #include <stdlib.h>
    #include<stdio.h>

    #define BUBBLE1 800
    #define BUBBLE2 1600
    #define BUBBLE3 2400
    #define BUBBLE4 3200
    #define BUBBLE5 4000

    int bubble_sort( int [], int );
    void improv_bubbleSort ( );

    main()
    {
    int pass = 0;
    int array1[BUBBLE1];
    int array2[BUBBLE2];
    int array3[BUBBLE3];
    int array4[BUBBLE4];
    int array5[BUBBLE5];

    //calling the bubble sort algorithm for the different arrays

    bubble_sort(array1 , BUBBLE1);
    pass = bubble_sort(int num); // <<<<<<
    Error on this line

    bubble_sort(array2 , BUBBLE2);

    bubble_sort(array3 , BUBBLE3);

    bubble_sort(array4 , BUBBLE4);

    bubble_sort(array5 , BUBBLE5);


    }//end to main()


    //bubble sort function
    int bubble_sort (int new_array[], int num)
    {
    int i;
    int j;
    int temp;
    int count = 0;

    for (i = (num - 1); i >= 1; i--)
    {
    for (j = 1; j <= i; j++)
    {
    if (new_array[j] > new_array[j + 1])
    {
    temp = new_array[j + 1];
    new_array[j + 1] = new_array[j];
    new_array[j] = temp;
    count ++;
    }//end to if
    }//end to iner for loop
    }// end to outer for

    return (count);

    }//end to function


  • Registered Users Posts: 7,501 ✭✭✭BrokenArrows


    Your function requires two variables to be passed in but you are only passing one


  • Registered Users Posts: 27 haze man


    Your function requires two variables to be passed in but you are only passing one
    ok cool thanks


  • Advertisement
  • Registered Users Posts: 27 haze man


    i have changed the code around but i still cant even get it to print out the value of the array, and the program is crashing when ever i launch the application, Can anyone help?

    /*program to use bubble sort algorithm and improved bubble sort algorithm */
    #include <conio.h>
    #include <time.h>
    #include <stdlib.h>
    #include<stdio.h>

    #define BUBBLE1 800
    #define BUBBLE2 1600
    #define BUBBLE3 2400
    #define BUBBLE4 3200
    #define BUBBLE5 4000

    int bubble_sort( int array[], int num);
    int improv_bubbleSort ( );

    main()
    {
    int pass = 0;
    int array1[BUBBLE1];
    int array2[BUBBLE2];
    int array3[BUBBLE3];
    int array4[BUBBLE4];
    int array5[BUBBLE5];

    //calling the bubble sort algorithm for the different arrays

    bubble_sort(array1 , BUBBLE1);
    pass = bubble_sort(array1, BUBBLE1);
    printf("The number of passes was %d for the array size %d \n", pass, array1);

    bubble_sort(array2 , BUBBLE2);
    pass = bubble_sort(array2, BUBBLE2);
    printf("The number of passes was %d for the array size %d \n", pass, array2);

    bubble_sort(array3 , BUBBLE3);
    pass = bubble_sort(array3, BUBBLE3);
    printf("The number of passes was %d for the array size %d \n", pass, array3);

    bubble_sort(array4 , BUBBLE4);
    pass = bubble_sort(array4, BUBBLE4);
    printf("The number of passes was %d for the array size %d \n", pass, array4);

    bubble_sort(array5 , BUBBLE5);
    pass = bubble_sort(array5, BUBBLE5);
    printf("The number of passes was %d for the array size %d \n", pass, array5);


    }//end to main()


    //bubble sort function
    int bubble_sort (int new_array[], int num)
    {
    int i;
    int j;
    int temp;
    int count = 0;

    for (i = (num - 1); i >= 1; i--)
    {
    for (j = 1; j <= i; j++)
    {
    if (new_array[j] > new_array[j + 1])
    {
    temp = new_array[j + 1];
    new_array[j + 1] = new_array[j];
    new_array[j] = temp;
    count ++;
    }//end to if
    }//end to iner for loop
    }// end to outer for

    return (count);

    }//end to function


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Use breakpoints to debug it and see what it is doing. Either that or put some printouts to show value of stuff as it runs.

    Also use the code tags to format the code you paste here, it's annoying to read without indentation.


Advertisement