### Create a StudySoup account

#### Be part of our community, it's free to join!

Already have a StudySoup account? Login here

# Class Note for EECS 168 at KU

### View Full Document

## 19

## 0

## Popular in Course

## Popular in Department

This 18 page Class Notes was uploaded by an elite notetaker on Friday February 6, 2015. The Class Notes belongs to a course at Kansas taught by a professor in Fall. Since its upload, it has received 19 views.

## Reviews for Class Note for EECS 168 at KU

### What is Karma?

#### Karma is the currency of StudySoup.

#### You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 02/06/15

Programming Basics Problem Solving Programming Comments Variables Data Types Constants Assignment Statements Input and Output Arithmetic Expressions Operator Precedence Rules Basic Program Structure Software Engineering Tips Initial Sphere Program Final Sphere Program Initial Statistics Program Final Statistics Program Problem Solving Steps 0 Understand the problem to be solved 0 Decompose problem into pieces you know how to solve 0 Write computer instructions for each piece 0 Combine pieces into a single program 0 Compile test and debug program 0 Use program to solve initial problem Programming 0 Objective of programming is to give computer detailed instructions 0 Computers and programmers have to read these instructions 0 Need to have unambiguous way to write instructions 0 Need to learn syntax and semantics of programming language 0 Rest of this class focuses on using C for problem solving Non objectoriented programming Data Structure Algorithm Program Comments 0 Comments are used to explain program purpose and methods used 0 Comments are human readable but ignored by compiler 0 Should write comments when designing program 0 Should also add comments explaining major changes 0 Two types of comments supported Old style C comment that can span multiple lines gt New C comment that goes to the end of the line Pay attention gt style comment cannot be nested Variables We need to store and access data to perform calculations Can allocate space in computer for data by declaring a variable Syntax is datatype variablename datatypes are listed below Should choose meaningful names for variables ie quotAgequot not quotAquot Variable names must start with upper or lower case characters variable names may contain numbers and selected special characters underscore 0 C is case sensitive so quotNumquot is a different variable from quotnumquot Examples of variable names a a3 a3 ab ab bbb Data Types 0 Different types of data can be saved depending on application int stores positive or negative integers 32 bit oat stores positive or negative real numbers 32 bit char stores ascii characters 39A39 39239 etc 8 bit The names int oat char and others are reserved words Variable declaration example int age Can store age in years oat height Can store height in meters char gender Can store 39M39 or 39F39 for gender int Xyz Can declare multiple variables on a line bool single Can store Boolean value true or false Constants 0 Certain variables have values which will never change 0 Can use constants to store these values 0 De ne constants by adding reserved word const before declaration 0 Also need to provide value at declaration time 0 Can have constants of any variable data type 0 Constant declarations must end with a semicolon Constant example const int SILLY 42 const oat P1 31415926535 const char YES 39Y39 Pay attention If you have the following statements in a program const int c5 c7 7 You will have the following compiler error when you try to compile the code assignment of readonly variable c Assignment Statements 0 Can copy one variable to another using the assignment operator 0 Variable on left of is destination where data is copied to 0 Value on right of is source where data comes from 0 Data values should be of the same type 0 Conversion will occur if values are not of same type 0 Assignment statements must end with a semicolon Assignment example int value int number oat data int X y z bool ag data 2158 data variable now equals 2158 value 17 value variable now equals 17 number value number variable now equals 17 data SILLY data variable now equals 420 number P1 number variable now equals 3 X y z O multiple assignments on one line ag false ag is now set to false Pay attention You cannot give an integer variable a oat value For example int a a 35 a will have a value 3 Input and Output Need to have some way to get data in and out of program Input commands read values typed in by user into variables Output commands write data values to users display screen Spaces between values are skipped when reading input No spaces are written between values on output unless added Special value endl can be written for carriage return Can use n for return t for tab and a for bell Must use quot for double quote for slash character escape sequence IO statements must end with a semicolon Input output example cin gtgt data reads one oat cin gtgt number gtgt value reads two integers cout ltlt 3843 cout ltlt SILLY cout ltlt quotPi is quot ltlt PI cout ltlt number cout ltlt endl cout ltlt quotnquot writes quot3843quot writes quot42quot writes quotPi is 31415926535quot writes contents of number goes to next line on screen goes to next line on screen Pay attention cin will automatically ignore the space and new line when reading variables cout will not automatically add space or new line between neighboring variables 10 Arithmetic Expressions o Often need to combine variables to get new values 0 Calculations can be expressed as arithmetic expressions 0 The arithmetic operators are multiplication division modulo remainder after division addition subtraction Arithmetic expressions data P1 5 data now equals 15707295 number SILLY 5 number now equals 8 cout ltlt l23 writes quot6quot value data 5 value now equals 10 number 27 lO number now equals 7 Pay attention 1 cannot be used on oat number 2 int a b c oat d a42 b5 cab c 8 and not 84 d a b d80 dalOb d84 ll Operator Precedence Rules Values are computed using quotnaturalquot operator precedence rules Parenthesized expressions evaluated rst from inside out Multiplication division modulo have high precedence Addition subtraction have lower precedence High precedence operations computed before low precedence Values calculated left to right at same precedence level Number the order of operations by hand for debugging Complex expressions number silly value number data number value silly value P1 data PI cout ltlt 123456 number l23456 12 Basic Program Structure 0 C programs normally have the following structure 1 introductory comments explain program purpose and method 2 include statements access to existing function libraries 3 global data structures used to store information later 4 user defined functions used to decompose problem later 5 main function variables and statements for program 0 Use blank lines to make the program look nice Program structure example include ltiostreamgt using namespace std int main int money 123 cout ltlt quotHello mom please send quot ltlt money ltlt endl return 0 Software Engineering Tips 0 Try to pick variable names which are meaningful 0 Always initialize variables before you use their values 0 You should write your comments before you write your code 0 Add to your program a little at a time grow your solution 13 Initial Sphere Program Purpose Program to calculate the volume and area of a sphere of a given radius Author int main Local variable declarations Read input parameters Calculate volume Calculate surface area Print output return 0 14 Final Sphere Program Purpose Program to calculate the volume and area of a sphere of a given radius Author include ltiostreamgt using namespace std int main Local variable declarations const oat P1 31415926535 oat radius Read input parameters cout ltlt quotEnter sphere radius quot cin gtgt radius Calculate volume and surface area oat volume 40 30 PI radius radius radius oat area 40 PI radius radius Print output cout ltlt quotRadius quot ltlt radius ltlt endl cout ltlt quotVolume quot ltlt volume ltlt endl cout ltlt quotArea quot ltlt area ltlt endl return 0 Initial Statistics Program Purpose Program to calculate mean and variance of ve input values Author int main Local variable declarations Read input parameters Calculate mean Calculate variance Print statistics return 0 16 Final Statistics Program Purpose Program to calculate mean and variance of ve input values Author include ltiostreamgt using namespace std int main Local variable declarations oat numl num2 num3 num4 num5 Read input parameters cout ltlt quotEnter ve numbers quot cin gtgt numl gtgt num2 gtgt num3 gtgt num4 gtgt num5 Calculate mean oat mean numl num2 num3 num4 num5 50 Calculate variance oat difl numl mean oat dif2 num2 mean oat dif3 num3 mean oat dif4 num4 mean oat dif5 num5 mean oat var dif1dif1 dif2dif2 dif3dif3 dif4dif4 dif5dif5 50 Print statistics cout ltlt quotMean quot ltlt mean ltlt endl 17 cout ltlt quotVariance quot ltlt var ltlt endl return 0 18

### BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.

### You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

## Why people love StudySoup

#### "I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"

#### "I bought an awesome study guide, which helped me get an A in my Math 34B class this quarter!"

#### "Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

#### "Their 'Elite Notetakers' are making over $1,200/month in sales by creating high quality content that helps their classmates in a time of need."

### Refund Policy

#### STUDYSOUP CANCELLATION POLICY

All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email support@studysoup.com

#### STUDYSOUP REFUND POLICY

StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here: support@studysoup.com

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to support@studysoup.com

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.