0

I wrote the following code for counting the frequency of names and then printing out the names in lexicographic order with their frequencies.The input here is supposed to be n number of names.So for sorting i wrote qsort function but i guess there is some error in it which i am not able to find bcoz using using other sorting method besides qsort works fine for this code.Please suggest what is the error as i have tried but found no help.below is the code snippet

#include <cstdlib>
#include<stdio.h>
#include<string.h>
struct stu
{
char name[100];
int no;
};
int cmpfunc(const void* p,const void *q){
struct stu *a = *((struct stu**)p);
struct stu *b = *((struct stu**)q);
int str=strcmp(a->name,b->name);
if(str>0) return 1;
if(str<0) return -1;
else return 0;
}

int main(int argc, char** argv) {
    int n,k=0;
scanf("%d",&n);
struct stu* data[1000];
for(int i=0 ; i<n ; i++)
{
    char nam[100];
    scanf("%s",nam);
    if(i==0)
    {
        struct stu* temp=(struct stu*)(malloc(sizeof(struct stu)));
        strcpy(temp->name,nam);
        temp->no=1;
        data[k++]=temp;
    }
    else
    {
         int j;
         for(j=0 ; j<k ; j++)
        {
            if(strcmp(data[j]->name,nam)==0)
            {
                data[j]->no++;
                break;
            }
        }
        if(j==k)
        {
            struct stu* temp=(struct stu*)(malloc(sizeof(struct stu)));
            strcpy(temp->name,nam);
            temp->no=1;
            data[k++]=temp;
        }
    }
}
    qsort(data, k, sizeof(struct stu*),cmpfunc);
    for(int i=0 ; i<k ; i++)
{
    printf("%s %d\n",data[i]->name,data[i]->no);
}
return 0;
}

if i give input as

5
abcd
abcd
fgh
fgr
fgh

the output is run failed

melpomene
  • 84,125
  • 8
  • 85
  • 148
return0
  • 215
  • 2
  • 9

1 Answers1

4

With the exception of <cstdio>, your code looks much more like C than C++. Perhaps you meant to use <stdio.h>, heed various warnings against casting malloc, check return values and use a C compiler, but this question is tagged C++. In C++ there are far more sensible idioms, for example:

  • std::string instead of char* / char[].
  • std::cin/std::cout instead of scanf/printf.
  • std::vector instead of struct stu* data[1000].
  • size_t instead of int for array indexes and looping over arrays.
  • new/delete/unique_ptr instead of malloc/free.

using namespace std;

struct stu {
    string name;
    int no;

    stu() {

    }
};

int cmpfunc(const stu *p, const stu *q) {
    return p->name < q->name;
}

int main(int argc, char** argv) {
    size_t n;
    cin >> n;
    vector<stu*> data;

    for (size_t i = 0; i < n; i++)
    {
        string nam;
        cin >> nam;
        if (i == 0)
        {
            auto* temp = new stu();
            temp->name = nam;
            temp->no = 1;
            data.push_back(temp);
        }
        else
        {
            int j;
            for (j = 0; j < data.size(); j++)
            {
                if(data[j]->name == nam)
                {
                    data[j]->no++;
                    break;
                }
            }
            if (j == data.size())
            {
                auto* temp= new stu();
                temp->name = nam;
                temp->no = 1;
                data.push_back(temp);
            }
        }
    }
    std::sort(data.begin(), data.end(), cmpfunc);
    for (int i = 0; i < data.size(); i++)
    {
        cout << data[i]->name << data[i]->no << endl;
        delete data[i];
    }
    return 0;
}
autistic
  • 1
  • 3
  • 35
  • 80
Coral Kashri
  • 3,436
  • 2
  • 10
  • 22