vendredi 29 mai 2020

How to jump back to the top of the code after if statement C language

I'm trying to make my program to start again from the beginning after user has selected an option from a menu. When the user selects 1 and then enters the amount of the donation I want the program to start again and show the menu What would you like to do? and not to just restart just the if statement.

Should I add a for loop inside of the if statement to accomplish this? thanks for the help.

 printf("What would you like to do?\n");
    printf("      1 - Donate\n");
    printf("      2 - Invest\n");
    printf("      3 - Print balance\n");
    printf("      4 - Exit\n");
    printf("\n");

    //scans menu choice
    scanf("%d", &menu_option);

    if(menu_option==1)
    {
    printf("How much do you want to donate?\n");
    scanf("%lf", &donation);
    donations_made++;
    current_Balance = initial_Balance + donation;
    }

jeudi 28 mai 2020

Is it better to loop through a 2D array of n pairings of values or have n if statements? - Java

I need to assess objects adjacent to my current object. There may be one above, below, left and right of my current position. If my current position is (x, y), then my adjacent objects are at positions: (x-1, y), (x+1, y), (x, y-1), (x, y+1).

Is it better to calculate these 4 positions and then store them in a 2D array, traversing them with a for... in loop:

int[][] adjacentObjects = {
          {x-1, y},
          {x+1, y},
          {x, y-1},
          {x, y+1} };

for (int[] adjacent : adjacentObjects) { ...
  if (adjacent[0] == ...) // x coordinate
  if (adjacent[1] == ...) // y coordinate

Or am I better off just having 4 near-identical if statements? Each would be 4 lines, where the x and y coordinates are the only difference.

My current mindset is that the for... in loop is cleaner, though not as readable, but I don't really know which is 'better.'

I've also thought about using another method where I'd pass the values of x and y but I also need access to another two 2D arrays in the current method, and it seems wasteful to send those as parameters to the new method as well.

I'm using Java, but I don't feel this is specific to any one language. Any help is greatly appreciated!

Python searching lists with lists, all and any

I am having trouble for some reason with a certain if statement. Let's say I have three lists:

list1 = [1,2,3,4,5]
list2 = [1,2,4,5]
list3 = [4,5,6,7,8]

I evaluate each. I want to flag it if the list has both 4 AND 5, but does not have either 2 OR 3. I thought this would work:

if all(q in [4,5] for q in the_list_name) and not any(q in [2,3] for q in the_list_name)

I would expect this to flag only list3. However it returns false on all of the lists. I wonder what I am doing wrong?

Trouble with nested ifelse statement in R

I'm trying to run a nested ifelse statement in R. Here's a look at the structure of my data using the glimpse() function from the tidyverse:

Rows: 22,104
Columns: 9
$ `Formation/Locality`    <chr> "Montmartre", "Montmartre", "Montmartre", "Fur", "Me...
$ Location                <chr> "Ile-de-France Region, France", "Ile-de-France Regio...
$ Environment             <chr> "terrestrial", "terrestrial", "terrestrial", "offsho...
$ `Palaeolongitude(N/-S)` <dbl> 47.4, 47.4, 47.4, 52.3, 46.9, 42.9, 47.5, 46.9, 46.2...
$ `Palaeolatitude(E/-W)`  <dbl> 1.6, 1.6, 1.6, 5.4, 4.8, 1.9, -5.2, 4.8, -93.6, -111...
$ TaxonomicLevel          <chr> "Order", "Order", "Order", "Order", "Order", "Order"...
$ TaxonomicName           <chr> "Upupiformes", "Upupiformes", "Upupiformes", "Trogon...
$ MinMax                  <chr> "MaxMa", "MaxMa", "MaxMa", "MaxMa", "MaxMa", "MaxMa"...
$ Age                     <dbl> 37.2, 37.2, 37.2, 55.8, 48.6, 37.2, 48.6, 48.6, 55.8...

I'm trying to get R to look at the Age column and if the value is within a certain range, for it to put the geological age name into a new column called AgeName. If the value is not within the range, I want it to move on to the next age range and so on and so forth. Here's my code so far:

pbdb_tidyish$AgeName <- ifelse(56>=pbdb_tidyish$Age&&47.8<pbdb_tidyish$Age,
                               "Ypresian",
                               ifelse(47.8>=pbdb_tidyish$Age&&41.2<pbdb_tidyish$Age,
                                      "Lutetian",
                                      ifelse(41.2>=pbdb_tidyish$Age&&37.8<pbdb_tidyish$Age,
                                             "Bartonian",
                                             ifelse(37.8>=pbdb_tidyish$Age&&33.9<=pbdb_tidyish$Age,
                                                    "Priabonian",NA))))

When I run this code, it creates the new column but fills the whole column with "Priabonian" so the dataset now looks like this:

Rows: 22,104
Columns: 10
$ `Formation/Locality`    <chr> "Montmartre", "Montmartre", "Montmartre", "Fur", "Me...
$ Location                <chr> "Ile-de-France Region, France", "Ile-de-France Regio...
$ Environment             <chr> "terrestrial", "terrestrial", "terrestrial", "offsho...
$ `Palaeolongitude(N/-S)` <dbl> 47.4, 47.4, 47.4, 52.3, 46.9, 42.9, 47.5, 46.9, 46.2...
$ `Palaeolatitude(E/-W)`  <dbl> 1.6, 1.6, 1.6, 5.4, 4.8, 1.9, -5.2, 4.8, -93.6, -111...
$ TaxonomicLevel          <chr> "Order", "Order", "Order", "Order", "Order", "Order"...
$ TaxonomicName           <chr> "Upupiformes", "Upupiformes", "Upupiformes", "Trogon...
$ MinMax                  <chr> "MaxMa", "MaxMa", "MaxMa", "MaxMa", "MaxMa", "MaxMa"...
$ Age                     <dbl> 37.2, 37.2, 37.2, 55.8, 48.6, 37.2, 48.6, 48.6, 55.8...
$ AgeName                 <chr> "Priabonian", "Priabonian", "Priabonian", "Priabonia...

Does anyone have any idea where I'm going wrong? I think it's just looking at the first Age value, running the ifelse statement then filling the whole column with the result of that as opposed to moving on to the next row.

Thanks,

Carolina

Problem with if statement and match with specific categories [duplicate]

I have a problem with an if statement

I want to display a different button for elements that are assigned to a specific category (7) than for elements that do not belong to that category:

<?php
            $result=get_media($ref);
            if(empty($result))

                include ('button1.php');

            if (count($result)>0)
            {

                for ($n = 0; $n < count($result); $n++)
                {

                    if($result[$n]["ref"] == '7')
                    {
                        include ('button2.php');
                    }
                    else 
                        include ('button1.php');
                }
            }
            ?>

The if statement works if the element is assigned to category "7" only. But if it is assigned to other categories as well, the "else" command is not working and both buttons are shown.

I could of course add more category IDs to the if statement but this would be a lot of them.

Also an unequal command in the else statement does not work.

Nested Run Keywords Statement Robot Framework

I have to execute a nested run keyword Statement and I am following below code in a function:

${value}=  Run keyword And Return Status   Dictionary Should Contain Key  ${details}  edit
    Run Keyword If   '${value}'=='True' 
    ...  Run Keywords
        ...    Run Keyword If  '&{details}[edit]'=='Block1'  Log  Block1 Call
        ...    AND  Run Keyword If  '&{details}[edit]'=='Block2'  Log  Block2 Call
        ...    AND  Run Keyword If  '&{details}[edit]'=='Block3'  Log  Block3 Call
        ...    AND  Run Keyword If  '&{details}[edit]'=='Block4'
                    ...  Run Keywords
                            ...  Log  Block4 1 Call
                                ...    AND  Log  Block4 2 Call
                                ...    AND  Log  Block4 3 Call

When I am passing the value as Block2, It returns:

Block2 Call
Block4 2 Call
Block4 3 Call 

But I want my result to only return "Block2 Call". Does Run Keyword does not support the Nested Statements?

If is not executed unless System.out.println is used or while debugging [duplicate]

We are developing a Simulator with Students with Java and Swing. The Problem we have is that after a Button Click in the Simulator.java the variable for private static boolean startButton change too true. After this the "if" should start doing their job, but that is not the case.

I have this Code Block in my Brain.java:


public class Brain
{
    // ==========================================================

    /*
     * This is the Part of Port A
     */
    // Tris for Port A | ARRAY VALUE 0
    private static boolean[] port_A_Tris_Checkboxes = new boolean[8];

    // PINs | ARRAY VALUE 1
    private static boolean[] port_A_Pins_Checkboxes = new boolean[5];

    /*
     * This is the Part of Port B
     */
    // Tris for Port B | ARRAY VALUE 2
    private static boolean[] port_B_Tris_Checkboxes = new boolean[8];

    // PINs | ARRAY VALUE 3
    private static boolean[] port_B_Pins_Checkboxes = new boolean[8];

    // ==========================================================

    /*
     * This is the Part of the Watchdog
     */
    private static boolean watchdogCheckbox;

    // ==========================================================

    /*
     * This is the Part of the Reset Button
     */
    private static boolean resetButton;

    // ==========================================================

    /*
     * This is the Part of the Einzelschritt Button
     */
    private static boolean einzelschrittButton;

    // ==========================================================

    /*
     * This is the Part of the Start Button
     */
    private static boolean startButton = false;

    // ==========================================================

    /*
     * This is the Part of the Stop Button
     */
    private static boolean stopButton = false;

    // ==========================================================

    public static void main(String[] args)
    {
        // startet das GUI
        Simulator.main(args);
        while (true) {

            if ((startButton == true) && (stopButton == false)) {

                // Do one step of the program
                Steuerwerk.runCommand(Decoder.decode(Simulator.getProgramMemory()
                        .getProgramMemory()[ProgramCounter.getProgramCounter()]),
                        Simulator.getProgramMemory().getProgramMemory()[ProgramCounter
                                .getProgramCounter()]);
                System.out.println(W_Register.getArgument());

            } else if (getStart() && getStop()) {

                /*
                 * If program has already started and the stop button was pressed,
                 * stop the program
                 */
                setStart(false);
                setStop(false);

            } else if (getStop()) {

                // No reason to save stopping of program if it didn't even start yet
                setStop(false);

            } else if (getEinzelschritt()) {

                // Do one step of the program
                Steuerwerk.runCommand(Decoder.decode(Simulator.getProgramMemory()
                        .getProgramMemory()[ProgramCounter.getProgramCounter()]),
                        Simulator.getProgramMemory().getProgramMemory()[ProgramCounter
                                .getProgramCounter()]);
                System.out.println(W_Register.getArgument());
                setEinzelschritt(false);
            }
        }

    }

    // ==========================================================

    /**
     * 
     * @param index : Checkbox value, true if checkbox has been marked
     * @param whichArray : Value that say which ArrayIndex is checked in which
     *        Array
     * 
     *        Diese Methode wird die in der GUI aktivierten Checkboxen hier in
     *        der Programm Logik vermerken sodass diese dann für die weitere
     *        Abarbeitung nach User wunsch geschehen und mit berüchsichtigung der
     *        aktivierten Checkboxen des Users
     */
    public static void setChechboxesForTrisOrPinBoolean(int index,
            int whichArray)
    {
        switch (whichArray) {
        case 0: {
            port_A_Tris_Checkboxes[index] = !port_A_Tris_Checkboxes[index];
            break;
        }
        case 1: {
            port_A_Pins_Checkboxes[index] = !port_A_Pins_Checkboxes[index];
            break;
        }
        case 2: {
            port_B_Tris_Checkboxes[index] = !port_B_Tris_Checkboxes[index];
            break;
        }
        case 3: {
            port_B_Pins_Checkboxes[index] = !port_B_Pins_Checkboxes[index];
            break;
        }
        default:
            throw new IllegalArgumentException("Unexpected value: " + whichArray);
        }
    }

    // ==========================================================

    /**
     * Diese methode wird die Aktivierung der Watchog Checkbox berücksichtigen
     * und eben dann auch vermerken
     */
    public static void setCheckboxForWatchdog()
    {
        watchdogCheckbox = !watchdogCheckbox;
    }

    // ==========================================================

    /*
     * The Set Method for the Reset Button
     */
    public static void setReset(boolean value)
    {
        if (value == true) {
            resetButton = true;
        } else {
            resetButton = false;
        }
    }

    /*
     * Get Method for the Reset Button Value
     */
    public static boolean getReset()
    {
        return resetButton;
    }

    // ==========================================================

    /*
     * The Set Method for the Einzelschritt Button
     */
    public static void setEinzelschritt(boolean value)
    {
        if (value == true) {
            einzelschrittButton = true;
        } else {
            einzelschrittButton = false;
        }
    }

    /*
     * Get Method for the Einzelschritt Button Value
     */
    public static boolean getEinzelschritt()
    {
        return einzelschrittButton;
    }

    // ==========================================================

    /*
     * The Set Method for the Start Button
     */
    public static void setStart(boolean value)
    {
        if (value == true) {
            startButton = true;
        } else {
            startButton = false;
        }
    }

    /*
     * Get Method for the Start Button Value
     */
    public static boolean getStart()
    {
        return startButton;
    }

    // ==========================================================

    /*
     * The Set Method for the Stop Button
     */
    public static void setStop(boolean value)
    {
        if (value == true) {
            stopButton = true;
        } else {
            stopButton = false;
        }
    }

    /*
     * Get Method for the Stop Button Value
     */
    public static boolean getStop()
    {
        return stopButton;
    }

    // ==========================================================
}

the other Part is my Simulator:

JButton btnStart = new JButton("START");
        btnStart.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                // TODO Auto-generated method stub
                System.out.println("Pressed");
                Brain.setStart(true);
            }
        });

The funny thing is that only if we add a System.out.println() or we Debug... it starts the if.