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

I need help with my C program. Essentially its suppose to take a file with names

ID: 3688148 • Letter: I

Question

I need help with my C program. Essentially its suppose to take a file with names and ages and sort them in alphabetical order. I decided to use bubble sort and scanline. Some help would be great.

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRSIZ 40
#define MAXNAMES 100

void
bubble_str(char *list[], int ages[], int n){
  
int i;
int pass;
char *temp;
int *temp2;
int sorted;
pass = 1;
do{
sorted = 1;
  
for(i = 0; i < n - pass; ++i){
if(strcmp(list[i], list[i +1]) > 0){
temp = list[i];
list[i] = list[i +1];
list[i + 1] = temp;
temp2 = ages[i];
ages[i] = ages[i +1];
ages[i+1] = temp2;
sorted = 0;
}
}
pass+= 1;
}while(!sorted);
}


char * scanline(char *dest, /* output - destination string */
int dest_len) /* input - space available in dest */
{
int i, ch;

/* Gets next line one character at a time. */
i = 0;
for (ch = getchar();
ch != ' ' && ch != EOF && i < dest_len - 1;
ch = getchar())
dest[i++] = ch;
dest[i] = '';

/* Discards any characters that remain on input line */
while (ch != ' ' && ch != EOF)
ch = getchar();

return (dest);
}


int main(int argc, char** argv) {

char name[MAXNAMES][STRSIZ];
char *new[MAXNAMES];
int ages [MAXNAMES];
int *alpha[MAXNAMES];
int num_names;
int i;
char one_char;
int one_int;
FILE *file_name;
int ch;
  
file_name = fopen("p5.txt", "r");
  
if(file_name == NULL)
{
printf("EE 233 Spring 2016 P4, nik4mub, Nicole Mubarak ");
exit(0);
}

else if (file_name)
{
  
/*Scans all the way to the end of the file*/
while ((ch = getc(file_name)) != EOF)

{
/*Gets the values from the file as a double*/
fscanf(file_name, "%c%d", &one_char, &one_int);
for(i = 0; i < num_names; ++i){
scanline(name[i],STRSIZ);
scanline(ages[i],STRSIZ);
}
for(i =0; i < num_names; ++i){
new[i] = name[i];
alpha[i] = ages[i];   
bubble_str(new, alpha, num_names);   
  
}
for(i =0; i < num_names; ++i){
printf("%-30s%-30s&d ", name[i],new[i],ages[i]);
}
}
}
}

Explanation / Answer

How bubble sort works:

We take an unsorted array for our example. Bubble sort take (n2) time so we're keeping short and precise.

14 33 27 35 10

Bubble sort starts with very first two elements, comparing them to check which one is greater.

14 33 27 35 10

In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33 with 27.

14 33 27 35 10

We find that 27 is smaller than 33 and these two values must be swapped.

14 33 27 35 10

The new array should look like this

14 27 33 35 10

Next we compare 33 and 35. We find that both are in already sorted positions.

14 27 33 35 10

Then we move to next two values, 35 and 10.

14 27 33 35 10

We know than 10 is smaller 35. Hence they are not sorted.

14 27 33 35 10

We swap these values. We find that we reach at the end of the array. After one iteration the array should look like this

14 27 33 35 10

To be precise, we are now showing that how array should look like after each iteration. After second iteration, it should look like this

14 27 10 33 35

Notice that after each iteration, at least one value moves at the end.

14 10 17 33 35

And when there's no swap required, bubble sorts learns that array is completely sorted.

10 14 027 33 35

Now we should look into some practical aspects of bubble sort.

Algorithm

We assume list is an array of n elements. We further assume that swapfunction, swaps the values of given array elements.

}

-----------------------------------------------------------------------------------------------------------------------------------------

program in C to sort a set of names in alphabetical order entered by user.:

typedef struct name_data {char *name;
char *surname;
int age;
float salary;
};
name_data employees [100]; /* This is an array of empolyees */
Then you need to input the data in - then a sort routine

void swap(employee *a, employee *b){

item tmp;

tmp = *a;
*a = *b;
*b = tmp;
}

void bubbleSort(employee a[], int size){

int i, j;

for (i=0; i<size-1; i++){
for (j=size-1; j>i; j--)
if (strcmp(a[j].surname, a[j-1].surname) > 0)
swap(&a[j], &a[j-1]);

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote