Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help. I need this program to call the sort function first, then display t

ID: 3859702 • Letter: P

Question

Please help. I need this program to call the sort function first, then display the original order of dataArray[], then display pointerArray[] sorted, then back to displaying original order of dataArray. I know I'm probably supposed to use const somewhere, but everytime time i do, the swap gets messed up. All the functions must be called in the order they appear in this program. Thank you in advance.

#include <stdio.h>

void swap(int* a,int* b);

void sort(int* array, int count);

void DisplayDataArray(int array[], int count);

void DisplayDPointerArray( int* array, int count);

int main()

{

int dataArray[]={120, 80, 72, 143, 123, 124, 125, 3000, 2999, 3000, 82, 876, 986, 345, 1990, 2367, 98, 2, 444, 993, 635, 283, 544, 923, 18, 543, 777, 234, 549, 864, 39, 97, 986, 986, 1, 2999, 473, 776, 9, 23, 397, 15, 822, 1927, 1438, 1937, 1956, 7, 29, -1 };

int i=0; int size = 0;

while(dataArray[i] != -1)

{ size++;

i++; }

int *pointerArray[size];

int k;

for ( k = 0; k < size - 1; ++k)

{ pointerArray[k]=&dataArray[k];

}

sort(*pointerArray,size);

printf("Now displaying data in the original order ");

DisplayDataArray(dataArray,size);

printf(" Now displaying data in sorted order ");

DisplayDataArray(dataArray,size);

printf(" Now displaying data in the original order ");

DisplayDataArray(dataArray,size);

return 0; }

void swap(int* a,int* b)

{ int t; t =*a; *a = *b; *b = t; }

void sort(int* array, int count)

{ int i, j;

for(i = 0; i < count-1; ++i)

{

for(j=0; j*(array+j+1))

{

swap((array+j),(array+(j+1)));

} } } }

void DisplayDataArray(int array[], int count)

{ int c=0; int i;

for ( i = 0; i < count; ++i)

{

if(c==10)

{ c=0;

printf(" ");

}

else

{

printf("%6d",array[i]);

c++; } } }

void DisplayDPointerArray( int* array, int count)

{ int c=0;

int i;

for ( i = 0; i < count; ++i)

{

if(c==10)

{

c=0;

printf(" ");

}

else

{

printf("%6d",*array);

array++; c++; } } }

Explanation / Answer

NOTE:
There is no need to pass address of the array to functions because arrays are passed by default in pointer method if you just give the array name as argument. Also in your sorting function there is no condition of when to swap the value which is important. I have corrected these two in your code and it works fine. Please check and let me know if you face any issues. I will revert back within 24 hours.

Code:
#include <stdio.h>

void swap(int* a,int* b);
void sort(int* array, int count);
void DisplayDataArray(int array[], int count);
void DisplayDPointerArray( int* array, int count);

int main()
{
int dataArray[]={120, 80, 72, 143, 123, 124, 125, 3000, 2999, 3000, 82, 876, 986, 345, 1990, 2367, 98, 2, 444, 993, 635, 283, 544, 923, 18, 543, 777, 234, 549, 864, 39, 97, 986, 986, 1, 2999, 473, 776, 9, 23, 397, 15, 822, 1927, 1438, 1937, 1956, 7, 29, -1 };

int i=0;
int size = 0;

while(dataArray[i] != -1){
size++;
i++;
}
  
int pointerArray[size];
int k;
for ( k = 0; k < size; ++k){
pointerArray[k]=dataArray[k];
}

sort(pointerArray,size);
printf("Now displaying data in the original order ");
DisplayDataArray(dataArray,size);
printf(" Now displaying data in sorted order ");
DisplayDataArray(pointerArray,size);
printf(" Now displaying data in the original order ");
DisplayDataArray(dataArray,size);

return 0;
}

void swap(int* a,int* b)
{
int t;

t =*a;
*a = *b;
*b = t;
}

void sort(int* array, int count)
{
int i, j;
for(i = 0; i < count-1; ++i)
for(j=0; j < count - i -1; j++)
if(array[j] > array[j+1] )swap((array+j),(array+j+1));

}

void DisplayDataArray(int array[], int count)
{
int c=0; int i;
for ( i = 0; i < count; ++i){
if(c==10){
c=0;
printf(" ");
}
else{
printf("%6d",array[i]);
c++;
}
}
}

void DisplayDPointerArray( int* array, int count)
{
int c=0;
int i;
for ( i = 0; i < count; ++i){
if(c==10){
c=0;
printf(" ");
}
else{
printf("%6d",*array);
array++;
c++;
}
}
}


Execution output screenshot:
https://pasteboard.co/GCmakbVl.png