I have error in my program.I want to display name in order by using radix sort

Recommended Answers

All 11 Replies

Well, we can't guess what the problem is. Show us you're code, show us the error, and tell us what you think.

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>

#define MAX 10

struct std{
char name[20];
int age,year;
};

class radixsort{
    struct std arr[MAX];
    int n,i;
    public:
    int a[];
    int b[];

    void getdata();
    void showdata();
    void sortLogic();
};

void radixsort :: getdata(){
    cout<<"How many elements you require : ";
    cin>>n;
    for(int i=0;i<n;i++){

cout<<"enter name: ";
        gets(arr[i].name);

cout<<"enter age: ";
        cin>>arr[i].age;

cout<<"enter year: ";
        cin>>arr[i].year;

cout<<endl;

}
}

void radixsort :: showdata(){
    cout<<"\n--Display--\n";
    for(int i=0;i<n;i++){
        cout<<"name:"<<arr[i].name<<endl;
        cout<<"age:"<<arr[i].age<<endl;
        cout<<"year:"<<arr[i].year<<endl;
}
}

void radixsort :: sortLogic(){
int m=a[0],exp=1;
for(int i=1;i<n;i++)
{
if(strcmp(arr[i].name,arr[i+1].name)>m)
m= strcmp(arr[i].name,arr[i+1].name);
}
while(m/exp>0){
  int bucket[10]={0};
  for(i=0;i<n;i++)
  bucket[strcmp(arr[i].name,arr[i+1].name)/exp%10]++;

  for(i=1;i<10;i++)
   bucket[i]+=bucket[i-1];

   for(i=n-1;i>=0;i--)
   b[--bucket[strcmp(arr[i].name,arr[i+1].name)/exp%10]]=strcmp(arr[i].name,arr[i+1].name);

   for(i=0;i<n;i++)
    strcmp(arr[i].name,arr[i+1].name) == b[i];
    exp*=10;



 }
}

void main(){
    clrscr();
    cout<<"\n*****Radix Sort*****\n";
    radixsort obj;
    obj.getdata();
    obj.sortLogic();
    obj.showdata();
    getch();
}

this is my program in c++

What is the error you are getting?

I think the error in this part .when i run the program stopped here

for(i=n-1;i>=0;i--)
   b[--bucket[strcmp(arr[i].name,arr[i+1].name)/exp%10]]=strcmp(arr[i].name,arr[i+1].name);
   for(i=0;i<n;i++)
    strcmp(arr[i].name,arr[i+1].name) == b[i];
    exp*=10;
> include <iostream.h>

C++ standard library headers do not need ".h". Remove them.

> include <stdio.h>

Why are you using this when you have iostream? Use iostream instead.

> include <conio.h>

Do you work for a museum? You should have stopped using this library 20 years ago. It is unsupported.

> include <string.h>

Same thing with the ".h"

> gets(arr[i].name);

Use cin.

> clrscr();

Is this part of conio? Remove it.

> void main(){

Main always returns `int`. This is incorrect, and will not compile on a modern compiler. This might be your error

> getch();

Is this part of conio? Remove it. If you need to pause it before closing, how about `cin.get();`?

It looks like you might have dug up your C++ book from an ancient egyptian tumb. Put it in a meuseum for display, and make a load of cash. You can use said cash to buy a modern book on C++.

PS. I have no idea why the formatting of this post is so strange. Oh well.

Are you getting an infinate loop?

for(i=n-1;i>=0;i--)
....
    for(i=0;i<n;i++)

Look at how i is changing here.

This program based in what we study but here i want to sort the name. when i sort the integer type the program run correctly

Oh good, your learning history.

Still, since you just want to sort names, I would suggest you remove conio.h, drop the .h's, change to int main, etc, etc.

Also, I feel like you'll have an easier easier time using C++ Strings as opposed to C-style strings.

This line is indeed strange:

strcmp(arr[i].name,arr[i+1].name)/exp%10

Why modulo 10? Arn't you dealing with the alphabet? Also, are you aware of what strcmp returns?

I did what you said but still error in this line:

bucket[strcmp(arr[i].name,arr[i+1].name)/exp%10]++;

how can i change it

You wrote all of this code, so surely you understand how radix sort works, correct? Or did you not write all of that code?

If you want to fix that line of code, then you'll first need to figure out what it's supposed to be doing. HINT: it involves finding the nth character in a string.

Once you figure out what it's supposed to be doing, it should be easy to fix. If you have problems, let us know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.