CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2013
    Posts
    7

    Array sort code help!!

    I need help with writing a function called swap that sorts the elements of an array in order from highest to lowest values where they descend and ascend. A particular thing about this function is that swap does NOT re-arrange elements in the array so it just uses a second array of indexes for the elements in the original array and then swap sorts the second array based on the values in the original array. A sorted version of the original array can then be produced with these sorted indexes. You can declare and initialize the original array without any user input. For example, int arr[10] = {1, 5, 22, 14, 6, -5, 7, 9, 12, 15 };
    Header of the function swap must be as shown below: void swap(int array[],int swapedIndexes [], int size, char mode) When mode is 'a', the function sorts the array in the ascending order, and when mode is 'd', the function sorts it in the descending order. int swappedIndexes[5];swap(array,swappedInde… 5, 'a');


    So far I have this code that randomly generates arrays of 10 elements and I found the max and min, but I am confused on what to do about the swapping part? Also I cant figure out how to make the numbers ascend or descend in numerical order?? Can somebody please please help me to finish with writing this code?
    Here is what i have done so far:

    #include<ctime>
    #include <iostream>
    using namespace std;
    int min(int [],int);
    void max(int [],int , int &);
    void main()
    { srand(time(0));
    //1-declare
    int g[10];
    int res=0;
    //2-init
    for(int i=0;i<10;i++)
    { g[i]=rand()%250;
    cout <<g[i] << "\t";
    }

    //3- processing
    res=min(g,10);
    cout << "\nthe minimum is " << res<<endl;
    max(g,10,res);
    cout << "the maximum is " << res<<endl;
    //printing


    system("pause");
    }
    int min(int a[],int n)
    { int m=a[0];

    for(int i=1;i<n;i++)
    if( a[i] <m)
    m=a[i];
    return m;
    }
    void max(int a[],int n, int &m)
    { m=a[0];

    for(int i=1;i<n;i++)
    if( a[i] > m)
    m=a[i];

    }
    Last edited by wuzup6678; July 6th, 2013 at 01:17 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Array sort code help!!

    Since you are using std why not use std classes (like vector) and functions like std::sort?
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2013
    Posts
    7

    Re: Array sort code help!!

    I haven't learned how to do that yet so I have no idea how to use std::sort

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Array sort code help!!

    Please use code tags when posting code. Go Advanced, select code and click '#'. Also please format your code before posting.

    Code:
    #include <ctime>
    #include <iostream>
    using namespace std;
    
    const int ARR_SIZE = 10;
    
    int min(int [], int);
    void max(int [], int, int &);
    
    void main()
    {
    //1 - declare
    int g[ARR_SIZE];
    int res = 0;
    
    	//2 - init
    	srand((unsigned int)time(0));
    
    	for (int i = 0; i < ARR_SIZE; i++) {
    		g[i] = rand() % 250;
    		cout << g[i] << "\t";
    	}
    
    	//3 - processing
    	res = min(g, ARR_SIZE);
    	cout << "\nthe minimum is " << res << endl;
    
    	max(g, ARR_SIZE, res);
    	cout << "the maximum is " << res << endl;
    
    	//printing
    
    	system("pause");
    }
    
    int min(int a[], int n)
    {
    int m = a[0];
    
    	for(int i = 1; i < n; i++)
    		if (a[i] < m)
    			m = a[i];
    
    	return m;
    }
    
    void max(int a[], int n, int &m)
    {
    	m = a[0];
    
    	for (int i = 1; i < n; i++)
    		if (a[i] > m)
    			m = a[i];
    
    }
    Can somebody please please help me to finish with writing this code?
    As this is an exercise, we can't write the code for you
    (see http://forums.codeguru.com/showthrea...ork-assignment)

    You're got the function definitions and you know what the functions are supposed to do. The first thing you need to do before you code is to design the functions. Express in english the algorithm required for each of the functions. Then design the logic/algorithm for the program. Once you have done that then you can start to code your program/functions from your design.

    Have a go at this. Post back what you have come up with and we'll provide further guidance.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jun 2013
    Posts
    7

    Re: Array sort code help!!

    ok I am just trying to figure out how to even approach this questions that is all. Can someone explain what the question is even asking? because i am very lost I understand it wants it to print ascending and descending values, now is that all it wants? In addition to the question posted above it also says at the bottom :
    ===========================================================
    In the main program I have to 1) Declare and initialize the array
    2) Declare the array sortedIndexes but do not initialize it. I have to play with the array
    sortedIndexes in the function selectionSort, so the selectionSort does NOT re-arrange elements in the array; instead, it uses a second array, an array of indexes for the elements in the original array and then selectionSort sorts the second array based on the values in the original array. A sorted version of the original array can then be produced with these sorted indexes. 3)Call the function selectionSort with the proper arguments (descending or ascending order, make sure both works properly) 4) Print the elements of the array in the requested order.
    The header has to be this way:
    Void selectionSort(int array[], int sortedIndexes [], int size, char mode)
    Example:
    int array[5]={3, 5,-1,10,0};
    int sortedIndexes[5];
    sortMe(array,sortedIndexes, 5, 'a');


    If your function re-arranges the elements in the array array, it is wrong.
    If you initialize the array sortedIndexes in the function main, it is wrong
    The function must sort arrays of any sizes; otherwise, its wrong.
    If you change the header of the function swap in any way, also wrong.


    =====================================================
    So I am beyond confused here, I do understand that it wants me to first have an option to press 'a' for ascend, then 'd' for desend and show those values go in order, now from here what else does it want me to print on the screen?? does it want the positions to show or something? its talking about sortedindexes like it wants to print the positions of the numbers but im not sure if it even asks to do this?? Can someone please help me understand what to do here?

    Heres an example of what I mean by positions, do you guys think it wants this?????
    The five highest valued names:
    k => 9 at position 4
    m => 8 at position 6
    l => 7 at position 5
    d => 6 at position 1
    e => 5 at position 2
    The five lowest valued names:
    f => 2 at position 3
    n => 3 at position 7
    c => 4 at position 0
    e => 5 at position 2
    d => 6 at position 1

  6. #6
    Join Date
    Jun 2013
    Posts
    7

    Re: Array sort code help!!

    here is how i was thinking of using the swap
    void swap(int& x, int& y)
    {
    int temp;
    temp = x;
    x = y;
    y = temp;
    }// end swap()
    Last edited by wuzup6678; July 6th, 2013 at 07:05 PM.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Array sort code help!!

    Quote Originally Posted by wuzup6678 View Post
    ok I am just trying to figure out how to even approach this questions that is all. Can someone explain what the question is even asking? because i am very lost :
    Instead of:
    Code:
    array[i];
    //..
    array[i+1];
    //..
    etc.
    you should have
    Code:
    array[indexArray[i]];
    //...
    array[indexArray[i + 1]];
    //...
    etc...
    The "indexArray" is the array of indices.

    Here is what you do -- write a normal sort routine, forgetting about the condition of having another array of indices. Can you do that? If you can, then replace the "[i]", "[i+1]"or whatever you used as an index with "[indexArray[i]]", "[indexArray[i + 1]]", etc.. If you want to see it work, work out a simple array of 4 or 5 elements with pencil and paper, concentrating on only swapping indexArray:

    You are using the indexArray at position i to point to the proper element in the array. In other words an "index within an index", that points you to the right place.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 6th, 2013 at 07:23 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured