
Ely M.
asked 12/17/20Programming Logic and Design
- Explain Bubble sorting
- Create an algorithm that allows the user to enter 20 names into a String array. Sort the array in ascending (alphabetical) order and display its contents.
- Design a program that asks the user to enter 10 golf scores. The scores should be stored in an Integer array. Sort the array in ascending order and display its contents
1 Expert Answer

Patrick B. answered 12/18/20
Math and computer tutor/teacher
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int (*CompareCallback) (void *, void *);
int CompareInt( void * rec1, void * rec2)
{
int iNum1 = * ((int*)rec1);
int iNum2 = * ((int*)rec2);
//printf(" comparing %d vs %d \n",iNum1,iNum2);
return(iNum1-iNum2);
}
int CompareStr(void * rec1, void * rec2)
{
//printf(" Comparing %s vs %s \n",(char*)rec1,(char*)rec2);
return (
strcmp(
(char*)rec1,
(char*)rec2
)
);
}
int CompareAmount( void * rec1, void * rec2)
{
double dblFlAmt1 = *((double *)rec1);
double dblFlAmt2 = *((double *)rec2);
int iReturn=0;
if (dblFlAmt1 != dblFlAmt2)
{
iReturn = (dblFlAmt1>dblFlAmt2) ? 1 : -1;
}
return(iReturn);
}
/*******************
counts[0] : # of integers in the array
counts[1]: # of float amounts in the array
counts[2]: # of strings in the array
counts[3]: string length
**************************************/
void Input( int * counts, int * intArray, double * dblArray, char strArray[][11])
{
int N,n;
int iLoop;
FILE * fptr = fopen("input.dat","r");
fscanf(fptr,"%d",&N);
counts[0]=N;
for (iLoop=0; iLoop<N; iLoop++)
{
fscanf(fptr,"%d",&intArray[iLoop]);
}
fscanf(fptr,"%d",&N);
counts[1]=N;
for (iLoop=0; iLoop<N; iLoop++)
{
fscanf(fptr,"%lf",&dblArray[iLoop]);
}
fscanf(fptr,"%d",&N);
counts[2]=N;
fscanf(fptr,"%d",&n);
counts[3]=n;
for (iLoop=0; iLoop<N; iLoop++)
{
fscanf(fptr,"%s",strArray[iLoop]);
}
fclose(fptr);
}
void BubbleSort ( void * array, int n, int recSize, CompareCallback compareFuncPtr)
{
int i,j;
char * A = (char*)array;
for (i=n-1; i>0; i--)
{
for (j=0; j<=i-1; j++)
{
char * curRecPtr = A + j * recSize;
char * nextRecPtr = A + (j+1)*recSize;
int iCompareReturn = compareFuncPtr((void*)curRecPtr,(void*)nextRecPtr);
if (iCompareReturn>0)
{
char * tempBuff = (char*) malloc(recSize);
//swaps if out of order
memcpy(tempBuff, curRecPtr, recSize);
memcpy(curRecPtr, nextRecPtr, recSize);
memcpy(nextRecPtr, tempBuff, recSize);
free(tempBuff);
} //swaps
} //for j
}//for i
} //BubbleSort
int main()
{
int iLoop=0;
int intNums[10];
double amounts[10];
char strs[20][11];
int counts[4];
Input (counts,intNums,amounts,strs);
BubbleSort(intNums,counts[0],sizeof(int),CompareInt);
for (iLoop=0; iLoop<10; iLoop++)
{
printf("%d \n",intNums[iLoop]);
}
printf("\n---------------------------------------------\n the amounts.......... \n");
BubbleSort(amounts,counts[1],sizeof(double),CompareAmount);
for (iLoop=0; iLoop<10; iLoop++)
{
printf("%lf \n",amounts[iLoop]);
}
printf("\n-----------------------------------------------\n the strings \n");
BubbleSort(strs,counts[2],counts[3],CompareStr);
for (iLoop=0; iLoop<20; iLoop++)
{
printf("%s \n",strs[iLoop]);
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
source code and input file uploaded to RESOURCES section under this link;12/18/20