-3

I was practicing some questions from hacker rank and got stuck in this question called sparse-arrays. (https://www.hackerrank.com/challenges/sparse-arrays/problem) I am a beginner and i am not able to find out the error in my code. Please help. Thank you

I think I am not comparing the strings correctly. I tried using compare function but still it did not work.

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

int main(){
    int n;
    cin>>n;
    string strings[n];
    for(int i=0; i<n; i++){
        cin>>strings[i];
    }
    int m;
    cin>>m;
    string queries[m];
    for(int i=0; i<m; i++){
        cin>>queries[i];
    }

     //comparing
        for(int i=0; i<m; i++)
    {
        int count=0;
        for(int j=0; j<n; j++)
        {
            if(queries[i]==strings[j])
                    count++;
        }
        cout<<count<<endl;
    }
}

The output should be the number of times the strings in (query) that have appeared in the (strings) but my program is getting terminated please help.

  • 2
    `string strings[n];` is high risk. So high that it's [not a part of Standard C++](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Given a suitably high value for `n`, this will run your program out of Automatic storage and probably cause a Stack Overflow. – user4581301 Sep 12 '19 at 17:52
  • @NeilButterworth Please suggest me where should i practise from then, I'm really confused and trying hard to learn. – Yash Agrawal Sep 12 '19 at 17:55
  • Suggestion: Back up your code. Then remove code bit by bit until you have a program that compiles, still crashes when the same input is given, and does nothing else. Usually reducing the noise of the good code around the bad code makes it easier to spot and fix the problem. – user4581301 Sep 12 '19 at 17:55
  • 3
    @YashAgrawal "_Please suggest me where should i practise from then_" From a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Sep 12 '19 at 17:55
  • @user4581301 How should i take the input then sir please explain if it's possible. – Yash Agrawal Sep 12 '19 at 17:56
  • @user4581301 Thank you for the suggestions. Please suggest a good book I'm really messed up. – Yash Agrawal Sep 12 '19 at 17:57
  • @AlgirdasPreidžius Thankyou so much Sir. – Yash Agrawal Sep 12 '19 at 17:59
  • [Link to excelent tutorial on debugging small programs.](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – user4581301 Sep 12 '19 at 18:02
  • Another very handy tool is the debugger that almost certainly came with your development environment. If your environment doesn't have a debugger, get one that does. The debugger does a lot of things, but most important is it lets you to control the rate of the program's execution, allowing the slow human brain keep up and watch what the program does. When you observe the program running, keep an eye out for it doing the unexpected, like taking the wrong path or putting the wrong value in a variable. The unexpected is almost always a bug and when it isn't your expectations are wrong. Fix both. – user4581301 Sep 12 '19 at 18:06
  • @user4581301 Thank you so much for the suggestions and for showing me the right way to learn things rather than saying the answer. I appreciate each bit of your help, thank you. – Yash Agrawal Sep 12 '19 at 18:10
  • The following topic explains why VLAs are not part of c++ and gives you the c++ alternative: [https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – drescherjm Sep 12 '19 at 18:22
  • The inner loop of your comparison code is infinite if `n` is positive (look carefully at the end condition - it is comparing two variables that are unchanged in the loop body). That will cause `count` to overflow, which causes undefined behaviour. Also, if for some reason, the loop terminates, `main()` also terminates, and - on many systems - the output window is destroyed before a mere human can see output. Add some code immediately before the end of `main()` that flushes `cout` and waits for input before `main()` returns. – Peter Sep 12 '19 at 19:02
  • @Peter Thankyou sir, i made a really silly mistake i get it. I have to practice a lot more. – Yash Agrawal Sep 12 '19 at 19:09

1 Answers1

0
for(int j=0; i<n; j++)

should be

    for(int j=0; j<n; j++)
Jeffrey
  • 11,063
  • 1
  • 21
  • 42
  • I feel really embarrassed. This was such a silly mistake and me asking this on stack overflow further makes me feel more embarrassed. Thank you so much for the help Sir. – Yash Agrawal Sep 12 '19 at 19:12
  • I had a question in my mind. Is it a better practice to use vector instead of VLAs in C++. – Yash Agrawal Sep 12 '19 at 19:20
  • yes, definitely. I didn't address VLA as it was not your issue here, but still, stay away from them, and use vectors. – Jeffrey Sep 12 '19 at 19:26
  • Sorry if it's a bad question. Can i ask why there is no risk of shortage of memory while using vectors but there is a high risk when using VLAs. – Yash Agrawal Sep 12 '19 at 19:37
  • 1
    Because VLAs are allocated on the (limited) stack memory whereas vectors use heap storage, which is typically much more plentiful. – Jeffrey Sep 12 '19 at 19:42
  • 1
    @YashAgrawal typical desktop today allows 1 MB to 10 MB for the Automatic storage (usually a stack). Dynamic storage (often implemented with a heap) is generally limited by the amount of addressable memory the computer has, typically in the gigabytes. A caveat with the term addressable: if you're using a 32 bit compiler the addressable storage is usually 2 or 4 GB (31 or 32 bits) no matter how much memory the computer has. – user4581301 Sep 12 '19 at 19:55