C Programming Bubble Sort and Search Names (Using Pointers)

Watch
Multifine
Badges: 9
Rep:
?
#1
Report Thread starter 1 year ago
#1
ASSIGNMENT: Create a program to do the following:

BONUS: A bonus of 20 points if you create the program in such a way that there is no limit on the number of names to be handled.

1) Read in names to sort until the user types the “enter” key as the first character of a “C-type” string (the maximum number of names is 20, there is not a maximum length to a name).

2) After sorting and displaying the array of “C-type” strings, ask the user for a “Ctype” string to search for (again, no maximum length).

3) Do a binary search to find if the “C-type” string is in the array

4) If the “C-type” string is in the array, tell the user the index of the “C-type” string.

5) If the “C-type” string is not in the array, tell the user that it is missing.

6) Repeat items 3 thru 5 until the user enters the “enter” key as the first character of the “C-type” string, at which time the program will complete.

Write the code for C programming.

Submit the .c and .h files in the project folder as created by Visual Studio.

************

SORT HEADER FILE:

#include "Sort.h"

typedef int BOOL;

#define FALSE 0
#define TRUE 1

/* this is known as a "Binary" Search */

int Search (const char Letters [], char C, size_t NumChars)
{
int First;
int Middle;
int Last;

First = 0;
Last = NumChars - 1;
while (First <= Last)
{
Middle = (First + Last) / 2;
if (C == Letters [Middle])
return Middle;
else
if (C < Letters [Middle])
Last = Middle - 1;
else
First = Middle + 1;
}
return -1;
}

/* this is known as a "Sinking" Sort or a "Bubble" Sort */

void Sort (char Letters [], size_t NumChars)
{
size_t i;
BOOL Sorted;
char Temp;

do {
Sorted = TRUE;
NumChars--;
for (i = 0; i < NumChars; i++)
if (Letters[i] > Letters[i + 1])
{
Temp = Letters [i];
Letters [i] = Letters [i + 1];
Letters [i + 1] = Temp;
Sorted = FALSE;
}
else;
} while (!Sorted);
}

*************

MAIN FILE:

#include <stdio.h>
#include <stdlib.h>

int main ()
{
char * Names [20];
char * Temp;

/* the bonus will do the array of Names as dynamic using pointers as */
char ** MoreNames;
MoreNames = (char **) malloc (20 * sizeof (char *));
/*
use example from last week to read in names and put the addresses
into slots in the array
*/

if (strcmp (Names [i], Names [i + 1]) > 0) /* means first name is greater than second */
{
Temp = Names [i];
Names [i] = Names [i + 1];
Names [i + 1] = Temp;
Sorted = FALSE;
}

/* remember to clean up the dynamic memory by using free */

system ("pause");
return 0;
}
*********************

I'm not sure where to start and how to use pointers for the bonus, to get the program to work. Any help is appreciated!
0
reply
winterscoming
Badges: 19
Rep:
?
#2
Report 1 year ago
#2
Do you still need to use an array and bubble-sort for the bonus? If not, you could use an alternative structure and sorting mechanism such as a binary tree which is sorted as part of insertion - i.e. http://cslibrary.stanford.edu/110/BinaryTrees.html


Otherwise, to continue using an array without a limit on the number of names, you could use the realloc() function to get another larger array and keep your existing data -- https://stackoverflow.com/questions/...06798#21006798

(It's impossible to resize an array, so what realloc actually does under-the-hood is to malloc() a new larger array, then do a simple memcpy() to preserve the data, before using free() with the old one - which should work fine for you in this case.
Last edited by winterscoming; 1 year ago
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Poll: What factors affect your mental health most right now? Post-lockdown edition

Anxiousness about restrictions easing (43)
5.8%
Uncertainty around my education (84)
11.34%
Uncertainty around my future career prospects (79)
10.66%
Lack of purpose or motivation (94)
12.69%
Lack of support system (eg. teachers, counsellors, delays in care) (39)
5.26%
Impact lockdown had on physical health (42)
5.67%
Social worries (incl. loneliness/making friends) (80)
10.8%
Financial worries (50)
6.75%
Concern about myself or my loves ones getting/having been ill (32)
4.32%
Exposure to negative news/social media (46)
6.21%
Difficulty accessing real life entertainment (24)
3.24%
Lack of confidence in making big life decisions (71)
9.58%
Worry about missed opportunities during the pandemic (57)
7.69%

Watched Threads

View All