Build set using hash-map on C++











up vote
0
down vote

favorite












I'm trying to build set using hash-map, but testing system sends me wrong answers. I don't have access to tests, but on my tests code works fine. I don't have any ideas for new tests, maybe you'll have some?
This is my code:



#include <iostream>
#include <fstream>
using namespace std;

int h1(int number) {
number %= 1000003;
return abs(number);
}

int h2(int number) {
number = number % 999991 + 12;
return abs(number);
}

int Insert(int * Hash_table,bool * Deleted,int& number) {
int x = h1(number);
int y = h2(number);
for(int i = 0; i < 1000003;i++) {
if(Hash_table[x] == NULL || Deleted[x]) {
Hash_table[x] = number;
Deleted[x] = false;
break;
}
x = (x + i * y) % 1000003;
}
}

string Exists(int * Hash_table,bool * Deleted,int& number) {
int x = h1(number);
int y = h2(number);
for(int i = 0; i < 1000003;i++) {
if(Hash_table[x] == 0 && number == 0 && !Deleted[x])
return "truen";
if(Hash_table[x] != NULL)
if(Hash_table[x] == number && !Deleted[x] ) {
return "truen";
}
if(Hash_table[x] == NULL || Deleted[x])
return "falsen";

x = (x + i * y) % 1000003;

}

}
int Delete(int * Hash_table,bool * Deleted,int& number){
int x = h1(number);
int y = h2(number);
for(int i = 0; i < 1000003;i++){
if(Hash_table[x] == 0 && number == 0){
Deleted[x] = true;
break;
}
if(Hash_table[x] != NULL) {
if(Hash_table[x] == number) {
Deleted[x] = true;
break;
}

x = (x + i * y) % 1000003;
}
else
return 0;
}
}

int main () {
int Hash_table[1000003];
bool Deleted[1000003];
ifstream input("set.in");
ofstream output("set.out");
string command;
int number;
while(true) {
input >> command;
if(input.eof())
break;
if(command == "insert") {
input >> number;
Insert(Hash_table,Deleted,number);
}
if(command == "delete") {
input >> number;
Delete(Hash_table,Deleted,number);
}
if(command == "exists") {
input >> number;
output << Exists(Hash_table,Deleted,number);
}
}
}


I have to realise such commands as "input","exists" and "delete". I think logic of code is nice, but I'm not sure about hash functions.
Updated: Add if statements for 0 and now h1 and h2 return absolute values.
But now I have problems in 4th test.










share|improve this question




















  • 1




    Welcome to Stack Overflow! Talk us through the sorts of local testing that you've done on your end. If you haven't written any local tests, I'd start there - it's much, much easier to debug things when you can see what the program is doing and how the behavior differs from what you expect than it is to submit code to a black box testing framework and hope that it turns out well.
    – templatetypedef
    Nov 10 at 20:25










  • Okay, for example, code works fine for such test: insert 2 insert 5 insert 3 exists 2 exists 4 insert 2 delete 2 exists 2 Output: true false false
    – Василий Югай
    Nov 10 at 20:29








  • 1




    (1) int Hash_table[1000003]; is seriously dangerous. Stack is small, life is short, allocate dynamically. (2) What about 0? Is it a number? insert 0 then exists 0.
    – n.m.
    Nov 10 at 20:40










  • (1) I know limit of number of input numbers (1000000), that's why I used const value. (2) Yes, with 0 my code doesn't work, it's because of NULL? How can I change if operators without using NULL?
    – Василий Югай
    Nov 10 at 20:51








  • 1




    s/NULL/nullptr/
    – Jesper Juhl
    Nov 10 at 20:55















up vote
0
down vote

favorite












I'm trying to build set using hash-map, but testing system sends me wrong answers. I don't have access to tests, but on my tests code works fine. I don't have any ideas for new tests, maybe you'll have some?
This is my code:



#include <iostream>
#include <fstream>
using namespace std;

int h1(int number) {
number %= 1000003;
return abs(number);
}

int h2(int number) {
number = number % 999991 + 12;
return abs(number);
}

int Insert(int * Hash_table,bool * Deleted,int& number) {
int x = h1(number);
int y = h2(number);
for(int i = 0; i < 1000003;i++) {
if(Hash_table[x] == NULL || Deleted[x]) {
Hash_table[x] = number;
Deleted[x] = false;
break;
}
x = (x + i * y) % 1000003;
}
}

string Exists(int * Hash_table,bool * Deleted,int& number) {
int x = h1(number);
int y = h2(number);
for(int i = 0; i < 1000003;i++) {
if(Hash_table[x] == 0 && number == 0 && !Deleted[x])
return "truen";
if(Hash_table[x] != NULL)
if(Hash_table[x] == number && !Deleted[x] ) {
return "truen";
}
if(Hash_table[x] == NULL || Deleted[x])
return "falsen";

x = (x + i * y) % 1000003;

}

}
int Delete(int * Hash_table,bool * Deleted,int& number){
int x = h1(number);
int y = h2(number);
for(int i = 0; i < 1000003;i++){
if(Hash_table[x] == 0 && number == 0){
Deleted[x] = true;
break;
}
if(Hash_table[x] != NULL) {
if(Hash_table[x] == number) {
Deleted[x] = true;
break;
}

x = (x + i * y) % 1000003;
}
else
return 0;
}
}

int main () {
int Hash_table[1000003];
bool Deleted[1000003];
ifstream input("set.in");
ofstream output("set.out");
string command;
int number;
while(true) {
input >> command;
if(input.eof())
break;
if(command == "insert") {
input >> number;
Insert(Hash_table,Deleted,number);
}
if(command == "delete") {
input >> number;
Delete(Hash_table,Deleted,number);
}
if(command == "exists") {
input >> number;
output << Exists(Hash_table,Deleted,number);
}
}
}


I have to realise such commands as "input","exists" and "delete". I think logic of code is nice, but I'm not sure about hash functions.
Updated: Add if statements for 0 and now h1 and h2 return absolute values.
But now I have problems in 4th test.










share|improve this question




















  • 1




    Welcome to Stack Overflow! Talk us through the sorts of local testing that you've done on your end. If you haven't written any local tests, I'd start there - it's much, much easier to debug things when you can see what the program is doing and how the behavior differs from what you expect than it is to submit code to a black box testing framework and hope that it turns out well.
    – templatetypedef
    Nov 10 at 20:25










  • Okay, for example, code works fine for such test: insert 2 insert 5 insert 3 exists 2 exists 4 insert 2 delete 2 exists 2 Output: true false false
    – Василий Югай
    Nov 10 at 20:29








  • 1




    (1) int Hash_table[1000003]; is seriously dangerous. Stack is small, life is short, allocate dynamically. (2) What about 0? Is it a number? insert 0 then exists 0.
    – n.m.
    Nov 10 at 20:40










  • (1) I know limit of number of input numbers (1000000), that's why I used const value. (2) Yes, with 0 my code doesn't work, it's because of NULL? How can I change if operators without using NULL?
    – Василий Югай
    Nov 10 at 20:51








  • 1




    s/NULL/nullptr/
    – Jesper Juhl
    Nov 10 at 20:55













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to build set using hash-map, but testing system sends me wrong answers. I don't have access to tests, but on my tests code works fine. I don't have any ideas for new tests, maybe you'll have some?
This is my code:



#include <iostream>
#include <fstream>
using namespace std;

int h1(int number) {
number %= 1000003;
return abs(number);
}

int h2(int number) {
number = number % 999991 + 12;
return abs(number);
}

int Insert(int * Hash_table,bool * Deleted,int& number) {
int x = h1(number);
int y = h2(number);
for(int i = 0; i < 1000003;i++) {
if(Hash_table[x] == NULL || Deleted[x]) {
Hash_table[x] = number;
Deleted[x] = false;
break;
}
x = (x + i * y) % 1000003;
}
}

string Exists(int * Hash_table,bool * Deleted,int& number) {
int x = h1(number);
int y = h2(number);
for(int i = 0; i < 1000003;i++) {
if(Hash_table[x] == 0 && number == 0 && !Deleted[x])
return "truen";
if(Hash_table[x] != NULL)
if(Hash_table[x] == number && !Deleted[x] ) {
return "truen";
}
if(Hash_table[x] == NULL || Deleted[x])
return "falsen";

x = (x + i * y) % 1000003;

}

}
int Delete(int * Hash_table,bool * Deleted,int& number){
int x = h1(number);
int y = h2(number);
for(int i = 0; i < 1000003;i++){
if(Hash_table[x] == 0 && number == 0){
Deleted[x] = true;
break;
}
if(Hash_table[x] != NULL) {
if(Hash_table[x] == number) {
Deleted[x] = true;
break;
}

x = (x + i * y) % 1000003;
}
else
return 0;
}
}

int main () {
int Hash_table[1000003];
bool Deleted[1000003];
ifstream input("set.in");
ofstream output("set.out");
string command;
int number;
while(true) {
input >> command;
if(input.eof())
break;
if(command == "insert") {
input >> number;
Insert(Hash_table,Deleted,number);
}
if(command == "delete") {
input >> number;
Delete(Hash_table,Deleted,number);
}
if(command == "exists") {
input >> number;
output << Exists(Hash_table,Deleted,number);
}
}
}


I have to realise such commands as "input","exists" and "delete". I think logic of code is nice, but I'm not sure about hash functions.
Updated: Add if statements for 0 and now h1 and h2 return absolute values.
But now I have problems in 4th test.










share|improve this question















I'm trying to build set using hash-map, but testing system sends me wrong answers. I don't have access to tests, but on my tests code works fine. I don't have any ideas for new tests, maybe you'll have some?
This is my code:



#include <iostream>
#include <fstream>
using namespace std;

int h1(int number) {
number %= 1000003;
return abs(number);
}

int h2(int number) {
number = number % 999991 + 12;
return abs(number);
}

int Insert(int * Hash_table,bool * Deleted,int& number) {
int x = h1(number);
int y = h2(number);
for(int i = 0; i < 1000003;i++) {
if(Hash_table[x] == NULL || Deleted[x]) {
Hash_table[x] = number;
Deleted[x] = false;
break;
}
x = (x + i * y) % 1000003;
}
}

string Exists(int * Hash_table,bool * Deleted,int& number) {
int x = h1(number);
int y = h2(number);
for(int i = 0; i < 1000003;i++) {
if(Hash_table[x] == 0 && number == 0 && !Deleted[x])
return "truen";
if(Hash_table[x] != NULL)
if(Hash_table[x] == number && !Deleted[x] ) {
return "truen";
}
if(Hash_table[x] == NULL || Deleted[x])
return "falsen";

x = (x + i * y) % 1000003;

}

}
int Delete(int * Hash_table,bool * Deleted,int& number){
int x = h1(number);
int y = h2(number);
for(int i = 0; i < 1000003;i++){
if(Hash_table[x] == 0 && number == 0){
Deleted[x] = true;
break;
}
if(Hash_table[x] != NULL) {
if(Hash_table[x] == number) {
Deleted[x] = true;
break;
}

x = (x + i * y) % 1000003;
}
else
return 0;
}
}

int main () {
int Hash_table[1000003];
bool Deleted[1000003];
ifstream input("set.in");
ofstream output("set.out");
string command;
int number;
while(true) {
input >> command;
if(input.eof())
break;
if(command == "insert") {
input >> number;
Insert(Hash_table,Deleted,number);
}
if(command == "delete") {
input >> number;
Delete(Hash_table,Deleted,number);
}
if(command == "exists") {
input >> number;
output << Exists(Hash_table,Deleted,number);
}
}
}


I have to realise such commands as "input","exists" and "delete". I think logic of code is nice, but I'm not sure about hash functions.
Updated: Add if statements for 0 and now h1 and h2 return absolute values.
But now I have problems in 4th test.







c++ algorithm hashmap






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 20:49

























asked Nov 10 at 20:19









Василий Югай

42




42








  • 1




    Welcome to Stack Overflow! Talk us through the sorts of local testing that you've done on your end. If you haven't written any local tests, I'd start there - it's much, much easier to debug things when you can see what the program is doing and how the behavior differs from what you expect than it is to submit code to a black box testing framework and hope that it turns out well.
    – templatetypedef
    Nov 10 at 20:25










  • Okay, for example, code works fine for such test: insert 2 insert 5 insert 3 exists 2 exists 4 insert 2 delete 2 exists 2 Output: true false false
    – Василий Югай
    Nov 10 at 20:29








  • 1




    (1) int Hash_table[1000003]; is seriously dangerous. Stack is small, life is short, allocate dynamically. (2) What about 0? Is it a number? insert 0 then exists 0.
    – n.m.
    Nov 10 at 20:40










  • (1) I know limit of number of input numbers (1000000), that's why I used const value. (2) Yes, with 0 my code doesn't work, it's because of NULL? How can I change if operators without using NULL?
    – Василий Югай
    Nov 10 at 20:51








  • 1




    s/NULL/nullptr/
    – Jesper Juhl
    Nov 10 at 20:55














  • 1




    Welcome to Stack Overflow! Talk us through the sorts of local testing that you've done on your end. If you haven't written any local tests, I'd start there - it's much, much easier to debug things when you can see what the program is doing and how the behavior differs from what you expect than it is to submit code to a black box testing framework and hope that it turns out well.
    – templatetypedef
    Nov 10 at 20:25










  • Okay, for example, code works fine for such test: insert 2 insert 5 insert 3 exists 2 exists 4 insert 2 delete 2 exists 2 Output: true false false
    – Василий Югай
    Nov 10 at 20:29








  • 1




    (1) int Hash_table[1000003]; is seriously dangerous. Stack is small, life is short, allocate dynamically. (2) What about 0? Is it a number? insert 0 then exists 0.
    – n.m.
    Nov 10 at 20:40










  • (1) I know limit of number of input numbers (1000000), that's why I used const value. (2) Yes, with 0 my code doesn't work, it's because of NULL? How can I change if operators without using NULL?
    – Василий Югай
    Nov 10 at 20:51








  • 1




    s/NULL/nullptr/
    – Jesper Juhl
    Nov 10 at 20:55








1




1




Welcome to Stack Overflow! Talk us through the sorts of local testing that you've done on your end. If you haven't written any local tests, I'd start there - it's much, much easier to debug things when you can see what the program is doing and how the behavior differs from what you expect than it is to submit code to a black box testing framework and hope that it turns out well.
– templatetypedef
Nov 10 at 20:25




Welcome to Stack Overflow! Talk us through the sorts of local testing that you've done on your end. If you haven't written any local tests, I'd start there - it's much, much easier to debug things when you can see what the program is doing and how the behavior differs from what you expect than it is to submit code to a black box testing framework and hope that it turns out well.
– templatetypedef
Nov 10 at 20:25












Okay, for example, code works fine for such test: insert 2 insert 5 insert 3 exists 2 exists 4 insert 2 delete 2 exists 2 Output: true false false
– Василий Югай
Nov 10 at 20:29






Okay, for example, code works fine for such test: insert 2 insert 5 insert 3 exists 2 exists 4 insert 2 delete 2 exists 2 Output: true false false
– Василий Югай
Nov 10 at 20:29






1




1




(1) int Hash_table[1000003]; is seriously dangerous. Stack is small, life is short, allocate dynamically. (2) What about 0? Is it a number? insert 0 then exists 0.
– n.m.
Nov 10 at 20:40




(1) int Hash_table[1000003]; is seriously dangerous. Stack is small, life is short, allocate dynamically. (2) What about 0? Is it a number? insert 0 then exists 0.
– n.m.
Nov 10 at 20:40












(1) I know limit of number of input numbers (1000000), that's why I used const value. (2) Yes, with 0 my code doesn't work, it's because of NULL? How can I change if operators without using NULL?
– Василий Югай
Nov 10 at 20:51






(1) I know limit of number of input numbers (1000000), that's why I used const value. (2) Yes, with 0 my code doesn't work, it's because of NULL? How can I change if operators without using NULL?
– Василий Югай
Nov 10 at 20:51






1




1




s/NULL/nullptr/
– Jesper Juhl
Nov 10 at 20:55




s/NULL/nullptr/
– Jesper Juhl
Nov 10 at 20:55












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










int h1(int number) {
number %= 1000003;
return number;
}

int h2(int number) {
number = number % 99991 + 1;
return number;
}


Abouve function will give negative output for negative number. That will cause runtime error for Hash_table[x]. See below:



int h1(int number) {
number = (number%1000003 + 1000003) % 1000003;
return number;
}

int h2(int number) {
number = (number % 99991 + 99991) % 99991 + 1;
return number;
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243062%2fbuild-set-using-hash-map-on-c%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    int h1(int number) {
    number %= 1000003;
    return number;
    }

    int h2(int number) {
    number = number % 99991 + 1;
    return number;
    }


    Abouve function will give negative output for negative number. That will cause runtime error for Hash_table[x]. See below:



    int h1(int number) {
    number = (number%1000003 + 1000003) % 1000003;
    return number;
    }

    int h2(int number) {
    number = (number % 99991 + 99991) % 99991 + 1;
    return number;
    }





    share|improve this answer

























      up vote
      0
      down vote



      accepted










      int h1(int number) {
      number %= 1000003;
      return number;
      }

      int h2(int number) {
      number = number % 99991 + 1;
      return number;
      }


      Abouve function will give negative output for negative number. That will cause runtime error for Hash_table[x]. See below:



      int h1(int number) {
      number = (number%1000003 + 1000003) % 1000003;
      return number;
      }

      int h2(int number) {
      number = (number % 99991 + 99991) % 99991 + 1;
      return number;
      }





      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        int h1(int number) {
        number %= 1000003;
        return number;
        }

        int h2(int number) {
        number = number % 99991 + 1;
        return number;
        }


        Abouve function will give negative output for negative number. That will cause runtime error for Hash_table[x]. See below:



        int h1(int number) {
        number = (number%1000003 + 1000003) % 1000003;
        return number;
        }

        int h2(int number) {
        number = (number % 99991 + 99991) % 99991 + 1;
        return number;
        }





        share|improve this answer












        int h1(int number) {
        number %= 1000003;
        return number;
        }

        int h2(int number) {
        number = number % 99991 + 1;
        return number;
        }


        Abouve function will give negative output for negative number. That will cause runtime error for Hash_table[x]. See below:



        int h1(int number) {
        number = (number%1000003 + 1000003) % 1000003;
        return number;
        }

        int h2(int number) {
        number = (number % 99991 + 99991) % 99991 + 1;
        return number;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 7:25









        nightfury1204

        1,30748




        1,30748






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243062%2fbuild-set-using-hash-map-on-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            Bicuculline

            さくらももこ