lundi 18 octobre 2021

Why won't my if statement change the value of my flag in C++? [closed]

#include <string>
#include <vector>
#include <cstdio>
#include <stdlib.h>
#include <iostream>
#include <functional>
#include <fstream>

using namespace std;

string ltrim(const string&);
string rtrim(const string&);

vector<vector<int>> groupSort(vector<int> arr)
{
    //create return array
    vector<vector<int>> arrOut(arr.size());
    
    //initialize return array
    for (int i = 0; i < arrOut.size(); i++)
    {
        vector<int> arr1(2);
        arrOut[i] = arr1;
    }

    //count how many of each number in given array
    for (int i = 0; i < arr.size(); i++)
    {
        //check if number is alrady in array
        bool flag = true;
        for (int k = 0; k < arr.size(); k++)
        {
            if (arr[i] == arrOut[k][0])
                flag == false;
        }
        if(flag != false)
        {
            //count each number
            arrOut[i][0] = arr[i];
            for (int j = 0; j < arr.size(); j++)
            {
                if (arr[i] == arr[j])
                    arrOut[i][1]++;
            }
        }
    }
    return arrOut;
}

So for some reason the if statement at line 34 not executing the flag == false statement. It doesn't make sense why this won't work to me maybe someone can explain it. I'm just doing some practice for coding interview questions and for some reason this error keeps occurring.

Aucun commentaire:

Enregistrer un commentaire