Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Any help with the problem below?

+4 votes
asked Feb 12 by Nedelea Claudiu (200 points)
This code needs to print the position of the number(-99), if the number is in the array. And if is not in the array it needs to print the array's size. Currently the program prints the position of the number as well as the size of the array.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

    int main(){
        
    vector<int>vec{9,6,5,4,-3,-32,-99,-45,-55,-77};
    int count{};
    bool in_the_vector{false};
        
    while (count <= int(vec.size())) {
             count ++;
    
            if (vec[count] == -99)
 
            cout<<count<<endl;
            in_the_vector == true;
    
    }
    
            if(!in_the_vector){
         // (vec[count] != -99);
            cout<<int (vec.size())<<endl;
            count == int(vec.size());
            
            }    
    return 0;       
    }

2 Answers

+4 votes
answered Feb 12 by xDELLx (7,630 points)
selected Feb 12 by Nedelea Claudiu
 
Best answer

Have you tried to debug the code, there are plenty of debuggers present (one present here actually) which can help you correct the code.

Lines # 15-20 have the bug. Please check how if conditions work without the braces & also check what the difference between = & == operator are.

I see you have used the algorithm header , so I would suggest using the find_if algo , it does the same job ,although without having to worry about minor bugs , writing manual loops, etc.

my implementation below:


#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

int main(){

        vector<int>vec{9,6,5,4,-3,-32,-99,-45,-55,-77};
        int count{};
        bool in_the_vector{false};

#if 0
//Corrected ur code below,to compile it ,just switch #if 0 to #if 1 
        while (count <= int(vec.size())) {
                count ++;

                if (vec[count] == -99){
                        cout<<count<<endl;
                        in_the_vector = true;
                        break;
                }
        }
        if(!in_the_vector){
                // (vec[count] != -99);
                cout<<int (vec.size())<<endl;
                count == int(vec.size());
        }
#endif
//My implementation below             
        auto it = std::find_if (begin(vec), end(vec) , [](int i) { return i==-99 ; }); 
        //we got the iterator of the found element
        //lets see if the index is valid
        auto index = std::distance(begin(vec) , it);
        std::cout << index <<std::endl;
        return 0;
}
commented Feb 12 by Nedelea Claudiu (200 points)
Really  helpful. Thanks
0 votes
answered Feb 16 by Kaushik4852 (230 points)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

    int main(){
        
    vector<int>vec{9,6,5,4,-3,-32,-99,-45,-55,-77};
    int count{};
    bool in_the_vector{false};
        
    for(count=0;count <= int(vec.size());count++)
    {
            
    
            if (vec[count] == -99)
 
            cout<<count+1<<endl; 
          //because count starts from 0 so we have to add 1 in count to get index no.  

          in_the_vector == true;
    
    }
    
            if(!in_the_vector)
            {
         // (vec[count] != -99);
            cout<<int (vec.size())<<endl;
            count == int(vec.size());
            
            }    
    return 0;       
    }

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...