Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)

Slides:



Advertisements
Similar presentations
Chapter 1: Computer Systems
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Constants and Data Types Constants Data Types Reading for this class: L&L,
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
The Java Programming Language
ECE122 L2: Program Development February 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 2 Program Development.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 1 “ Introduction to Java and OOP”
Outline Java program structure Basic program elements
Chapter 1 Introduction. © 2004 Pearson Addison-Wesley. All rights reserved1-2 Outline Computer Processing Hardware Components Networks The Java Programming.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Chapter 1 Introduction.
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
String Escape Sequences
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 1 Introduction.
HOW COMPUTERS MANIPULATE DATA Chapter 1 Coming up: Analog vs. Digital.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© 2006 Pearson Education Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Chapter 1.4 Programming languages Homework Due: Monday, August 11, 2014.
Java Language and SW Dev’t
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
© 2006 Pearson Education 1 Obj: cont 1.3 and 1.4, to become familiar with identifiers and to understand how programming languages work HW: p.51 #1.8 –
The Java Programming Language
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 2 Data and Expressions Part One. © 2004 Pearson Addison-Wesley. All rights reserved2-2/29 Data and Expressions Let's explore some other fundamental.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2006 Pearson Education Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Working with Java.
Instructor: Alexander Stoytchev
Lecture 2 Data Types Richard Gesick.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ICS103 Programming in C Lecture 3: Introduction to C (2)
Multiple variables can be created in one declaration
CprE 185: Intro to Problem Solving (using C)
Escape Sequences What if we wanted to print the quote character?
Chapter 1: Computer Systems
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Lecture3.
Focus of the Course Object-Oriented Software Development
Instructor: Alexander Stoytchev
Presentation transcript:

Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)

Variables and Assignment CprE 185: Intro to Problem Solving Iowa State University, Ames, IA Copyright © Alexander Stoytchev

Administrative Stuff Labs TA Office hours WebCT page is up How to submit homework 1 Hw2 is out

Labs Section E: Tuesdays, 12:10 - 2:00pm (Coover, room 2018) Section F: Wednesdays, 2:10 - 4:00pm (Coover, room 2018) Section G: Tuesdays, 2:10 - 4:00pm (Coover, room 2018) Section J: Wednesdays, 4:10 - 6:00pm (Coover, room 2018) Section L: Tuesdays, 10: :00am (Coover, room 2018)

WebCT page We’ll use it to submit homeworks The official class web page is still:  Anonymous course feedback

Go to WebCT -> Discussions It is truly anonymous It helps me improve this class. Instantaneous feedback is more helpful than feedback at the end of the semester.

How to submit HW 1 (demo) _Fall_185/HomeworkSubmission/

HW2

Quick review of last 2 lectures

General Form of a C Program Figure 2.7. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Our First Program int main(int argc, char* argv[]) {}{} #include // comments about the program printf(“Hello World! \n”); return 0;

The same program in the Java language public class MyProgram {}{} // comments about the class public static void main (String[] args) {}{} // comments about the method System.out.println(“Hello World”);

Programming Languages Each type of CPU executes only a particular machine language A program must be translated into machine language before it can be executed A compiler is a software tool which translates source code into a specific target language Often, that target language is the machine language for a particular CPU type

Syntax and Semantics The syntax rules of a language define how we can put together symbols, reserved words, and identifiers to make a valid program The semantics of a program statement define what that statement means (its purpose or role in a program) A program that is syntactically correct is not necessarily logically (semantically) correct A program will always do what we tell it to do, not what we meant to tell it to do

Program Development The mechanics of developing a program include several activities  writing the program in a specific programming language  translating the program into a form that the computer can execute  investigating and fixing various types of errors that can occur Software tools can be used to help with all parts of this process

Entering, Translating, and Running a High-Level Language Program [Figure 1.12 in the textbook]

Basic Program Development errors Edit and save program Compile program Execute program and evaluate results

Errors (a.k.a., bugs) A program can have three types of errors The compiler will find syntax errors and other basic problems (compile-time errors)  If compile-time errors exist, an executable version of the program is not created A problem can occur during program execution, such as trying to divide by zero, which causes a program to terminate abnormally (run-time errors) A program may run, but produce incorrect results, perhaps using an incorrect formula (logical errors)

The first bug! Read more at:

Chapter 2

Variables A variable is a name for a location in memory A variable must be declared by specifying the variable's name and the type of information that it will hold int total; int count, temp, result; Multiple variables can be created in one declaration data type variable name

Rules for valid variable names The name can be made up of letters, digits, the underscore character ( _ ), and the dollar sign Variable names cannot begin with a digit C is case sensitive - Total, total, and TOTAL are different identifiers By convention, programmers use different case styles for different types of names/identifiers, such as  title case for variable names - Lincoln  upper case for constants - MAXIMUM

Variable Initialization A variable can be given an initial value in the declaration When a variable is referenced in a program, its current value is used int sum = 0; int base = 32, max = 149;

Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; The value that was in total is overwritten You can only assign a value to a variable that is consistent with the variable's declared type The expression on the right is evaluated and the result is stored in the variable on the left

Assignment Through a Function y = f(x); Q = sin(30); The assignment operator is still the = sign

Assignment Through scanf() int variable; scanf(“%d”, &variable); 30 There is not assignment operator in this case

Constants A constant is an identifier that is similar to a variable except that it holds the same value during its entire existence As the name implies, it is constant, not variable The compiler will issue an error if you try to change the value of a constant In C, we use the const modifier to declare a constant const int MIN_HEIGHT = 69;

Constants Constants are useful for three important reasons First, they give meaning to otherwise unclear literal values  For example, MAX_LOAD means more than the literal 250 Second, they facilitate program maintenance  If a constant is used in multiple places, its value need only be updated in one place Third, they formally establish that a value should not change, avoiding inadvertent errors by other programmers

#define primitive Constants can also be defined using the primitives of the C preprocessor #define KMS_PER_MILE 1.609

Miles-to-Kilometers Conversion Program Figure 2.1. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Memory State: (a) Before and (b) After Execution of the Program Figure 2.2. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Effect of kms = KMS_PER_MILE * miles; Figure 2.3. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

[Figure 1.4 in the textbook]

Memory Analogy

Some Primitive Data Types int float double

float and double analogy [image fom

float and double analogy float double [image fom

Binary Numbers Once information is digitized, it is represented and stored in memory using the binary number system A single binary digit (0 or 1) is called a bit Devices that store and move information are cheaper and more reliable if they have to represent only two states A single bit can represent two possible states, like a light bulb that is either on (1) or off (0) Permutations of bits are used to store values

Bit Permutations 1 bit bits bits bits Each additional bit doubles the number of possible permutations

Bit Permutations Each permutation can represent a particular item There are 2 N permutations of N bits Therefore, N bits are needed to represent 2 N unique items 2 1 = 2 items 2 2 = 4 items 2 3 = 8 items 2 4 = 16 items 2 5 = 32 items 1 bit ? 2 bits ? 3 bits ? 4 bits ? 5 bits ? How many items can be represented by

Relationship Between a Byte and a Bit [Figure 1.5in the textbook]

What is the value of this binary number? * * * * * * * *2 0 0* *64 + 1*32 + 0*16 + 1*8 + 1*4 + 0*2 + 0* = 44 (in decimal)

What is the maximum number that can be stored in one byte (8 bits)?

* * * * * * * *2 0 1* *64 + 1*32 + 1*16 + 1*8 + 1*4 + 1*2 + 1* = 255 (in decimal) Another way is: 1*2 8 – 1 = 256 – 1 = 255

What would happen if we try to add 1 to the largest number that can be stored in one byte (8 bits)?

Analogy with car odometers

[

The story with floats is more complicated IEEE Standard [

In the example shown above, the sign is zero so s is +1, the exponent is 124 so e is −3, and the significand m is 1.01 (in binary, which is 1.25 in decimal). The represented number is therefore × 2 −3, which is [

Character Strings A string of characters can be represented as a string literal by putting double quotes around the text: Examples: "This is a string literal." "123 Main Street" "X"

Escape Sequences What if we wanted to print a the quote character? The following line would confuse the compiler because it would interpret the second quote as the end of the string printf ("I said "Hello" to you."); An escape sequence is a series of characters that represents a special character An escape sequence begins with a backslash character ( \ ) printf ("I said \"Hello\" to you.");

Escape Sequences Some C escape sequences: Escape Sequence \b \t \n \r \" \' \\ Meaning backspace tab newline carriage return double quote single quote backslash

printf() function printf(“format string”, variable1, variable2, …); printf(“For int use %d”, myInteger); printff(“For float use %f”, myFloat); printf(“For double use %lf”, myDouble); printf(“For float or double %g”, myF_or_D);

scanf() function scanf(“format string”, &variable1, &variable2, …); scanf(“%d”, &myInteger); scanf(“%f”, &myFloat); scanf(“%lf”, &myDouble); scanf(“%d%f”, &myInteger, &myFloat);

Evaluating Expressions

Effect of sum = sum + item; Figure 2.4. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Effect of scanf("%lf", &miles); Figure 2.5. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Evaluation Tree for area = PI * radius * radius; Figure 2.8. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Step-by-Step Expression Evaluation Figure 2.9. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Evaluation Tree and Evaluation for v = (p2 - p1) / (t2 - t1); Figure Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Evaluation Tree and Evaluation for z - (a + b / 2) + w * -y Figure Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Questions?

THE END

TA Office hours The office hours are posted on the class web page. The TAs get paid to help you with this class. Make them earn their salaries. You can see any TA you like (not only the ones that lead your lab section). Different TAs have different strengths.