vendredi 1 février 2019

How do I fix this code so that it doesnt flag up a 'System.OutOfRangeException' error? [duplicate]

I'm currently creating a binary calculator using simple for loops. I'm not sure how to set the size of my array so that it doesnt show up with this error.

This is for a school project and I have tried changing the order that the arrays are set.

int arrayLengths = int.Parse(Console.ReadLine()); // user input
carry = new int[arrayLengths + 1]; 
result = new int[arrayLengths + 1];


for (int i = firstBinaryNumber.Length - 1; i >= 0; i--)
            {

if (firstBinaryNumber[i] == 0 && secondBinaryNumber[i] == 0 && carry[i] == 0)
                    {
                        carry[i - 1] = 0;
                        result[i] = 0;
                    }

if (firstBinaryNumber[i] == 0 && secondBinaryNumber[i] == 0 && carry[i] == 1)
                    {
                        carry[i - 1] = 0;
                        result[i] = 1;
                    }


if (firstBinaryNumber[i] == 0 && secondBinaryNumber[i] == 1 && carry[i] == 0)
                    {
                        carry[i - 1] = 0;
                        result[i] = 1;
                    }


if (firstBinaryNumber[i] == 0 && secondBinaryNumber[i] == 1 && carry[i] == 1)
                    {
                        carry[i - 1] = 1;
                        result[i] = 0;
                    }


if (firstBinaryNumber[i] == 1 && secondBinaryNumber[i] == 0 && carry[i] == 0)
                    {
                        carry[i - 1] = 0;
                        result[i] = 1;
                    }


if (firstBinaryNumber[i] == 1 && secondBinaryNumber[i] == 0 && carry[i] == 1)
                    {
                        carry[i - 1] = 1;
                        result[i] = 0;
                    }


if (firstBinaryNumber[i] == 0 && secondBinaryNumber[i] == 1 && carry[i] == 0)
                    {
                        carry[i - 1] = 0;
                        result[i] = 1;
                    }


if (firstBinaryNumber[i] == 1 && secondBinaryNumber[i] == 1 && carry[i] == 0)
                    {
                        carry[i - 1] = 1;
                        result[i] = 0;
                    }


if (firstBinaryNumber[i] == 1 && secondBinaryNumber[i] == 1 && carry[i] == 1)
                    {
                        carry[i - 1] = 1;
                        result[i] = 1;
                    }
                } 

What I need is for this error to stop flagging up and for the carry and result arrays to show the correct numbers.

Finding a Pattern in R

I am trying to clean some data. Below is an example of my data.

   test1          test2         test3    
 jsb cjn       kd N069W j        N9DSW 

I want to indicate what column has the pattern N0{num}{num}W in it. The {num} part can be any number between 0-9. This pattern can also appear anywhere in the string. Hence in this case my results would be as follows.

   test1          test2         test3     col
 jsb cjn       kd N069W j        N9DSW      2

Thanks in advance for any help.

PHP If else and some logics

I just having some problem of if/else or somewhat logical things in here, I have a fullcalendar that shows all the date that being reserve, I limit the reserve by 5 per date, but when I having a 2 or more reserved on the date and having reserve by other date, It gives me the same result as 4.

        $res = mysqli_query($db,$query);
        $count = mysqli_num_rows($res);
        $count = 5 - $count;
        $date_changed = "";
        $reserve_id = 0;

        foreach ($res as $row) 
        {
            date_default_timezone_set('Asia/Manila');

            $current_timestamp = strtotime($row["dateend"] . '+1 day');

            $time = date("Y/m/d",$current_timestamp);


            if($row["datestart"] == $date_changed)
            {

            }
            else
            {
                if(empty($count))
                {
                    $count = '0';
                }
                else
                {
                    if($reserve_id == $row['reserve_id'])
                    {
                        $count;
                    }
                    else
                    {
                        $count = 4;
                    }
                }

                $data[] = array(
                    'id'    => $row["reserve_id"],
                    'title' => $count,
                    'start' => $row["datestart"],
                    'end' => $time,
                    'color' =>getColor($row["status"])
                );
                $date_changed = $row["datestart"];
                $reserve_id = $row["reserve_id"];
            }

        }

        echo json_encode($data);

This is image of the error with captions

All i had tried is to get the right available on the 2nd of febuary 2018 and not the others.

(C++) Program runs over an equality check between values of two strings

I'm making a program that has as input a string of lower case characters and spaces, and takes each character and shows it on screen in the order it appears, separated by a space. Example: If I input "ana has apples" I get an output "a n h s p l". The output I get instead is the input string, just with more spaces, so something like "a n a h a s a p p l e s".

My code is this:

#include <iostream>
#include <string>

using namespace std;

int main()
{
string s, s2;
int i, j;
bool OK;

cout<<"Enter the string: "; getline(cin, s);

for(i=0; i<s.length(); i++)
{
    OK=false;
    for(j=0; j<s2.length(); j++)
        if(s.at(i)==s2.at(j))
            OK==true;
    if(OK==false)
        s2.push_back(s.at(i));
}

for(i=0; i<s2.length(); i++)
    cout<<s2.at(i)<<" ";


return 0;
}

Doing some debugging I found that the problem is at the line if(s.at(i)==s2.at(j)). Even if they are actually equal, the program won't accept the condition as true and turn OK into true, thus pushing back all of the characters from the first string into the second string, having s and s2 be the same. How do I fix this, am I supposed to use a different operator than the classic ==, if so which one? Why is this not working?

Compare two columns using pandas 2

I'm comparing two columns in a dataframe (A & B). I have a method that works (C5). It came from this question: Compare two columns using pandas

I wondered why I couldn't get the other methods (C1 - C4) to give the correct answer:

df = pd.DataFrame({'A': [1,1,1,1,1,2,2,2,2,2],
                   'B': [1,1,1,1,1,1,0,0,0,0]})

#df['C1'] = 1 [df['A'] == df['B']]

df['C2'] = df['A'].equals(df['B'])

df['C3'] = np.where((df['A'] == df['B']),0,1)

def fun(row):
    if ['A'] == ['B']:
        return 1
    else:
        return 0
df['C4'] = df.apply(fun, axis=1)

df['C5'] = df.apply(lambda x : 1 if x['A'] == x['B'] else 0, axis=1)

enter image description here

Possible dynamic If statement in VBA?

I want to use if statement to evaluate 2 parameters among a plethora of conditions. I know I can use if statement with AND/OR but I think it will be visually cumbersome to do it in excel.

Is it possible to implement the if statement in VBA dynamically? Meaning I want to check the result of the If statement promptly while changing its conditions -say from a drop down list- in the excel sheet (Without having to open and run the VBA window).

if statement with a search text condition in a tuple

I need to write in erlang an if statement with the condition to search this test "tele/folin-house-s1/SENSOR" in the tuples that I recevie. Below I insert the tuples I'm receiving:

{message,<<0,5,128,208,182,250,162,9,244,64,0,0,105,198,0,0>>,
     1,<<"DVES_0DBFDB">>,
     #{dup => false,retain => true},
     #{username => undefined},
     <<"tele/folin-house-s1/LWT">>,<<"Offline">>,
     {1549,8794,985040}}.
{message,<<0,5,128,208,182,250,162,9,244,64,0,0,105,198,0,0>>,
     1,<<"DVES_0DBFDB">>,
     #{dup => false,retain => true},
     #{username => undefined},
     <<"tele/folin-house-s1/LWT">>,<<"Offline">>,
     {1549,8794,985040}}.
{message,undefined,0,<<"733f0bfd-0741-4e55-bd14-ba10a156165f1549007325254">>,
     #{dup => false,retain => false},
     #{username => undefined},
     <<"tele/folin-house-s1/SENSOR">>,
     <<"{\"Time\":\"2018-12-18T22:37:00\",\"BME680\":{\"Temperature\":21.3,\"Humidity\":38.1,\"Pressure\":1021.4,\"Gas\":308.19},\"TempUnit\":\"C\"}">>,
     {1549,8796,895335}}.
{message,undefined,0,<<"733f0bfd-0741-4e55-bd14-ba10a156165f1549007325254">>,
     #{dup => false,retain => false},
     #{username => undefined},
     <<"tele/folin-house-s1/SENSOR">>,
     <<"{\"Time\":\"2018-12-18T22:37:00\",\"BME680\":{\"Temperature\":21.3,\"Humidity\":38.1,\"Pressure\":1021.4,\"Gas\":308.19},\"TempUnit\":\"C\"}">>,
     {1549,8803,302986}}.

I would to filter with an if statement only the tuple like this one:

{message,undefined,0,<<"733f0bfd-0741-4e55-bd14-ba10a156165f1549007325254">>,
     #{dup => false,retain => false},
     #{username => undefined},
     <<"tele/folin-house-s1/SENSOR">>,
     <<"{\"Time\":\"2018-12-18T22:37:00\",\"BME680\":{\"Temperature\":21.3,\"Humidity\":38.1,\"Pressure\":1021.4,\"Gas\":308.19},\"TempUnit\":\"C\"}">>,
     {1549,8803,302986}}. 

that contains "tele/folin-house-s1/SENSOR". I don't know erlang coding.