0

I was doing a problem on LeetCode where I had to sort an input array and return output array such that sorted even numbers precede the sorted odd numbers. I wrote the following code:-

#include <iostream>
#include <vector>
#include <string.h>

#include "easy.h"

using namespace std;

int main()
{
    vector<int> evenArray;
    vector<int> oddArray;
    string tempStr;
    getline(cin, tempStr);
    for (int i = 0; i < tempStr.length(); ++i){

    //  cout << tempStr[i] << endl;

        if (isdigit(tempStr[i]) && (tempStr[i])%2==0){
            //cout << "is even : " << tempStr[i] << endl;
            evenArray.push_back((int)(tempStr[i]));
            //printVector(evenArray);
            //cout << "\n";
        } else if (isdigit(tempStr[i]) && (tempStr[i])%2!=0){
            //cout << "is odd : " << tempStr[i] << endl;
            oddArray.push_back((int)(tempStr[i]));
            //printVector(oddArray);
            //cout << "\n";

        }
    }

    for (int k = 0; k < oddArray.size(); ++k){
        evenArray.push_back(oddArray[k]);
    }
    printVector(evenArray);
    return 0;
}

I inputted the following 2 4 5 3 4, expected {2 ,4, 4, 3, 5}, instead got {50, 52, 52, 53, 51}. Please note that the easy.h header file does no harm and I am only calling one function printVector() from the header file. If in any case, you want the code for printVector():-

template <typename t> void printVector(vector<t> vec){
    cout << "{";
    for (int l = 0; l < vec.size(); ++l){
        cout << vec[l] << ", ";
    }
    cout << "\b\b}";
}

Also, the comments made were used by me by the time of debugging the program. Any help or reference will be useful.

  • 5
    You're pushing `tempStr[i]` onto the array ... but `tempStr[i]` is a character... – ChrisMM May 05 '22 at 12:48
  • 2
    `'1' != 1`. `char`s are integers but the character `'1'` is not encoded by the value `1` – 463035818_is_not_an_ai May 05 '22 at 12:49
  • *Any help or reference will be useful.* -- A single call to [std::sort](https://en.cppreference.com/w/cpp/algorithm/sort) and a single call to [std::stable_partition](https://en.cppreference.com/w/cpp/algorithm/stable_partition) is all you need to solve this problem. Literally 2 or 3 lines of code. – PaulMcKenzie May 05 '22 at 12:53
  • @ChrisMM but I am checking if the character is an integer in the isdigit() function. What do I need to modify in order to fix the program? Please elaborate as I am a complete newbie in C++ – ojasmaheshwari May 05 '22 at 12:53
  • 3
    @ojasmaheshwari If you're new to C++, don't use sites like LeetCode to learn C++. That site and similar other "competition coding" sites are not designed to teach you C++. The questions asked at those sites assume you know the computer language you will be using to solve their random puzzles, and know the language well-enough to never make simple mistakes as you're making. Instead, invest in good, peer-reviewed C++ books to actually learn the language. – PaulMcKenzie May 05 '22 at 12:57
  • `isdigit` just says it's the characters between 0 and 9, but they are still characters, not numbers. You could subtract `'0'` from the character to get the number, though this will only work for single digit numbers. Values like `12` will be interpreted as `1` then `2` by your implementation. Since you are a beginner, then forget that LeetCode even exists. It is garbage, and not a good resource to learn from. – ChrisMM May 05 '22 at 12:57
  • @ojasmaheshwari Computers operate on numbers only. Letters also have to be represented as numbers, so we map those letters (or "characters") to numbers. Most popular mapping is called ASCII, in there character `'A'` is `65`, character `'a'` is `97`, and character `'0'` has value `48`. Doing `(int)'0'` gives you the numeric value of that character - `48`. To convert character `'0'` to integer value `0` do `tempStr[i] - '0'`. – Yksisarvinen May 05 '22 at 12:59
  • I think you should take some time to think about the difference between digits (i.e. symbols) and integers (i.e. numbers). – molbdnilo May 05 '22 at 13:06
  • @ojasmaheshwari [Here is an example of using what I mentioned earlier](https://godbolt.org/z/rx1eMsGvc). You will *not* learn how to utilize the library functions such as shown in the link by going to LeetCode, Geeks, HackerRank, or any of those sites. – PaulMcKenzie May 05 '22 at 13:06

0 Answers0