Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #include<malloc.h>
- #include<stdlib.h>
- #define MAX_NAME 50
- typedef struct {
- int item_num;
- char item_name[MAX_NAME + 1];
- } structure;
- void printing_bubble(structure A[], int size) {
- int c = 0;
- printf("\nThis are your items: ");
- for (c = 0; c < size; c++) {
- printf("%d %s ", A[c].item_num, A[c].item_name);
- }
- }
- void simple_bubble_sort(structure A[], int size) {
- int i = 0, j = 0;
- structure temp;
- for (i = 0; i < size - 1; i++) {
- for (j = 0; j < size - 1 - i; j++){
- if (A[j].item_num > A[j + 1].item_num) {
- temp = A[j];
- A[j] = A[j + 1];
- A[j + 1] = temp;
- }
- }
- }
- }
- int main(void) {
- structure *one_item = NULL;
- int num_of_items = 0;
- int allocated = 10;
- int i = 0, exit = 1;
- structure *temp = NULL;
- one_item = malloc(allocated * sizeof(*one_item));
- do {
- printf("Type \"0\" to exit: ");
- scanf("%d", &exit);
- if (exit == 0) {
- break;
- }
- printf("Type in your %d item serial number: ", num_of_items + 1);
- scanf("%d", &one_item[i].item_num);
- printf("Type in your product name: ");
- scanf(" %[^\n]", one_item[i].item_name);
- num_of_items += 1;
- if (num_of_items >= allocated) {
- temp = realloc(one_item, num_of_items * 2 * sizeof(*one_item));
- if (temp != NULL) {
- one_item = temp;
- }
- }
- } while (num_of_items < 10);
- printf("\nBefore sorting:\n ");
- printing_bubble(one_item, num_of_items);
- simple_bubble_sort(one_item, num_of_items);
- printf("\nAfter sorting: \n");
- printing_bubble(one_item, num_of_items);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment