MPATE-GE 2618: C Programming for Music Technology. Unit 1.3

Similar documents
9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Informatica e Sistemi in Tempo Reale

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Sources: On the Web: Slides will be available on:

FEEG Applied Programming 5 - Tutorial Session

Conditionals (with solutions)

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

6. Control Structures

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

The programming language C. sws1 1

I PUC - Computer Science. Practical s Syllabus. Contents

Tutorial on C Language Programming

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Two-way selection. Branching and Looping

Introduction to Python

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Fundamentals of Programming

Chapter 8 Selection 8-1

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

While Loop. 6. Iteration

General Software Development Standards and Guidelines Version 3.5

3/13/2012. Writing Simple C Programs. ESc101: Decision making using if-else and switch statements. Writing Simple C Programs

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

The C Programming Language course syllabus associate level

Example of a Java program

JavaScript: Control Statements I

2 SYSTEM DESCRIPTION TECHNIQUES

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

B.Sc.(Computer Science) and. B.Sc.(IT) Effective From July 2011

CS 241 Data Organization Coding Standards

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

Lecture 2 Notes: Flow of Control

CP Lab 2: Writing programs for simple arithmetic problems

MATLAB Programming. Problem 1: Sequential

So far we have considered only numeric processing, i.e. processing of numeric data represented

Selection Statements

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

Object Oriented Software Design

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

The C Programming Language

Adding and Subtracting Positive and Negative Numbers

The string of digits in the binary number system represents the quantity

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Object Oriented Software Design

C Language Tutorial. Version March, 1999

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

Computer Science 217

Introduction to Python

C Coding Style Guide. Technotes, HowTo Series. 1 About the C# Coding Style Guide. 2 File Organization. Version 0.3. Contents

LC-3 Assembly Language

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

5 Arrays and Pointers

Repetition Using the End of File Condition

Chapter 5. Selection 5-1

Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example

arrays C Programming Language - Arrays

VHDL Test Bench Tutorial

Problem Solving Basics and Computer Programming

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Computer Programming Tutorial

Custom Javascript In Planning

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

Conditional Statements Summer 2010 Margaret Reid-Miller

VB.NET Programming Fundamentals

ECE 341 Coding Standard

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Objective-C Tutorial

ALGEBRA. sequence, term, nth term, consecutive, rule, relationship, generate, predict, continue increase, decrease finite, infinite

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

Computer Science 281 Binary and Hexadecimal Review

Numbering Systems. InThisAppendix...

Introduction to SQL for Data Scientists

The Answer to the 14 Most Frequently Asked Modbus Questions

To Evaluate an Algebraic Expression

C PROGRAMMING FOR MATHEMATICAL COMPUTING

Topic 11 Scanner object, conditional execution

El Dorado Union High School District Educational Services

Java Basics: Data Types, Variables, and Loops

Chapter One Introduction to Programming

ECE 0142 Computer Organization. Lecture 3 Floating Point Representations

ModuMath Basic Math Basic Math Naming Whole Numbers Basic Math The Number Line Basic Math Addition of Whole Numbers, Part I

PL / SQL Basics. Chapter 3

Moving from CS 61A Scheme to CS 61B Java

Integrated C Development System For Rabbit 4000 and 5000 Microprocessors User s Manual

Computer Science 1 CSci 1100 Lecture 3 Python Functions

Pseudo code Tutorial and Exercises Teacher s Version

Stacks. Linear data structures

Binary Representation

Ohio University Computer Services Center August, 2002 Crystal Reports Introduction Quick Reference Guide

Secrets of printf. 1 Background. 2 Simple Printing. Professor Don Colton. Brigham Young University Hawaii. 2.1 Naturally Special Characters

Transcription:

MPATE-GE 2618: C Programming for Music Technology Unit 1.3

More operators e ++ and -- are increment and decrement operators ++i or i++ is equivalent to i = i + 1; --i or i-- is equivalent to i = i - 1; i += 1 is equivalent to i = i + 1; i -= 1 is equivalent to i = i 1; i *= 5 is equivalent to i = i * 5; i /= 3 is equivalent to i = i / 3;

Syntax for if statement: Conditional statements if (condition) { "statements; } Syntax for if-else: if (condition) { "statements; } else { "statements; }"

Conditional statements continued Syntax for else-if: if (condition) { "statements; } else if (condition) { "statements; } else if (condition) { "statements; } else { "statements; } e else ifs and final else are all optional Braces can be optional if there is only one statement to execute after the condition is satisfied, but you are strongly urged to use them. If the first else-if is true, only the statements encapsulated within its braces are executed; statements in subsequent else-ifs down the chain are NOT evaluated even if the else-if would also evaluate to true.

A useful gcc flag e Wall flag allows gcc to output all warnings gcc Wall test.c o test

Positive or negative? A problem example using conditionals and operators Write a program that prompts the user for an integer and then prints a statement saying whether that integer is positive, negative, or zero. Modify the program so it executes 5 times, making sure each prompt from the program asking the user to enter a value is labeled by iteration. At the end of the program print out a sum of the 5 values the user entered. See files posneg.c and posneg2.c for solution.

Note on style Use clear variable names Lower case vs. upper case: starting variable names with a lower case letter is recommended Good variable name: int numtries; Bad variable name for same value: float z2; Comment your code diligently Indent properly (this is very easy if you re using emacs hitting tab will automatically indent the line the correct amount) Be consistent in whatever you do

scanf revisited To read from standard input, use scanf What does this do when the user inputs, say, 999999999999999999999? It returns the number of input items successfully assigned. int n; scanf("%i", &n); printf("%i", n);

Logical operators Logical operators allow compound tests && is AND and is OR and! is NOT #include <stdbool.h> int a = 0; b = 10; char ch = 'z'; bool done = false; if (b > a && done == true ) { } // this is false if (b > a done) { } // this is true if (!(b > a && done)) { } // this is true if (!done && a <= 10) { } // this is true if (b!= 10 ch == 'y') { } // this is false

Leap year program Exercise: write a program that allows a user to enter a year and then tell the user whether that year is a leap year or not. A year is a leap year if: It s evenly divisible by 4 and not by 100 OR it s evenly divisible by 400 Use scanf for input and do some error checking on the input. Solution: see leap.c

for loops Basic for loop syntax: for (initial_expression; loop_condition; loop_expression) { statements; } Example: int i; for (i = 0; i < 10; i++) { "printf("*"); }

Nested for loops Exercise: a program that prints a triangle consisting of userspecified symbols Sample output: * ** *** **** ***** ****** Solution: triangle.c (renamed from test.c)

while loops Syntax for while loop: while (condition) { statements; } Exercise: rewrite the triangle printing program using while loops. Solution: triangle2.c (renamed from test2.c)

Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, F(n) = (n-1)+(n-2); Write a program that asks the user for a number, N, and then outputs the Nth Fibonacci number. See fib.c for solution.

e perils of overflow All variable types have a limit to data they can hold. e.g. a 32-bit integer has a range of 2 31 to 2 31 1 ese low and high limits can be found for in the library limits.h. See example overflow.c

Size of types When would you use long vs. short ints? How do I know the max and min values of each type for your platform? ese values can be found in limits.h SHRT_MIN, SHRT_MAX, USHRT_MAX INT_MIN, INT_MAX, UINT_MAX LONG_MIN, LONG_MAX, ULONG_MAX LLONG_MIN, LLONG_MAX, ULLONG_MAX etc. Note there are no min vals for unsigned types - that s because they re always zero.

do-while loops Syntax for do-while loop: do { "statements to repeat; } while (condition);" Exercise: Rewrite triangle printing program using do-while loops. Solution: triangle3.c

More on printf formatting Width and precision %[-][#]<width>.<precision> %3.2f %-2.5f etc." - means left justify # can mean show the prefix (e.g. 0x for a hex number) or show decimal point for floats. Exercise: write a program that creates a table of corresponding Fahrenheit and Celsius temperatures ranging from -10 to 10 degrees Celsius formatted so that the C/F columns are of a fixed width (8 characters wide) and only 2 decimal places are displayed. Formula Fahrenheit in terms of Celsius: F = 9 See temperature.c example 5 C + 32