0

Looking at the function sorteraF in the code I try to sort the array of type struct. I want to do this for both name type char and varunummer type int. However it is not working. To sort after names I used sortAfterNames which is not working either.

             //lager program lab 
             #include <stdio.h>
             #include <stdlib.h>
             #include <string.h> 
             #define WORDLENGTH 30
             #define MAX 10

             struct varor{ //VAROR STANDS FOR GOODS----------
                 int varunummer;
                 char namn[WORDLENGTH];
                 int lagersaldo; 
             };
             typedef struct varor Vara;

             Vara createVara(int varunummer, char namn[], int lagersaldo){  //CREATE THE GOODS
                 Vara v;
                 v.varunummer=varunummer;
                 strcpy(v.namn, namn);
                 v.lagersaldo=lagersaldo;
                 return v;
             }
           //register
             void regVaror( Vara reg[], int *pNrOfGoods){   //REGISTER THE GOODS

                 char confirm;
                 char namn[WORDLENGTH],
                      tmp[WORDLENGTH];
                 int varunummer, lagersaldo;
                 printf("\nAre you sure you want to register new goods?\n1: YES - (continue)\n2: NO - (go back to menu)\n");
                 scanf(" %c%*c", &confirm);// choose
                 switch(confirm){
                 case '1': 
                 do{
                     printf("Enter varunummer:");//varunummer stands for identify number of the good
                     gets(tmp); 
                     varunummer=atoi(tmp);

                     printf("Enter name:");
                     gets(namn);

                     printf("Enter Availability:");
                     gets(tmp);
                     lagersaldo=atoi(tmp);

                     reg[*pNrOfGoods]=createVara(varunummer,namn,lagersaldo);
                     (*pNrOfGoods)++;
                     printf("\nRegister more goods?\n1: YES - (continue)\n2: NO - (go back to menu)\n");
                     scanf(" %c%*c", &confirm);
                 }while(confirm=='1');

                 case '2': break;

                 }
             }
             //PRINTING THE GOODS ---------
            void printVara(Vara reg[], int nrOfGoods){
                int x;
                printf("\nVarunummer \t Namn \t\t\t Lagersaldo\n");
                for(x=0;x<nrOfGoods;x++){
                    printf(" %d \t\t %s \t\t\t %d\n",reg[x].varunummer,reg[x].namn,reg[x].lagersaldo);
                }
            }
    //SORTING THE GOODS---------
             void sorteraF(Vara reg[], int length){
                 int i, j;
                     struct varor tmp;
                     for (i = 0; i < (length-1); i++){
                         for (j = 0; j < (length -1- i); j++){
                             if (reg[i].varunummer < reg[i + 1].varunummer){
                                 tmp = reg[j];
                                 reg[j] = reg[j + 1];
                                 reg[j + 1] = tmp;
                             } 
                         }
                     } 
             }
void sortAfterName(reg[]){
int i,j;
         char tmp[MAX];
         for(i=0;i<MAX;i++){
             for(j=i+1; j<MAX;j++){
                 if(strcmp(reg[i].namn, reg[i+1].namn) >0){
                     tmp=reg[i];
                     reg[i]=reg[i+1];
                     reg[i+1]=tmp[i];
                  }
              }

             int main(){
                 int run=1;
                 Vara vRegister[MAX];
                 int nrOfGoods=0;
                 while(run){
                    char choice;
                    printf("\n\t\tMeny - Lager Program\n\n\
                    (1) Regrister new varor\n\b\b\b\b\
                    (2) Print all varor\n\
                    (3) Sort varor\n\
                    (4) Avsluta programmet\n");
                    scanf(" %c%*c", &choice);

                    if(choice=='1') regVaror(vRegister, &nrOfGoods);
                    if(choice=='2') printVara(vRegister, nrOfGoods);
                    if(choice=='3') sorteraF(vRegister, MAX);
                    else if(choice=='4') run=0;

                    printf("\n\nNumber of varor: %d\n", nrOfGoods);


                 }
                 return 0;
             }
3
  • 2
    any specific reason not to use qsort()? Commented Oct 19, 2017 at 15:11
  • @FelixPalmen I don't know how to use it, but I am down for anything at this point. Commented Oct 19, 2017 at 15:13
  • 1
    @KevinPunkt I don't know how to use it -> google "qsort sample" Commented Oct 19, 2017 at 15:18

1 Answer 1

3

Outline how to use qsort() (no reason to reinvent the wheel here):

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

#define WORDLENGTH 30

struct varor
{
    int varunummer;
    char namn[WORDLENGTH];
    int lagersaldo; 
};
typedef struct varor Vara;

// comparison function for qsort, just compare the names with strcmp:
static int compareByName(const void *v1, const void *v2)
{
    return strcmp(((const Vara *)v1)->namn, ((const Vara *)v2)->namn);
}

// the same for the numbers
static int compareByNumber(const void *v1, const void *v2)
{
    int n1 = ((const Vara *)v1)->varunummer;
    int n2 = ((const Vara *)v2)->varunummer;
    if (n1 < n2) return -1;
    if (n1 > n2) return 1;
    return 0;
}

int main(void)
{
    Vara v[100];

    // populate v with data

    // to sort by name:
    qsort(v, sizeof v / sizeof *v, sizeof *v, compareByName);

    // to sort by number:
    qsort(v, sizeof v / sizeof *v, sizeof *v, compareByNumber);
}

sizeof v / sizeof *v here results in the whole count of elements (100 here, in your example code probably 10) -- so if you don't populate the whole array, pass the number of used elements here.

Sign up to request clarification or add additional context in comments.

2 Comments

This is above the level I am expected I think for this project. And when I use this or even bubble sort , it compiles but after i sort it and view it everything is replaced with 0. I don't know what causes this
@KevinPunkt See my edit for a probable cause if you just copy&paste'd ... nevertheless, using qsort() is the simplest method. Implementing your own sorting algorithm is always more work (and, therefore, more likely to introduce more bugs)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.