1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.

Slides:



Advertisements
Similar presentations
COMP 2130 Intro Computer Systems Thompson Rivers University
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Introduction to C Programming
One Dimensional Arrays
Programming Languages and Paradigms The C Programming Language.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Kernighan/Ritchie: Kelley/Pohl:
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
C workshop Yuli Kaplunovsky - Today - Introduction to C Recommended book: The C programming Language / Kernighan & Ritchie.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
0 Chap. 4 Functions and Program Structure 4.1 Basics of Functions 4.2 Functions Returning Non-integers 4.3 External Variables 4.4 Scope Rules 4.5 Header.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Imperative Programming Prof. Béat Hirsbrunner Amine Tafat, PhD Student Matthias Buchs and Raphaël Lesceux, Graduate Students Department of Informatics.
Functions and Program Structure Chapter 4. Introduction Functions break large computing tasks into smaller ones Appropriate functions hide details of.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Computer Science 210 Computer Organization Introduction to C.
C Programming A Modern Approach
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
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.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
CPS120: Introduction to Computer Science Decision Making in Programs.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Engineering Computing I Chapter 4 Functions and Program Structure.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Exam / Homework Exam 1 Starting K&R chapter 4 tonight
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP C Part III.
Functions. Flow of Control Review while for do while goto break & continue switch Relational operators short circuits.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Functions and Program Structure Chapter 4. Introduction Functions break large computing tasks into smaller ones Appropriate functions hide details of.
Functions and Program Structure CSE 2031 Fall June 2016.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Dynamic memory allocation and Intraprogram Communication.
C++ Lesson 1.
Unit 3 Control Structure
Information and Computer Sciences University of Hawaii, Manoa
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Computer Science 210 Computer Organization
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Programmazione I a.a. 2017/2018.
11/10/2018.
Computer Science 210 Computer Organization
Scope, Parameter Passing, Storage Specifiers
Functions and Program Structure
Scope Rules and Storage Types
Functions and Program Structure
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Functions and Program Structure
Variables in C Topics Naming Variables Declaring Variables
C Language B. DHIVYA 17PCA140 II MCA.
Scope Rules.
Presentation transcript:

1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University

Contents 2 Introduction Data Types Arrays & Functions Control Flow Program Structure

Introduction: Example ‘C’ code 3 #include... /* print Fahrenheit-Celsius table for fahr = 0, 20, 40,..., 300, using a loop */ main() { int fahr, celsius; int lower, upper, step; lower = 0;/* lower limit of temperature scale */ upper = 300;// upper limit step = 20;// step size fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9;// integer value? printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; } }

printf("%d\t%d\n", fahr, celsius); %d specifies an integer argument (d: decimal). Output ……..

5 Is there any problem in the above output? The Celsius temperatures in the previous out are not accurate. For example 0 o F is o C. Then? We can use float data type for fahr and celsius instead of int. float fahr, celsius; Then how to print real numbers using printf() ? celsius = 5 * (fahr-32) / 9;// okay? printf("%3.0f\t%6.1f\n", fahr, celsius); %f specifies a floating point argument (f: floating point). 6.1 means Print 6 digits before the decimal point (called width) and one digit after the decimal point (called precision).

6 Some easy errors: printf("%d\t%6.1f\n", fahr, celsius); ??? printf("%3.0f\t%6.1f\n", fahr); ??? printf("%6.1f\n", fahr, celsius); ???

Review 7

Boolean values in ‘C’ 8 When evaluating integers as Booleans in C, are negative numbers true or false? A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

9 If -Else

Arrays 10 /* * C Program to Find the Largest Number in an Array */ #include int main() { int array[50], size, i, largest; printf("\n Enter the size of the array: "); scanf("%d", &size); printf("\n Enter %d elements of the array: ", size); for (i = 0; i < size; i++) scanf("%d", &array[i]); largest = array[0]; for (i = 1; i < size; i++) { if (largest < array[i]) largest = array[i]; } printf("\n largest element present in the given array is : %d", largest); return 0; }

Functions 11 #include int power(int m, int n); int main() { int i; for (i = 0; i < 10; i++) printf("%d %d %d\n", i, power(2, i), power(3, i)); return 0; } int power(int base, int n) { int i, p; p = 1; for (i = 1; i <= n; i++) p = p * base; return p; }

2. Types, Operators, Expressions 12

Data Types 13

Data Types 14

Data Types 15

int getchar(void) 16 C library function - getchar() The C library function int getchar(void) gets a character (an unsigned char) from stdin. This function returns the character read as an unsigned char cast to an int or EOF on end of file or error. Remember that chars in C are integers. char is just another integer type but smaller than int.

3. Control Flow 17 Statement …; Block, or compound statement { … } The same control structures as Java if, … for, while, do-while switch break, continue You must know them all already.

Goto and Labels 18 for (i = 0; i < n; i++) for (j = 0; j < m; j++) if (a[i] == b[j]) goto found; /* didn't find any common element */... found: /* got one: a[i] == b[j] */... Better not use goto statements Can you convert the above code so that you would not use goto ?

4. Functions and Program Structure 19 Function in C is the same as method in Java. return-type function-name(argument declarations) Various parts may be absent.

External Variables 20 If a large number of variables must be shared among functions, external variables (or also called global variables) are more convenient and efficient than long argument lists. External variables are declared outside of any function, usually with initial values. Automatic variables (local variables and parameters) are internal to a function; they come into existence when the function is entered, and disappear when it is left. External variables, on the other hand, are permanent, so they can retain values from one function invocation to the next. Thus if two functions must share some data, yet neither calls the other, it is often most convenient if the shared data is kept in external variables rather than being passed in and out via arguments.

Static Variables 21 The static declaration, applied to an external variable or function, limits the scope of that object to the rest of the source file being compiled. External static thus provides a way to hide names from other files. static char buf[BUFSIZE]; // only in this file static int bufp = 0; // only in this file int getch(void) {... } void ungetch(int c) {... } Static in Java has a bit different meaning.

Register Variables 22 A register declaration advises the compiler that the variable in question will be heavily used. The idea is that register variables are to be placed in machine registers, which may result in smaller and faster programs. But compilers are free to ignore the advice. register int x; register char c; f(register unsigned m, register long n) { register int i;... } Usually for index variables used in loop structures

Initialization 23 Very similar to Java In the absence of explicit initialization, external and static variables are guaranteed to be initialized to zero; but automatic and register variables have undefined (i.e., garbage) initial values.

Scope Rules 24 A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable can not be accessed. There are three places where variables can be declared in C programming language: Inside a function or a block which is called local variables, Outside of all functions which is called global variables. In the definition of function parameters which is called formal parameters.

Scope Rules- local variables 25 Variables that are declared inside a function or block are called local variables. They can be used only by statements that are inside that function or block of code. #include int main () { /* local variable declaration */ int a, b; int c; /* actual initialization */ a = 10; b = 20; c = a + b; printf ("value of a = %d, b = %d and c = %d\n", a, b, c); return 0; }

Scope Rules- global variables 26 Global variables are defined outside of a function, usually on top of the program. The global variables will hold their value throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program. #include /* global variable declaration */ int g; int main () { /* local variable declaration */ int a, b; /* actual initialization */ a = 10; b = 20; g = a + b; printf ("value of a = %d, b = %d and g = %d\n", a, b, g); return 0; } A program can have same name for local and global variables but value of local variable inside a function will take preference.

Scope Rules- formal parameters 27 Function parameters, formal parameters, are treated as local variables with-in that function and they will take preference over the global variables. #include /* global variable declaration */ int a = 20; int main () { /* local variable declaration in main function */ int a = 10; int b = 20; int c = 0; printf ("value of a in main() = %d\n", a); c = sum( a, b); printf ("value of c in main() = %d\n", c); return 0; } int sum(int a, int b) {/* function to add two integers */ printf ("value of a in sum() = %d\n", a); printf ("value of b in sum() = %d\n", b); return a + b; }

Questions? 28