0

I am writing a program that creates an array of random numbers from 1 to 100 and sorts them in ascending order. Below is working code that does this, but I need to modify it so that the "swap" function makes use of pointers. The call for the swap function should look like this: swap(???,???) where the two inputs are pointers. What is the best way to accomplish this?

    #include<stdio.h>
    #include<math.h> 

    int main()
    {
       void fillArray(int sizeArray, int array[sizeArray]);
       void printArray(int sizeArray, int array[sizeArray]);
       void sortArray(int sizeArray, int array[sizeArray]);

       int sizeArray;

       printf("\nSize of the array? ");
       scanf("%d", &sizeArray);

       int array[sizeArray];

       fillArray(sizeArray,array);
       sortArray(sizeArray, array);
       printArray(sizeArray, array);
    }

    void fillArray(int sizeArray, int array[sizeArray])
    {
       int increment;

       for(increment=0; increment<sizeArray; increment++)
       {
          array[increment]=rand()%101;
       }
    }

    void sortArray(int sizeArray, int array[sizeArray])
    {
       void swap(int increment2, int increment, int array[]);
       int increment, increment2, temp;

       for (increment=0; increment < sizeArray ; increment++)
       {
          for (increment2=increment+1; increment2 < sizeArray; increment2++)
          {   
             swap(increment2, increment, array);
          }
       }
    }

    void swap(int increment2, int increment, int array[])
    {
       int temp;
       if (array[increment2] < array[increment])
             {
                temp=array[increment];
                array[increment]=array[increment2];
                array[increment2]=temp;
             }
    }

    void printArray(int sizeArray, int array[sizeArray])
    {
       int increment=0;

       printf("\nHere's the sorted array:\n");

       while(increment<21)
       {
          printf("\n    array[%d] is %d", increment, array[increment]);
          increment++;
       }
    }

The output should look like this: output

1

3 Answers 3

0

Define your swap function as below:

void swap(int *increment2, int* increment)

Modify your for loop where you call the swap function:

for (increment=0; increment < sizeArray ; increment++)
{
      for (increment2=increment+1; increment2 < sizeArray; increment2++)
      {   
         swap(array[increment2], array[increment]);
      }
}

Then, modify your swap function:

void swap(int *increment2, int* increment)
{
       int temp;
       if (increment2 < increment)
       {
            temp= *increment2;
            *increment2=*increment;
            *increment2=temp;
       }

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

1 Comment

Thank you for your help!
0

You need to fix your function call for the parameters to be with pointers.

void swap(int *increment2, int* increment)

Then in your swap function you need

You will need to deference the integer*.

Example
int n1;
int* x = 100
n1 = *x;

You may need to deference in the future example

Your function accepts pointers

void swap(int *increment2, int* increment)

If you have integers or another data type to reference them, refer to their address, you can perform & for referencing.

int i = 5;
int* x;
x = &i;

x is now an integer pointer to the address of i.

Comments

0

Your calling code needs to pass the address of the integers to compare and swap. Either of the following forms is acceptable, and they are equivalent.

swap(array+increment2, array+increment);
swap(&array[increment2], &array[increment]);

The first form takes the address of the first element (array) and adds the index (increment2) to get the address of the correct element.

The second version is more straightforward, perhaps. It uses the & address-of operator to take the address of array[increment2], which is the desired integer.

Your swap function need to be defined as follows:

void swap(int** p2, int** p1)
{
    int temp;
    if (*p2 < *p1)
    {
        temp=*p1;
        *p1=*p2;
        *p2=temp;
    }
}

Note how the pointers are dereferenced with the * operator to get the integer values for comparison (and storage in temp).

1 Comment

If you're happy with the answer, please "accept" so that it can be closed.

Your Answer

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