Part 1 Simple Arithmetic

Similar documents
Part 1 - Your First algorithm

Part 1 - Your First algorithm

Part 1 Arithmetic Operator Precedence

Part 1 Your First Function

printf( Please enter another number: ); scanf( %d, &num2);

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

MITOCW watch?v=0jljzrnhwoi

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

(Refer Slide Time: 00:23)

MITOCW watch?v=kz7jjltq9r4

Text Input and Conditionals

The Stack, Free Store, and Global Namespace

Running Wordstar 6 on Windows 7 Using vdos

Robert Ragan s TOP 3

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

MITOCW ocw f99-lec12_300k

Introduction to Programming

Introduction. Using Styles. Word 2010 Styles and Themes. To Select a Style: Page 1

Introduction to the Emacs Editor

How to Improve Your Campaign Conversion Rates

Post Experiment Interview Questions

4. Write sets of directions for how to check for direct variation. How to check for direct variation by analyzing the graph :

Yup, left blank on purpose. You can use it to draw whatever you want :-)

Linked Lists. What is a Linked List?

BEGINNER PHP Table of Contents

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Watson Conversation Cheat Sheet

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Troubleshooting Maple Worksheets: Common Problems

MITOCW watch?v=se4p7ivcune

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

5. Control Statements

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

CSI Lab 02. Tuesday, January 21st

Starting. Read: Chapter 1, Appendix B from textbook.

The following content is provided under a Creative Commons license. Your support

Exercise 6 - Addressing a Message

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

Arduino IDE Friday, 26 October 2018

In further discussion, the books make other kinds of distinction between high level languages:

Clickteam Fusion 2.5 Creating a Debug System - Guide

Using Microsoft Excel

Note: Please use the actual date you accessed this material in your citation.

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

Chapter One: Getting Started With IBM SPSS for Windows


Basic Fiction Formatting for Smashwords in OpenOffice L. Leona Davis. Copyright 2012 L. Leona Davis All Rights Reserved

3. Simple Types, Variables, and Constants

These are notes for the third lecture; if statements and loops.

What's the Slope of a Line?

Lesson 3 Transcript: Part 1 of 2 - Tools & Scripting

Slide 1 CS 170 Java Programming 1 Testing Karel

FILE ORGANIZATION. GETTING STARTED PAGE 02 Prerequisites What You Will Learn

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

Wednesday. Wednesday, September 17, CS 1251 Page 1

Max and Programming Is Max a Programming Language?

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

Python Activity 2: Input and Variables in Python

1007 Imperative Programming Part II

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Introduction to Unix

Direct Variations DIRECT AND INVERSE VARIATIONS 19. Name

AHHHHHHH!!!! NOT TESTING! Anything but testing! Beat me, whip me, send me to Detroit, but don t make me write tests!

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5

How to import text files to Microsoft Excel 2016:

Introduction to Unix

CS3 Midterm 1 Fall 2006

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

Binary, Hexadecimal and Octal number system

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Tutorial: Bryce Instancing Without the Instancing Lab

KMyMoney Transaction Matcher

MITOCW watch?v=rvrkt-jxvko

Staff Intranet Survey Results

Neon Carrot Prototype I Evaluation. Dan Cody, Logan Dethrow, Ben Fisher, Jeff Stanton April 6, Preamble

From time to time Google changes the way it does things, and old tutorials may not apply to some new procedures.

MITOCW watch?v=sdw8_0rdzuw

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

Note: Photoshop tutorial is spread over two pages. Click on 2 (top or bottom) to go to the second page.

Karlen Communications Accessible Word Document Design: Images and Alt Text. Karen McCall, M.Ed.

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

Spectroscopic Analysis: Peak Detector

Math 25 and Maple 3 + 4;

Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:

CPSC 320 Sample Solution, Playing with Graphs!

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

PowerPoint Instructions

Program Organization and Comments

The following content is provided under a Creative Commons license. Your support

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Installing and Configuring the Voice UPB Bridge updated 1-Jan-2019

BBC Learning English Face up to Phrasals Mark's Mistake

But I know what most of you will do: use a calculator to convert; that's fine, IF you understand the theory.

Helping the Compiler Help You. Thomas Dy

Introduction to Python Code Quality

Transcription:

California State University, Sacramento College of Engineering and Computer Science Computer Science 10A: Accelerated Introduction to Programming Logic Activity B Variables, Assignments, and More Computers were originally designed for their fantastic ability to perform complex math. That hasn t changed much over the many, many decades since the first mainframes. Even today, most of our favorite applications make use of complex math (behind the scenes). For example, the video games that you play (rather than study) are heavily reliant on computations from determining the path of a projectile, the gravity under your avatar s feet, and even things like making realistic shadows, shading, and mist. Part 1 Simple Arithmetic It's time to use one of the most important features of any programming language variables. 1. Create a new Flowgorithm flowchart. 2. Insert an assignment statement into your flowchart. A rectangle shape should show up. 3. Open the assignment dialog box the same way you did with Output in the last activity. (Double-click) 4. In the Variable box, type: age 5. In the Expression box, type: 18 (or whatever your age is). You have now created the assignment statement: age = 18. 6. Execute your flowchart Whoops. That didn't work. Look at the error message. It seems that Flowgorithm requires a variable to be declared before it can be used. So far in this exercise, you've only dealt with variables that store numbers, but variables can store individual characters, strings, and even some other stuff. NOTE: Declaring Variables In most modern programming languages, it is required that you "declare" your variables before you can use them. That is, you have to tell the program what kind of data you plan to store and what you will name it. Yes, it's annoying. But, it's actually a good idea. You'll appreciate it in time. 7. Add a Declare box to the beginning of your flowchart. Place it before the assignment. We are going to be storing whole numbers, so set the type to Integer.

2 8. Trying executing it again. Ah, good. That worked. It didn't do anything you could see, but at least it didn't crash. EXPLORE: Chart Styles The flowcharts used by Flowgorithm look a tad different from the one used by your textbook. That's not a surprise. There are many different ways of drawing flowcharts. If you want Flowgorithm to look exactly like the textbook, it can! From the menu, select Edit Choose Chart Style. In the "Chart Style" dropdown, select "Gaddis". Part 2 Uninitialized Variables In some programming languages, if you don't assign a value to a variable, the language automatically initializes. Relying on that automatic initialization gets you into bad habits. You need to be in charge! If you want a value in a variable, put it there! For purposes of this course (and many future courses), any variable which is supposed to have some initial value must be explicitly assigned by you. Failure to do so will be considered an error (sloppy programming), even if the language lets you! 1. Let's so something more exciting than just assigning your age. Create declarations for two variables named graduate and years. These will both be integers. Remember to put these at the beginning of your flowchart. 2. Now, let's figure out how old you will be when you graduate from Sac State. Create another assignment statement to set graduate= age + years. 3. Don't assign anything to years yet. 4. Let's output the contents of the graduate variable. Create an Output shape and type the name graduate. You can remove the one that just outputs your age. 5. Execute. Whoops! Flowgorithm did not like that. Read the error message carefully. It appears that the years variable was uninitialized. 6. Added an assignment statement that stores 4 into years. Look very carefully where you should put this. 7. If you did everything correctly, the output should be your age plus 4. If something else happens, you ve made a mistake. Try to solve it. If you can t, call for help. Part 3 Value of a Variable Let's try an experiment. 1. Change the output statement. Rather than outputting the value of graduate, let's output the word graduate. (Put it in quotes.) 2. Execute and observe the results.

3 3. After you see the result, change the output box back to the correct approach. If you want the value of a variable, you use the variable name. If you put a name in quotes, you are no longer dealing with the value of the variable, just a bunch of letters, i.e., a string. Please, re-read the paragraph above. It is important! Don t go on until you understand the difference between outputting "graduate" and outputting graduate. Part 4 Better Output If you were actually writing a program for a human end-user, that user would not be a programmer he/she would not read the program. They would just look at the result. Therefore, you need to make the results "meaningful." 1. Open your output shape the one that displays the age you will graduate. 2. Let's output your age, but make it read "My age will be " and then the graduate variable. Remember the ampersand variable? "My age will be " & graduate 3. Execute and observe the results. This is what is meant by an "output label". Think carefully about what you've done. You could have gotten the right original result by using an output box with "My age will be 22" in it. But if the program was changed (the value of age or years), the result would still output 22. That's why you use a combination of literal strings (the stuff inside the quotes) and variables (outside quotes) and the ampersand to join them (concatenate them) for output. NOTE: Echoing This output operation demonstrates a concept called "echoing" we "echo" the values used in the calculation in the actual output (in this case, the values 11 and 9). Often, it is a very useful thing to do, and we'll do it quite a bit. There are times when it is not as useful, but for now, consider it a good thing to echo the values. Part 5 Improving the Output Label 1. Change the output operation so that it results in this output: I'm 18 and in 4 years I will be 22. Be sure you are using an appropriate combination of literal strings and variables. The 18, the 4, and the 18 should all be values of variables. 2. Execute and observe the results. Make sure you have appropriate blanks in there. (Blanks inside quotes are just like any other characters.) 3. To make sure you have made correct use of variables, change the years variable to 5 (yes, it often takes five years to graduate) 4. Execute and observe the results. If the output changes to I'm 18 and in 5 years I will be 23, you're on the right track!

Part 6 String Variables 4 Not all variables store numbers. Here's an example using variables that store "strings". As you learned previously, string in computer programming means any combination of alphabetic and numeric characters, plus punctuation, spaces, etc. In most languages, literal strings are enclosed in double-quotes. 1. Create two assignment shapes (and declares). The first assigns your own name (first and last) to the variable name. The second one will assign your major. Don't forget quotes. 2. Now provide an output operation that looks like this: "My name is " & Name "." 3. Execute and observe the results. EXPLORE: Console Formatting You noticed that the Console Window looks like you chatting on your phone. Traditionally, the Console Window displays just simple, and plain, text. If you want to use the text-only format, you can click on the icon on the top of the Window. Well, that is interesting! You have a somewhat user-friendly program now. Let's use all the variables you have created (name, major, age, years and graduate). Your flowchart has two output statements. The first one outputs your graduation age and the other your name. 4. Remove both the output statements 5. Add a new output statement that will output the following. The contents of your name, age, years, graduate age and major will all be read from variables. My name is Joe Gunchy, I'm 18 and in 5 years I will have Computer Science degree when I am 23. Part 7 Input and Prompting the User Up to now, the Flowgorithm flowcharts have really been pretty useless. Every time you execute the flowchart, you get exactly the same output. That's because you've used only assignment statements to give values to variables. In order to give a variable a different value, you would have to go in and change the flowchart. The missing element is input. The Input Statement gives us a way to put values into variables while the program is executing, rather than only while we are writing the program. This way, we can execute the same program twice, but get completely different output, because each time we execute the program, we put in different values. 1. Replace each assignment statement (age, name, and major) with input statements. 2. Execute and observe the results. Wow! Much better! But that wasn't very user friendly! Sure, it worked. But, it just wanted you to type in a value without telling you what is wanted. The Console Window displays the variable name so that helps a little. But what if the variable name was x or value? Yup, not much help at all. So, let's not rely on that.

We need the program to tell us what it needs us to enter. This, in computer science, is called a "prompt". You have seen these a lot (unless you never used a computer before today). 5 3. Now add an Output Statement before each input. The output statement should tell the user what to enter. 4. Execute your program. Isn't that better? Save your program. You will turn this in.