H
H
HNKHENM2014-12-02 01:36:01
Programming
HNKHENM, 2014-12-02 01:36:01

Where could you not see?

It was necessary to write a program that reads words from a file, and then calculate the address from these words using the hash function and display the hashing results. Initially, it was necessary to write a program in Pascal, in fact, it works properly on it, but trying to write it using c + + errors pop up at the compilation stage, here is one of them:
First stage exception handling at 0x6E1808A5 (msvcr120d.dll) in Project7.exe: 0xC0000005: Access violation while reading at 0xCCCCCCCC.
Unhandled exception at 0x6E1808A5 (msvcr120d.dll) in Project7.exe: 0xC0000005: Access violation while reading at 0xCCCCCCCC.
What can be a snag?

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
const int n = 15;
int hashStr(char str[n]){

  unsigned int i;
  int key = 0;

  for (i = 1; i < strlen(str); i++){

    key += (int)str[i];

  }
  return (key % n) + 1;
}
int main()
{
  setlocale (LC_ALL, "RUS");
  bool b;
  unsigned int count, i;
  string rec[n];
  fstream F;
  char s[n];
  int j;
  count = 0;



  F.open("test.txt");

  for (j = 1; j < n; j++){

    rec[j] = "";

  }

  if (F){

    while (!F.eof()){

      b = false;
      F >> s;
      j = hashStr(s);

      if (rec[j] == "" || s == rec[j]) {
      
        rec[j] = s;
        b = true;
      
      }
      else{
      
        for (count = 1; count <= n; count++){
        
          if (rec[(j + count) % n + 1] == "" || s == rec[(j + count) % n + 1]){
          
            j = (j + count) % n + 1;
            rec[j] = s;
            b = true;
            break;
          
          }
        
        }
      
      }
      if (b == true){
      
        cout << s << " Найдено в позиции " << j << " количество сравнений " << count <<endl ;

      }
      else{
      
        cout << s << " не найдено , количество сравнений "<< count << endl;
        count = 0;
      
      }

    }
    F.close();
  }

  else cout << " Файл не существует" << endl;

  system("pause");

  return 0;

}

If you need the contents of a text file, then here:
Audi
Saab
Bentley
Infinity
Hummer
Hummer
Fiat
Jeep
Cadillac
VolksWagen
Daewoo
Mersedes
Dodge
Mitsubishi
Lamborgini

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Armenian Radio, 2014-12-02
@HNKHENM

  1. Arrays in C start at 0
  2. Initialization of strings immediately after creation is meaningless, they are already empty
  3. The rotten Pascal style of declaring one index variable for all cycles led you to the main mistake - the counter must be declared directly in the for(int j=0....; statement, but your j after the cycle is equal to n and thus is outside the array boundary, hence the crash of the program.
  4. Another place to fall F >> s; - if the string is longer than 14(!)* characters - there will be an overflow
  5. Since when did elementwise summation become known as hashing? Collisions will be huge. C++11 for example has hashing built in. In any case, it is better to pass a constant reference to std::string to the input of the hashing function rather than an array of characters

HNKHENM : if you are interested in improving this code:
______________________________________________________
*Don't forget that the very last character in a line is the end marker \0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question