0

I have tried void BubbleSort, but it does not work as well. Do you know how could I sort the array using pointers and basing on mark_lab values? So first should be the elements, which have the highest mark_lab value.

Ususally I receive error in if statement from the 1st for(), however it seems to be written correct.

#include <stdlib.h>
#include <string.h>

    struct student{
    char name [20];
    char surname [20];
    char id_num [10]; 
    int mark_lab;
    int mark_test;
    };
int main(){
    int i;

    struct student st[]={
        {"AA", "Pu", "1C666", 8, 9},
        {"RR",  "Ab","4DC33",5,7},
        {"AA", "Z","24C25", 10, 6},
        {"KK", "Oz","161RD", 9,10},
        {"EE", "Pre", "DC902", 7,8}
    }; 

  //to sort the array
   for (i=0;i<4;i++){ 
        if (stu[3]->mark_lab<stu[i]->mark_lab){
            stu[3]->name=stu[i]->name;
            stu[3]->surname=stu[i]->surname;
            stu[3]->id_num=stu[i]->surname;
            stu[3]->mark_lab=stu[i]->mark_lab;
            stu[3]->mark_test=stu[i]->mark_lab;
            
            stu[i]->name=stu[i+1]->name;
            stu[i]->surname=stu[i+1]->surname;
            stu[i]->id_num=stu[i+1]->surname;
            stu[i]->mark_lab=stu[i+1]->mark_lab;
            stu[i]->mark_test=stu[i+1]->mark_lab;
            
            stu[i+1]->name=stu[3]->name;
            stu[i+1]->surname=stu[3]->surname;
            stu[i+1]->id_num=stu[3]->surname;
            stu[i+1]->mark_lab=stu[3]->mark_lab;
            stu[i+1]->mark_test=stu[3]->mark_lab;
        }
    }
    
    //to print it on the screen
    for (i=0;i<4;i++){
        printf (printf("\nName: %s, Surname: %s, ID: %s, Lab_Mark: %d, Test Mark: %d",
         stu[i]->name, stu[i]->surname,stu[i]->id_num, stu[i]->mark_lab,stu[i]->mark_test);
    }

    return 0;
}
oguz ismail
  • 1
  • 16
  • 47
  • 69
Eli
  • 51
  • 5
  • After you fix the sorting this part is wrong: `printf (printf("` maybe this was a typo?? – drescherjm Dec 14 '20 at 16:56
  • This should help with the Bubble Sort algorithm: [https://www.softwaretestinghelp.com/bubble-sort/](https://www.softwaretestinghelp.com/bubble-sort/) – drescherjm Dec 14 '20 at 16:58
  • 1
    I would lean toward `std::sort` and a Compare function. But I also think the c++ tag doesn't belong on this question. – sweenish Dec 14 '20 at 17:12

1 Answers1

1

There's a lot going on here, so let's break down what's wrong and what to do without directly handing you the answers. The 'easiest' solution is what sweenish suggested using std::sort and a compare function, but if you need to implement the sorting yourself, here's a rough breakdown of the steps you need to take without giving you the entire implementation.


Miscellaneous bugs

  • Your array is named 'stu' but you are attempting to access it with the name 'st'. Your compiler should have warned you instantly (if you've tried to even compile this at all) about this problem.
  • You're trying to access student elements by dereferencing the struct with '->'. Your array stores students, not heap-allocated student *'s, so you need to access with the '.' operator, or malloc each student in the array.
  • drescherjm also pointed out your printf(printf( error
  • Personally, I like to 'typedef' my structs, but you don't have to do that.

Swapping

First, a swap function will make your life much easier. We can simply swap using the array and indices of the two elements. The simple way is with a temporary variable, though you can use XOR-swapping as well (which comes with its own caveats). Remember that when using a function to modify the array, we need to pass the array by reference, as the array is stored in the stack frame of main(), and we want our functions changes to persist, so we must pass the array by reference. See here for more information on that. Your swap function should look something like this.

void swap(struct *students[], int idx_a, int idx_b) {
    struct student temp = *students[idx_a];
    *students[idx_a] = *students[idx_b];
    *students[idx_b] = temp;
}

I noticed that you are also setting struct id_num's to surnames. If this is something you actually are supposed to do, you will need to account for that in this swap function as well.


Sorting

You'll use your swap function to implement sorting. You said you were using bubble sort, so this is pseudo-code of what that may look like. Remember that you reverse-sorting (largest-first) so your comparisons will need to be 'flipped' from normal bubble sort implementations.

for (i = 0; i < 4; i++) {
    for ( j = 0; j < 4 - i - 1; j++) {
        if (st[j].mark_lab < st[j+1].mark_lab)
           swap(&st, j, j+1);
    }
}

This should get you most of the way there. In the future, I highly recommend compiling this code before you try debugging or going to stackoverflow, as compilers are much smarter than you and I, and will give you descriptive explanations for many of the errors you are experiencing. Try compiling with 'gcc -o [executable_name] [your_program.c]'. When you clean up your compile-time errors, you can run with ./[executable_name] to try to diagnose any run-time errors you may have.

Overall, I would recommend solidifying your understanding of C's syntax, and the fundamentals of how pointers and structs work. When you find yourself writing many lines of redundant code, such as the majority of the body of your for loop, stop coding. Reassess what exactly you are trying to do, and try to find a cleaner way to do it.

  • I am trying to use the swap method , but I have an error. I have edited first line by writing struct student *stu, but I still have an error in the next line. Could uou please help? I think I have syntax error, which I cannot understand – Eli Dec 21 '20 at 21:07