0

I wrote this code, but it is only giving me an address of something:

#include<iostream>
using namespace std;

void swap(int* a,int* b) 
{
    int temp=*a;
    *a=*b;
    *b=temp;
}

void sort(int* p[],int n) 
{
    for(int i=0; i<n;i++)
    {
        if(*p[i]>*p[i+1])
        swap(p[i],p[i+1]);
    }
}

int main()
{
    int arr[]={8,6,5,4,3,7,1};
    int* p[7];

    for(int i=0;i<7;i++)
    {
        p[i]=&arr[i];
    }

    sort(p,7);

    /*i tried to change arr[1] to p[1] or *p[1] but same output*/
    cout<<arr[1]<<arr[2];
    return 0;
}

I think I'm lacking in concept somewhere. The complete question is this:

Write the following function that indirectly sorts the floats pointed to by the first n pointers in the array p by rearranging the pointers: void sort(float* p[],int n)

Please help.

1
  • Do you know how to sort a simple int[] rather than a float*[] (without resorting to std::sort, which you would do in real code but which would defeat the purpose of the exercise)? Commented Apr 2, 2017 at 9:38

2 Answers 2

1

Here is a better way of doing what you want, there are many ways it can be done better but it looks like your new to c++ so I tried to make it as simple as possible.

#include "stdafx.h"
#include<iostream>
using namespace std;

//not needed, std::swap does the same job!
void swap(int *a, int *b)
{
    std::swap(a, b);
}

//using a pointer to the array is better!
void sort(int arr[], int n)
{
    //two loops are needed to sort the entire array!
    for (int x(0); x < n - 1; x++)
    {
        //optimize the loop by removing already sorted items from loop
        int sorted = n - x;
        for (int y(0); y < sorted - 1; ++y)
            if (arr[y] > arr[y + 1])
                std::swap(arr[y], arr[y + 1]);
    }
}


int main()
{
    //vector or std::array better option!
    int arr[] { 8,6,5,4,3,7,1 };

    //provide the array to sort(), no need to make stuff harder
    sort(arr, 7);

    //Show you that the sort worked!
    for (int ccc = 0; ccc < 7; ccc++)
        std::cout << arr[ccc] << ' ';

    std::cout << '\n';

    cout << "Index 5: " << arr[5] << "\nIndex 6: " << arr[6] << "\n";

    return 0;
}

Here is a very good tutorial on sorting arrays: http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/

Sign up to request clarification or add additional context in comments.

Comments

0

First of your code has a buffer overflow, which means undefined behaviour and anything it does is therefore by definition correct. In sort the index i goes over all array positions from 0 to n-1. But then you access i+1, which is one element beyond the array. For me the code segfaults.

Correcting this the output is "54" and here is what happens.

The swap function swaps the values in arr instead of swapping the pointers in p. The exercise asks you to indirectly sort the array, menaing the pointers. You need to use ** or *& there. So what happens in the sort loop is that 8 and 6 are swapped, then 8 and 5 are swapped and 8 and 4 are swapped and so on. So your array then is {6,5,4,3,7,1,8}. Last you output arr[1] and [2], which is 5 and 4. Since you don't output a space between them you get "54".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.