Results 1 to 4 of 4

Thread: Call-by-reference

  1. #1
    Join Date
    Jun 2013
    Posts
    1

    Default Call-by-reference

    I NEED HELP


    This program is sorting a randomized array of integers using the bubblesort algorithm.
    a) Try to understand the structure of the source code. Compile and run the program.
    b) Simplify the code and correct the program. Also the program logic can be improved!
    c) Modify the source code, so that the swapping of two values will be done by a function called swap values()
    using call-by-reference. The function should have as arguments only the two array elements that must be
    exchanged. (Note: do not pass the whole array to the function!)




    We consider an array with the first element containing the number of
    elements in the array, followed by 10 randomly initialized integers (elements).
    The code must sort the 10 elements in ascending order.

    Compile: g++ -Wall sorting.cpp -o sorting
    */

    #include <iostream>
    #include <stdlib.h>

    using namespace std;

    int main()
    {
    const int SIZE=10;
    int s_array[SIZE];
    int s_array_2[]={3,2,0,0};

    bool isExchanged=true;

    // We suppose that: the first element of the array contains the number
    // of elements in the array, followed by 10 elements that must be sorted
    s_array[0] = sizeof( s_array) / sizeof( s_array[0]);

    // Read the 10 elements
    for (int i=1; i<=SIZE; i++)
    {
    // rand() generates a random int value in the range [0-999]
    s_array[i]= rand() % 1000;
    }


    // Output the unsorted array
    cout << "UNsorted array has "<< s_array[0] <<" elements. These are:\n";
    for (int j=1; j<=10; j++)
    {
    cout<< "\t"<< s_array[j] <<",";
    }
    cout<< endl;


    // sort the 10 array elements
    while (isExchanged==true)
    {
    isExchanged=false;

    for (int x=1; x<=10; x++)
    {
    // exchange two elements
    if (s_array[x] > s_array[x+1])
    {
    int temp=s_array[x];
    s_array[x]=s_array[x+1];
    s_array[x+1]=temp;
    isExchanged=true;
    }
    }
    }

    // Output the sorted array
    cout << "The array has "<< s_array[0] <<" elements. These are:\n";
    for (int j=1; j<=10; j++)
    {
    cout<< j << "\t: "<< s_array[j] <<"\n";
    }

    // Hint: uncomment the following code in order to find mistakes ... :-)
    //
    // // Display the second array
    // for (int j=0; j<=2; j++)
    // {
    // cout<< "\t"<< s_array_2[j] <<",";
    // }
    // cout<< endl;

    return 0;
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Call-by-reference

    We are not solving school tasks people. We can help you with specific problems but that's all.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Call-by-reference

    I NEED HELP
    Don't we all

    Seriously though, which bit do you need help with. As wysota says, we are not going to do your whole assignment for you.

  4. #4
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Talking Re: Call-by-reference

    :d :d :d :d

Similar Threads

  1. Replies: 6
    Last Post: 3rd December 2012, 07:26
  2. Replies: 10
    Last Post: 19th September 2012, 15:13
  3. How get a reference to a QList
    By franco.amato in forum Newbie
    Replies: 4
    Last Post: 20th January 2011, 21:52
  4. Reference to ui
    By Phalanx in forum Qt Programming
    Replies: 7
    Last Post: 22nd April 2010, 15:58
  5. pointer/reference
    By mickey in forum General Programming
    Replies: 7
    Last Post: 16th July 2006, 14:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.