Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this problem you will be asked to modify the function below as follows: Add

ID: 3856318 • Letter: F

Question

For this problem you will be asked to modify the function below as follows: Add an additional input argument (call it user_sel_in ) that will allow a user to enter an operation type (addition, subtraction for example). This will always be a numeric argument. If the user enters a 1, they mean to add, if the user enters a 2, they mean to subtract. Add an additional output argument (call it user_sel_out) that will allow you to return a string to the user, representing what they chose to do. For example, if the user enters a 1, you should return the string 'addition'. If the user enters a 2, you should always return the string 'subtraction'. In the function body, you should do the following: Based on what the user entered for the parameter that you created, you will either add up the first two parameters, or you will subtract the second parameter from the first. Example: first_arg - second_arg. You should return the answer in the output argument result, and you should indicate what the user chose in the output argument that you created, called user_sel_out.

Solution MATLAB Documentation 1%You need to modify this line by adding additional input and output arguments as described 2 function [result] function-example(array1, array2) 4 %Follow the instructions above to complete the body of this code 5 end

Explanation / Answer

It’s relatively easy to write out solution steps to a problem in our language (English, in these notes). The difficult part is you have to talk to the computer in its own language. It would be nice if we could just write “Hey computer, here’s two numbers – add them together and tell me the sum.” A human might understand these instructions, but a computer won’t. Why? First, the computer needs to be told how to do tasks in very specific, logical steps. For this little addition example, the steps would be:

Next, we need to talk to the computer in its own language. We translate each solution step into a statement (or statements) in the computer’s language. And, in this course, the computer’s language is Small Basic. To be able to tell the computer how to do any task, you need to have a thorough understanding of the Small Basic language. Your understanding of Small Basic will allow you to translate your programming steps into a language the computer can understand.

Another thing to remember as you write Small Basic programs is that you need to be logical and exact. A computer will follow your instructions – even if they’re wrong! So, as you learn Small Basic, we will emphasize the need to be exact. Once you write exact and logical Small Basic code, the computer is very good and fast at doing its job. And, it can do some pretty amazing things. Let’s look at a couple of other examples of writing out a programming task as a series of steps to illustrate some things a computer can do.

What if the local school principal asks you to average the test scores of the 352 students in the school? Those steps are:

Not too hard, huh? Notice here that the second step can be further broken down into smaller steps. To add up 352 scores, you would:

In these steps, the computer would do the same task (adding a number) 352 times. Computers are very good at repeating tasks – we will see that this process of repetition is called looping. You will build code for this example in Class 7.

Computers are also very good at playing games with you (that’s why video games are so popular). Have you ever played the card game “War?” You and another player take a card from a standard playing deck. Whoever has the ‘highest’ card wins the other player’s card. You then each get another card and continue the comparison process until you run out of cards. Whoever has the most cards once the game stops is declared the winner. Playing this game would require steps similar to these:

Things are a bit more complicated here, but the computer is up to the task. The first step requires the computer to shuffle a deck of cards. How do you tell a computer how to do this? Well, before this course is over, you will know how. For now, just know that it’s a series of several programming steps. We will put the Small Basic program for such a specific task in its own area called a subroutine. This makes the program a little easier to follow and also allows use this code in other programs. Notice Step 4 requires the computer to make a decision – determining which card is higher. Computers are very good at making decisions. Finally, Step 5 asks us to repeat the handing out of cards – another example of looping. You will also build this program in Class 7.

If all of these concepts are not clear at the moment, that’s okay. They will become clearer as you progress through this course. I just wanted you to have some idea of what you can do with Small Basic programs. Just remember, for every Small Basic program you create, it is best to first write down a series of logical steps you want the computer to follow in performing the tasks needed by your program. Then, converting those steps into the Small Basic language will give you your Small Basic program – it’s really that simple. This class begins instruction in the elements of Small Basic. And, in subsequent classes, you learn more and more Small Basic, adding to your Small Basic vocabulary. We’ll start slow. By the end of this course, you should be pretty good at “talking Small Basic.”

Small Basic - The First Lesson

At long last, we are ready to get into the heart of a Small Basic program - the Small Basic language. In this class, we will discuss variables (name and type), assignments, arithmetic operations, and techniques for working with a particular type of variable called strings. In each subsequent class in this course, you will learn something new about the Small Basic language.

Variables

All computer programs work with information of one kind or another. Numbers, text, dates and pictures are typical types of information they work with. Computer programs need places to store this information while working with it. What if we need to know how much ten bananas cost if they are 25 cents each? We would need a place to store the number of bananas, the cost of each banana, and the result of multiplying these two numbers together. To store such information, we use something called variables. They are called variables because the information stored there can change, or vary, during program execution. Variables are the primary method for moving information around in a Small Basic program. And, certain rules must be followed in the use of variables.

Variable Names

You must name every variable you use in your program. Rules for naming variables are:

If a variable name consists of more than one word, the words are joined together, and each word after the first begins with an uppercase letter

The most important rule is to use variable names that are meaningful. You should be able to identify the information stored in a variable by looking at its name. As an example, in our banana buying example, good names would be:

As mentioned in an earlier class, the Small Basic language is not case sensitive. This means the names BananaCost and bananacost refer to the same variable. Try to be consistent in how you write variable names. And make sure you assign unique names to each variable. A nice feature of the Small Basic intellisense feature is that as you add variables to your program, the variable names are added to the list of choices available in the intellisense drop-down menu.

Variable Types

Quantity Variable Name Cost of each banana BananaCost Number of bananas purchased NumberBananas Cost of all bananas TotalBananaCost
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote