Home > Back-end >  I'm getting this warning sign Array index 4001 is past the end of the array (which contains 400
I'm getting this warning sign Array index 4001 is past the end of the array (which contains 400

Time:10-27

I have two questions

First: When i try to run the code it gives me a warning where it says "Array index 4001 is past the end of the array (which contains 4001 elements)"

Second: I want to read the words from the file and then pass them through the function so i can add the words to the hash table and index them accordingly and print the count of the unique words from the text file. the size function does that. can someone please help me with this

#include <iostream>
#include <string>
#include <fstream>

#define HASHSIZE 4001

using namespace std;

class entry {
 public:
  string word;

  int frequency;

  entry() { frequency = 0; }
};

class Hashtable {
 private:
  entry entryArr[HASHSIZE];

  int updateArr[HASHSIZE];

  int costArr[HASHSIZE];

  int sizeUnique = 0;

  int probeCount;

  int updateCount;

 public:
  int HashKey(string key)

  {
    int totalsum = 0;

    // this function is to assign every word a key value to be stored against.

    for (int i = 0; i < key.length(); i  ) totalsum  = int(key[i]);

    return (totalsum % HASHSIZE);
  }

  void update(string key) {
    int k = HashKey(key);

    if (entryArr[k].frequency == 0) {
      entryArr[k].frequency  ;
      updateCount  ;
      probeCount  ;
      sizeUnique  ;
    }
    // function to enter the unique words in the array
    else if (entryArr[k].word == key) {
      entryArr[k].frequency  ;
      probeCount  ;
    }

    while (entryArr[k].frequency != 0 && entryArr[k].word != key) {
      k  ;
    }
    if (entryArr[k].word == key) {
      entryArr[k].frequency  ;
    } else {
      entryArr[k].word = key;
    }
    sizeUnique  ;
    updateCount  ;
    probeCount  ;
  }

  int probes() {
    costArr[HASHSIZE] = probeCount;
    return probeCount;
  }

  int size()  // function to count the total number of unique words occuring
  {
    int count = 0;
    updateArr[HASHSIZE] = updateCount;
    for (int i = 0; i < HASHSIZE; i  )
      if (updateArr[HASHSIZE] != 0) {
        count = costArr[i] / updateArr[i];
      }
    cout << count;
    return count;
  }
};

int main() {
  entry e;
  Hashtable h;

  ifstream thisfile("RomeoAndJuliet.txt");

  if (thisfile.is_open()) {
    while (!thisfile.eof) {
      h.update(e.word);
    }

    thisfile.close();
    cout << "The total number of unique words are: " << h.size();
  }

  return 0;
}

CodePudding user response:

An array with 4001 elements has valid indexes 0,1,...,3999,4000 as C is indexing from 0.

CodePudding user response:

When i try to run the code it gives me a warning where it says "Array index 4001 is past the end of the array (which contains 4001 elements)"

This is because array index starts with 0 instead of 1. And so an array of size 4001 can be safely indexed(accessed) upto 4000 and not 4001.

I want to read the words from the file and then pass them through the function so i can add the words to the hash table and index them accordingly and print the count of the unique words from the text file

The below program shows how to do this. The program shown below counts how many times a given word occurred in a given input.txt file and then print that count infront of the word.

#include <iostream>
#include <map>
#include <sstream>
#include<fstream>
int main() {
    std::string line, word;
   //this map maps the std::string to their respective count
    std::map<std::string, int> wordCount;
    
    std::ifstream inFile("input.txt");
    
    
    if(inFile)
    {
        while(getline(inFile, line, '\n'))        
        {
            
            std::istringstream ss(line);
            
            while(ss >> word)
            {
                //std::cout<<"word:"<<word<<std::endl;
            wordCount[word]  ;
            }      
        }    
    }
    
    else 
    {
        std::cout<<"file cannot be opened"<<std::endl;
    }
    
    inFile.close();
    std::cout<<"Total unique words are: "<<wordCount.size()<<std::endl;
    for(std::pair<std::string, int> pairElement: wordCount)
    {
        std::cout << pairElement.first <<"-" << pairElement.second<<std::endl;
    }
    return 0;
}

The output of this program can be seen here.

Note that(as shown in above example) there is no need to create a separate class for the purpose given in your second question. We can do this(as shown above) literally using 4 to 6 lines(excluding opening and closing the file) of code.

  • Related