0

in my program I've been trying to get an input from user, and check if the letters in it were small or capital, and then make a prefix array, where I would check if the number of next small and big letters (counting from the begging of the word) was the same. I wanted to do that by assignig big letters value 1 and -1 to small ones.

My problem is that after I load the word from user to my array, and then want to make prefix array, the numbers don't match - in my opinion.

My input is:

STaSzIc

And my output is:

 1 1 -1 1 -1 1 -1 

1 1
2 2
0 3
0 4
0 5
0 6
0 7

Why after 3'rd value, next don't work? Here's my code:

#include <iostream>

using namespace std;

int main()
{
    string Caps;
    int tab[1000000];
    cin >> Caps;
    for(int i = 1; i <= Caps.length(); i++){
        if(Caps[i - 1] <= 90){
            tab[i] = 1;
            cout<<tab[i]<<' ';
        }else{
            tab[i] = -1;
            cout<<tab[i]<<' ';
            
        }
        
    }
    cout<<endl;
    cout<<endl;
    int prefiksy[1000000];
    
    for(int i = 1; i <= Caps.length(); i++){
        prefiksy[i] = tab[i] + tab[i - 1];
        cout<<prefiksy[i]<<" "<<i<<endl;
    }
    
}

I was hoping for such result:

1 1 -1 1 -1 1 -1 
    
    1 1
    2 2
    1 3
    2 4
    1 5
    2 6
    1 7
8
  • + tab[i - 1]; you don't initialize tab[0] before use.
    – 3CxEZiVlQ
    Commented Oct 27, 2022 at 15:24
  • Isn't it 0 by deafult? Commented Oct 27, 2022 at 15:25
  • 1
    No, of course not.
    – 3CxEZiVlQ
    Commented Oct 27, 2022 at 15:26
  • tab[i] + tab[i - 1] only calculates the difference between the current and previous element, you don't keep a total tally if that's what you mean
    – perivesta
    Commented Oct 27, 2022 at 15:27
  • Also currently your prefiksy array is useless since you never read from it, maybe that's the error
    – perivesta
    Commented Oct 27, 2022 at 15:28

1 Answer 1

0

The following code works:

#include <iostream>

using namespace std;

int main()
{
    string Caps;
    int tab[100]={0};
    cin >> Caps;
    for(int i = 1; i <= Caps.length(); i++){
        if(Caps[i - 1] <= 90){
            tab[i] = 1;
            cout<<tab[i]<<' ';
        }else{
            tab[i] = -1;
            cout<<tab[i]<<' ';

        }

    }
    cout<<endl;
    cout<<endl;
    int prefiksy[100];
    prefiksy[1] = tab[1];
    cout<<prefiksy[1]<<" "<<1<<endl;
    for(int i = 2; i <= Caps.length(); i++){
        prefiksy[i]=prefiksy[i-1]+tab[i];
        cout<<prefiksy[i]<<" "<<i<<endl;
    }

}

Check this statement prefiksy[i] = tab[i] + tab[i - 1];.The logic of the code is wrong. Do trace table for prefiksy[i]. You are trying to add only value of array tab to prefiksy which is not the intention of the algorithm. You need to mind the value of array 'prefiksy' at each iteration as well because it is going to be the value for the upcoming series in the question. Also it is always better to initialize an array after it's creation. Practice more, you'll understand and get better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.