samedi 24 février 2018

Does it make a difference where I put this if statement?

So I was working on a lab with link lists and I got to a part where I have to output an error message if an element wasn't found after traversing the list. My question is, shouldn't it make a difference whether I put this if statement after the while loop or after the else statement? If I put it after the second else, shouldn't the function end as soon as that else finishes executing? I tried running it and I still got the same expected output, but I'm trying to figure out where it makes the most sense to put it since labs are graded on efficiency and style.

void AnyList::deleteNode(int deleteData)
{
    if (count == 0)
    {
        cerr << "Cannot delete from an empty list.\n";
    }
    else
    {
        Node *current = ptrToFirst; 
        bool found = false; 

        if (current->getData() == deleteData)
        {
            ptrToFirst = current->getPtrToNext();
            delete current;
            current = NULL;
            count--; 
            found = true; 
        }
        else
        {

            Node *trailCurrent = current;
            current = current->getPtrToNext(); 

            while (current != NULL && !found)
            {
                if (current->getData() == deleteData)
                {
                    trailCurrent->setPtrToNext(current->getPtrToNext());
                    delete current;
                    current = NULL;
                    trailCurrent = NULL;
                    count--;
                    found = true;
                }
                else
                {
                    trailCurrent = current; 
                    current = current->getPtrToNext();
                }
            }
            //if (!found)             <-- Does it make more sense to put it here?
            //{
            //  cerr << "Item to be deleted is not in the list.\n";
            //}

        }
        //if (!found)                 <-- Or here?
        //{
        //  cerr << "Item to be deleted is not in the list.\n";
        //}
    }

}

While Loop/If Else (Python)

This is all in Python 3.5.2

I've been trying to get this done for my school. This is only a small part of the overall assignment.

I've been trying to google the solution to this but I can't figure it out.

If someone can help me figure this out I'd appreciate it.

I have to make a choice on whether or not I want to use a 24 or 36 month payment plan for my car note.

I had the 'if' statement on the outside but it didn't work.

print ("Month")
month = 1

while month <= 24:
    print (month)
    month = month + 1

if paymentPlan == 24:
    print ("You've chosen the 24 month plan.")

elif paymentPlan == 36:
    print (month)
    month = month + 1
    print ("You've chosen the 36 month plan.")

else:
    print ("Invalid Entry")

JavaScript - adding spaces after push in empty array & one checkbox is free

Here's my codepen: https://codepen.io/CwittleZ/pen/vdzazO?editors=1010

When you click on the meat selection, it gets pushed into an array and then displayed, but there's no space between selections. How would I go about adding that space?

function meatSelected() {
  var meat = [];
  var meatChecked = document.querySelectorAll(
    "input[name=meat_options]:checked"
  );
  for (var i = 0; i < meatChecked.length; i++) {
    meat.push(meatChecked[i].value);
  }
  console.log(meat);
  document.getElementById("meat").innerHTML = meat;
}

Also, I need one of the meats to be free, but anything over will be extra. I don't know how or where to add that 'if meat checked is > 1, all other meats will be an additional $1 each.' Is there somehow a way to access the function meatSelected for that purpose? I'm just starting out with JavaScript, so please no jQuery and simple terms if possible, even if it is a longer code. I just want to be able to understand why it works.

  if (document.getElementById("pepperoni").checked) {
    add += 1;
  }
  if (document.getElementById("sausage").checked) {
    add += 1;
  }
  if (document.getElementById("canadian_bacon").checked) {
    add += 1;
  }
  if (document.getElementById("ground_beef").checked) {
    add += 1;
  }
  if (document.getElementById("anchovy").checked) {
    add += 1;
  }
  if (document.getElementById("chicken").checked) {
    add += 1;
  }

reducing if statements in C for efficiency

I am trying to make my code more efficient by reducing the number of conditional statements as I am going to write the code in assembly in the future. Do you guys see any way to reduce the statements to be more efficient?

     if (j==18)
       {
          store=12;
       }
     else if(j==30)
       {
        store=10;
       }
     else if(j==42)
     {
        store=8;
     }
     else if(j==54)
       {
        store=6;
       }
     else if(j==66)
       {
        store=4;
       }
     else if(j==78)
       {
        store=2;
       }
       else if(j==92)
       {
        store=2;
       }
         else if(j==106)
       {
        store=4;
       }
         else if(j==120)
       {
        store=6;
       }
         else if(j==134)
       {
        store=8;
       }
        else if(j==148)
       {
        store=10;
       }
         else if(j==162)
       {
        store=12;
       }
       else store=1;    

Keep a 2-tuple of ints within a specific range

I am having trouble with logic checks. There is a coordinate system grid that is 15x15, row x column.

There is a random list of 2-tuples that contains coordinates:[(3,3),(4,5),(14,14),(13,0),(0,13)].

I would like to check if some random choice tuple is within a certain range: 2x2 and 12x12. So for example the tuple: (3,3) and (4,5) would be allowed, but (13,0) and (0, 13) would not.

How would I go about implementing checks to see if a tuple pair lies within the range?

Prompt message box through custom menu on Google Sheets/App Script

I have created a custom menu on Google Sheets with two options:

function onOpen(e) {
 var menu = SpreadsheetApp.getUi().createMenu('Trendtype')
 menu.addItem('Add new store', 'openForm')
 menu.addItem('Update Database', 'replacebackenddatabase') }

When a user selects "Update Database" I would like a message box to appear and ask for confirmation with "Do you want to proceed" and Yes/No basis. IF the user selects "Yes", I would like the function 'replacebackenddatabase' to run. If not, I would just like the message box to close and nothing to happen.

How can I do this? Thank you!

counter value is incrementing just once in if else condition inside while loop

The purpose of the code below is

  1. Scan the Location of the bot
  2. Scan the state of the location, whether it is dirty or clean
  3. Do this continuously until the bot get 3 times "no" state continuously

The issue is: counter value increment just once, counter = 1, and then never increment. That's why the loop never ends.

#include <stdio.h>
#include <stdlib.h>


int main(int argc, char *argv[]) {
    char location[1];
    char state[4];
    int count;

    //If the bot get state "no" 3 times continuosly, end the loop.
    while(count<3){

        //Scan bot location
        printf("Bot Location ?\n");                 
        scanf("%s",&location);

        if(strcmp(location, "a")==0){
            printf("Is it dirty?\n");
            //Scan state of the location, dirty or clean (yes or no)
            scanf("%s",&state);

            if(strcmp(state,"yes")==0){

                //Reset the counter value to zero if state is yes
                count=0;

                //printing the counter value
                printf("Value of Counter %d \n",count);
                printf("Clean\n");
                printf("Go left to B\n");
            }
            else if (strcmp(state,"no")==0) {
                printf("Go Left to B\n");

                //Counter increment if state is no
                count++;
                printf("Value of Counter %d \n",count);
            }
            else {
                printf("Wrong input\n");
            }
        }
        else if (strcmp(location, "b")==0){
            printf("Is B dirty?\n");
            scanf("%s",&state);

            if(strcmp(state,"yes")==0){

                //Reset the counter value to zero if state is yes, same as Location A
                count=0;
                printf("Clean\n");
                printf(" Go Right to A\n");
                printf("Value of Counter %d \n",count); 
            }
            else if (strcmp(state,"no")==0) {
                printf("Go Right to A\n");

                //Counter increment if state is no, same as Location A
                count++;
                printf("Value of Counter %d \n",count);
            }
            else {
                printf("Wrong input\n");
            }
        }
        else {
            printf("Location not found\n");
        }
    }
    return 0;
}