-3

I need to solve a c++ problem but something doesn't work and i don't know why.

N numbers are given, divided in k sequences. I need to find out which sequences are equal, knowing that k divides n. is the problem with some values as an example: Here is the problem with some values as an example. (it's in romanian)

I tried something like this:

#include <bits/stdc++.h>
using namespace std;
bool compare(int a[], int b[], int n)
{
    for(int i=1;i<=n;i++)
        if(a[i]!=b[i])
            return false;
    return true;
}
int main()
{
    int n, k;
    int i, j, secv;
    cin>>n>>k;
    int flag=1, v[n];
    for(i=1;i<=n;i++)
        cin>>v[i];
    secv=n/k;
    for(i=1;i<k && flag==1;i++)
        for(j=i+1; j<=k && flag==1; j++)
            if(compare(v+i*secv, v+j*secv, secv)==true)
                {
                    cout<<i<<" "<<j;
                    flag=0;
                }
    if(flag==1) cout<<"NU";
    return 0;
    

}
Lauraa
  • 1
  • 2
  • 3
    Almost always ,yes. Just about everything in C++ is origin 0. – user4581301 Mar 04 '22 at 19:38
  • 4
    @Lauraa Variable length arrays like this int flag=1, v[n]; is not a standard C++ feature. Instead use std::vector – Vlad from Moscow Mar 04 '22 at 19:38
  • 1
    @Lauraa This loop for(i=1;i<=n;i++) cin>>v[i]; invikes undefined behavior because the valid range of indices is [0, n) – Vlad from Moscow Mar 04 '22 at 19:39
  • A `<=` in the exit condition of a for loop is a red flag. It's almost always wrong, so when you see it, you should examine the code closely. – user4581301 Mar 04 '22 at 19:40
  • 3
    Combining `#include ` with `using namespace std;`. is a fast way to shoot yourself in the foot. There are tens of thousands of identifiers in the C++ Standard library and that combination includes all of then and pulls them into conflict with whatever you have in the global namespace where they can give you very nasty surprises. – user4581301 Mar 04 '22 at 19:42
  • 1
    This looks a lot like you're trying to learn C++ through competition websites. That's a hard road to travel. The goal of competition code is to win the competition, not look or play nice. It's hard to learn from because it is usually extremely cryptic and what you do learn is often unsafe, breaking language rule right and left. The competition doesn't care if the program is invalid so long as it produces the correct output the one time the program is run. – user4581301 Mar 04 '22 at 19:46
  • 2
    C++ is an extremely complicated language, so prefer to learn C++ from material that's intended to teach like [a good set of books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Once you've learned the language fundamentals, then start competing for fun or profit. – user4581301 Mar 04 '22 at 19:48
  • @Lauraa It is unclear what the program should do. Whether it should determine that there are at least two subsequences equal each other ir it should count how many subsequences equal each other or to output all equal subsequences. – Vlad from Moscow Mar 04 '22 at 19:54
  • Is the code in this question just an example of code that doesn't access the `[0]` element of an array? Your question here is simply _"is it wrong?"_, correct? – Drew Dormann Mar 04 '22 at 20:52
  • I really want to learn c++ because of my exams but idk which type of exercices i should do. I know the theory but i need to practice. Thank you for your help, ill rewrite the code :') – Lauraa Mar 04 '22 at 22:54

1 Answers1

3

If your array is of size k and you are iterating the first element at index 1 then there are two problems:

  • Your array will only be able to save k-1 elements because in memory an array's first element is at index 0.

  • Some of the default array functions like begin and size will include 0 index so it might produce unexpected results.

How an array is saved in memeory An array name is just a placeholder to the continuous memory reference. So if you's starting your array with 1 then you're skipping through the first index.

The best practice is to start an array with index 0.

Rupakshi
  • 31
  • 3