COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
1 CSE1301 Computer Programming Lecture 6 C Primitives 3.
Structure of a C program
CS1061 C Programming Lecture 6: Operators and Expressions A. O’Riordan, 2004.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
Please pick up an attendance question and submit in 5 minutes CS 1003 Lecture #3 Sept 12, 2006 Knarig Arabshian.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
The switch Statement, DecimalFormat, and Introduction to Looping
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
1 CSE1301 Computer Programming Lecture 6: Components of a C Program (Part 2)
The preprocessor and the compilation process COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Logic & Logical Operators Logical Operators are used to connect two or more relational expressions into one OR to reverse the logic of an expression. OperatorMeaning.
C Tokens Identifiers Keywords Constants Operators Special symbols.
CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 1 The Switch Statement The switch statement provides another means to select one.
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
CSC 107 – Programming For Science. The Week’s Goal.
COP Structures Instructor: Diego Rivera-Gutierrez I’m back baby!
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
COP 3275 – Iteration and loops Instructor: Diego Rivera-Gutierrez.
Pointers *, &, array similarities, functions, sizeof.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Variables Symbol representing a place to store information
Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
COP 3275 – Character Strings Instructor: Diego Rivera-Gutierrez.
COP 3275 – Character Strings and Introduction to Pointers Instructor: Diego Rivera-Gutierrez.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Making Decisions in c. 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
1 Topics Type Variables Keywords and Identifiers Assignments Constant Variables.
USER INTERACTION AND VARIABLES Tarik Booker CS 290 California State University, Los Angeles.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
© Copyright by Deitel 1 Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple.
CSC Programming for Science Lecture 5: Actual Programming.
COP 3275 – Finishing Loops and Beginning Arrays Instructor: Diego Rivera-Gutierrez.
CprE 185: Intro to Problem Solving (using C)
User Interaction and Variables
Week 3 - Friday CS222.
פרטים נוספים בסילבוס של הקורס
C Programming Variables.
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Introduction to Programming
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
DATA TYPES There are four basic data types associated with variables:
EECE.2160 ECE Application Programming
Presentation transcript:

COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez

ADMINISTRATIVE STUFF Not so new rule: whenever you participate tell me your name! Reminder: Second quiz this Friday (29 th ) Format likely similar to 1 st quiz. If you have concerns please send them my way. Homework #2 due Monday (June 1 st ) Homework #3 likely to be assigned on Friday (29 th )

ADMINISTRATIVE STUFF Homework #2: We will go into detail of my post later today (with examples). The basics: When reading the hours, minutes, and seconds use %d not %i. To display a particular number of characters use. between % and the letter. As in %.2d.

BUNCH OF STUFF WE HAVEN’T COVERED AKA Programming in C Potpurri

SWITCH STATEMENTS switch( ) { case : break; case : break; … case : break; default: }

#include int main(void) { float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f; char operator = '\0'; printf("Input the operation you want to evaluate: "); scanf(“%f %c %f", &operand1, &operator, &operand2); //with what we know, we could do: switch(operator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case 'x': printf(“Warning the recommended operator is * not x\n“); case '*': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; default: printf(“Unknown operator %c\n“, operator); } printf("The result is: %f\n“, result); return 0; }

#include int main(void) { float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f; char operator = '\0'; printf("Input the operation you want to evaluate: "); scanf(“%f %c %f", &operand1, &operator, &operand2); //with what we know, we could do: switch(operator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case 'x': printf(“Warning the recommended operator is * not x\n“); case '*': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; default: printf(“Unknown operator %c\n“, operator); } printf("The result is: %.2f\n“, result); return 0; }

TERNARY OPERATOR - CONDITIONAL OPERATOR ? : ; This is useful to assign a variable a value for example: float result = operator == ‘+’? operand1 + operand2 : operand1 – operand2; float result = (operator == ‘+’)?(operand1 + operand2) : (operand1 – operand2) ;

REGARDING PARENTHESIS AND THE TERNARY OPERATOR I said last week that confusing parenthesis wouldn’t be part of quizzes and I stand by that. Given an exercise there will always be parenthesis around expressions to tell you what gets executed first, but you will need to be able to follow it. For example: float result = (operator == ‘+’) ? (operand1 + operand2):(operand1 – operand2) ;

float result = (op == ‘+’) ? (o1 + o2):(o1 – o2) ; Could also be: float result = (op == ‘+’) ? (o1 + o2) : ( (op == '-') ? (o1 – o2) : (o1 * o2) ) ; And I expect you to be able to follow the logic.

ASCII TABLE American Standard Code for Information Interchange (ASCII). Starndard character-encoding scheme. Not the only one! By far the most common in C

DIFFERENCE BETWEEN ++X AND X++ Last week we covered the operators ++ and --. The simple explanation was that x++ is roughly equivalent to x = x + 1; y-- is roughly equivalent to y = y – 1; Turns out the expressions ++x and --y are also valid. What’s the difference? Let’s use it in a more complicated expression: int x = 3; int n = 5 % 3 – (x++) + 2;

++X VS X++ int x = 3; int n = 5 % 3 – (x++) + 2; 5 % 3 = ? 5 % 3 = 2 x++ = ? Could be x++ = 3 or x++ = 4. We only know that after the expression x will be 4 So n could be 2 – = 2 or 2 – = 1 Let’s test it.

#include int main(void) { int x = 3; int n = 5%3 – (x++) + 2; printf("x = %d\n", x); printf("n = %d\n", n); return 0; }

#include int main(void) { int x = 3; int n = 5%3 – (x++) + 2; printf("x = %d\n", x); //this should be x = 4 printf("n = %d\n", n); //what should this be? return 0; }

#include int main(void) { int x = 3; int n = 5%3 – (++x) + 2; printf("x = %d\n", x); //this should be x = 4 printf("n = %d\n", n); //what should this be? return 0; }

TYPE SPECIFIERS Specifier long short unsigned signed So, can I have a infinitely large number stored in a variable of type int?

SIZE IN MEMORY OF DIFFERENT TYPES There is an operator called sizeof. One parameter: a type. Returns an unsigned value that uses the %zu value to printf. sizeof For example: sizeof(int);

WHAT DO COMPUTERS DO BETTER THAN HUMANS?

LOOPING Repetitive stuff. Compute sum of many numbers Generate a list of powers of a number Work with an array of numbers Work with an array of strings Turn based games Display a video Video games Execute a program

LOOPING Three structures. Equivalent in computational power. for statement for( ; ; ) while statement while( ) Do-while statement do while ( );

BETTER LOOPING – SERIOUSLY GUYS USE PROTECTION Three structures. Equivalent in computational power. for statement for( ; ; ) { } while statement while( ) { } Do-while statement do { }while ( ); BRACKETS BETTER LOOPING – SERIOUSLY GUYS USE PROTECTION