Solved

integer from pointer (Part2)

Posted on 2004-05-02
29
326 Views
Last Modified: 2010-04-15
Ok you can look at the history of this question at this pointer http://oldlook.experts-exchange.com:8080/Programming/Programming_Languages/C/Q_20974846.html

I have to finish two other functions and I am done. I wont post any questions unless and until I am stuck at some stage. Thanks in advance.
0
Comment
Question by:b_vishwajit
  • 15
  • 13
29 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 10973412
Hi b_vishwajit,

I'm not sure what you're trying to solve here.



Kent
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10973675
Kdo dont worry. Just look at the history. I am trying to finish my program.
I said
>>I have to finish two other functions and I am done
If get any problems while implementing I will post them.Thanks.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10973679
Hi  I am using linear probing to insert district numbers into hash table and here is the code snipptet which does that:
if(tableSize!=MAX_DISTRICTS)
                        {
                                if(keyArray[pos]==0)
                                {
                                       keyArray[pos]=districtNo;
                                }
                                else
                                {
                                       while(keyArray[pos]!=0&&pos<=MAX_DISTRICTS)
                                       {
                                              pos++;
                                       }
                                       if(pos==MAX_DISTRICTS)
                                       {
                                              pos=0;
                                              while(keyArray[pos]!=0)
                                              pos++;
                                       }
                                       keyArray[pos]=districtNo;
                                }
                               
                                tableSize++;
                        }

It works without any problems.
MAX_DISTRICTS=31
hashFunction(k)=k%MAX_DISTRICTS.
If there are any problems with this code snippet then please mention it here. Thanks.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10974249
A possible problem:

                                   while(keyArray[pos]!=0&&pos<=MAX_DISTRICTS)
                                      {
                                             pos++;
                                      }
                         //if the loop runs till pos<=MAX_DISTRICTS,here pos=MAX_DISTRICTS+1
                        //The following if wont execute and it will try to insert at keyarray[MAX+1]
                        //which is not allocated for you.
                                      if(pos==MAX_DISTRICTS)
                                      {
                                             pos=0;
                                             while(keyArray[pos]!=0)
                                             pos++;
                                      }
                                      keyArray[pos]=districtNo;

You store in keyarray from 0 or 1?
Since the initial value is 0 in all keyarray values,i have to assume that you dont allow 0 as a district number,in which case you'd be storing from 1.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10974490
In an array elements are stored from 0 right? That is they are indexed starting from 0.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10974498
Yeha I get your point now. Instead of using <= I should just use < and that will solve that minor problem. Is that what you meant?
Thanks.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10974555
Yeah.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10974600
ok. Now I have finished everything. Just gimme sometime. I am debugging my code.If there are errors I would like to know them:
http://www.geocities.com/b_vishwajit/
You can download my file from the above link and the name of the file is schools.c. If you have any problems downloading let me know. I especialy want to know if delete() function and displaySchoolList() function are working properly.Thanks a lot.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10974608
Input format:

INPUT FORMAT:


No duplicate district numbers into the hashtable or duplicate school names into the same linked list.
User enters the  data of the type that is to be expected (this means for strings user will enter a string and for ints user will enter an int).
only use positive integers will be used to insert into the hashtable so you will not need to check that case. However, you will still need to make sure that all menu selections are in range.
Each school name has a maximum of 25 printable characters and will not contain any spaces.
School name is case-sensitive.
Thanks.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10974616
I see that everything is working properly. Did you find any errors.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10974718
Found 2 bugs:

First,if you delete the first school in the list for a district,it displays some junk values for the first node.

Second,If you enter districts >31,it hashes correctly but when displaying the school list,it should display the district number not the hashed index number.

For e.g. if you enter district 31 and then display the school list,it prints School list for district:0


0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10974727
Also,in your scanf after an invalid selection,you've missed out the &.
while(choice!=1.......
{
scanf("%d",&choice);
                   ^
}
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10974732
>For e.g. if you enter district 31 and then display the school list,it prints School list for >district:0

The list is ok.only the number displayed is incorrect
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10974759
Ok I am fixing them now. Just stay here. Thanks.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 5

Author Comment

by:b_vishwajit
ID: 10974803
Allright I fixed two problems. Left with one more i.e. option 4 prints a school list even thought there are no schools in the list. How can I avoid that? Thanks.
You can download the updated file from the same link.Thanks.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10974816
For deletion of first node,the problem is that distArray[] for that district number still points to the node that has now been deleted.

You need to pass the district number to the delete function so that you can access the distArray[] at the right position.

I have done the debugging for you but i wont give it to you.
Its better that you do it yourself.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10974822
But it displays an empty list.

Thats allright.If you want it to display that there are no schools in the list,just check the corresponding distArray[] value for that district.If its NULL,there arent any schools.

0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10974847
I dont see any changes in your delete function.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10974919
I have almost lost my patience. But I know what changes I have to make in delete. I will work on that now. Thanks.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10974936
I have made some changes to the delete function(). Can you please check it.

delete(struct node *q,char* findName,int dist)
{
    struct node *old, *temp;
   
    temp=q;
   
    while(temp!=NULL)
    {
        if(strcmp(temp->name,findName)==0)
        {
                if(temp==q)
                {
                      q=temp->next;
                      free(temp);
                      distArray[hashFunction(dist)]=q;
                      return;
                }
               
                else
                {
                      old->next=temp->next;
                      free(temp);
                      distArray[hashFunction(dist)]=old->next;
                      return;
                }
               
        }
        else
        {
                old=temp;
                temp=temp->next;
        }
    }
     printf("\nSchool named %s not found\n", findName);
}
Thanks.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10974972
Hi,

you need to change the distArray value only for the first node.

This is what i did:

void delete(struct node *q,char* findName,int dno)
{
    struct node *old, *temp;

    temp=q;

    while(temp!=NULL)
    {
      if(strcmp(temp->name,findName)==0)
      {
            if(temp==q)
            {
                  q=temp->next;
                  free(temp);
                  distArray[dno]=q; //set distArray to point to next node
                  return;
            }

            else
            {
                  old->next=temp->next;
                  free(temp);
                  return;
            }

      }
      else
      {
            old=temp;
            temp=temp->next;
      }
    }
     printf("\nSchool named %s not found\n", findName);
}

and call it like:
delete(distArray[hashFunction(districtNo)],sname,hashFunction(districtNo));

0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 500 total points
ID: 10974982
Because you need to change distArray only when the first node is deleted.
In other cases,the start node remains the same so no changes to the distArray.

I've tested your program and its working fine now.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10974992
Doing the same thing your way:

       if(strcmp(temp->name,findName)==0)
       {
               if(temp==q)
               {
                     q=temp->next;
                     free(temp);
                     distArray[hashFunction(dist)]=q;
                     return;
               }
               
               else
               {
                     old->next=temp->next;
                     free(temp);
//                     distArray[hashFunction(dist)]=old->next;
//You dont need this so remove it
                     return;
               }

Here,you'd call with:
delete(distArray[hashFunction(districtNo)],sname,districtNo);

0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10975003
>>and call it like:
delete(distArray[hashFunction(districtNo)],sname,hashFunction(districtNo));

I am actualy passing the districtNo directly to delete() and then within delete() I am calling hashFucntion(). Either way it should be ok right?
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10975011
Yeah.
See my last post.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10975117
Ok so thats it and I am done. Now I am gonna install NFS underground and have some break. Thanks a lot ankuratvb.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10975129
I am going to request in CS to delete all solid code in both the discussions. If you have any objections let me know. Thanks.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10975156
No objections as such but why do you want to do that.

0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10975236
Actualy to go along with the rules of my course. I am not supposed to paste any part of the code of my hw on any forums. Thanks.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now