EC 413 Computer Organization

Similar documents
EK131 E5 Introduction to Engineering

C: How to Program. Week /Mar/05

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Chapter 2 - Introduction to C Programming

EL2310 Scientific Programming

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

CSE 303 Lecture 8. Intro to C programming

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Course Outline Introduction to C-Programming

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Work relative to other classes

6.096 Introduction to C++ January (IAP) 2009

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

Computer Systems Lecture 9

Chapter 1 & 2 Introduction to C Language

C Language, Token, Keywords, Constant, variable

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Input And Output of C++

The Compilation Process

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Lecture 2: C Programming Basic

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Introduction to C Programming

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

Introduction to Computing Lecture 03: Basic input / output operations

EL2310 Scientific Programming

Compiling and Running a C Program in Unix

The C language. Introductory course #1

Introduction to the C Programming Language

ECEN 449 Microprocessor System Design. Review of C Programming

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

Topic 6: A Quick Intro To C

Introduction to C Language

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Programming and Data Structure

Unit 7. Functions. Need of User Defined Functions

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Computers Programming Course 5. Iulian Năstac

Fundamentals of Programming & Procedural Programming

Lecture 3. More About C

C and Programming Basics

OBJECT ORIENTED PROGRAMMING

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

A Fast Review of C Essentials Part I

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

Chapter 1 Introduction to Computers and C++ Programming

C++ Quick Guide. Advertisements

Functions. Systems Programming Concepts

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

Lecture 6. Statements

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

CSC231 C Tutorial Fall 2018 Introduction to C

CC112 Structured Programming

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Programming in C. What is C?... What is C?

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

BLM2031 Structured Programming. Zeyneb KURT

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Basic Types and Formatted I/O

Chapter 2: Basic Elements of C++

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Computersactually execute low-level machine language (also known as binary number).

Computer System and programming in C

Introduction to C/C++ Programming

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Lectures 5-6: Introduction to C

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CSCI 2132 Software Development. Lecture 8: Introduction to C

Lecture 3: C Programm

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Lectures 5-6: Introduction to C

Programming refresher and intro to C programming

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

UIC. C Programming Primer. Bharathidasan University

Introduction to C An overview of the programming language C, syntax, data types and input/output

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

1.1 Introduction to C Language. Department of CSE

Chapter 11 Introduction to Programming in C

CSCE 110 PROGRAMMING FUNDAMENTALS

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Programming in C Quick Start! Biostatistics 615 Lecture 4

Motivation was to facilitate development of systems software, especially OS development.


AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Functions. (transfer of parameters, returned values, recursion, function pointers).

UEE1302 (1102) F10: Introduction to Computers and Programming

Transcription:

EC 413 Computer Organization C/C++ Language Review Prof. Michel A. Kinsy

Programming Languages There are many programming languages available: Pascal, C, C++, Java, Ada, Perl and Python All of these languages share core concepts By focusing on these concepts, you are better able to learn any programming language

Programming Languages There are many programming languages available: Pascal, C, C++, Java, Ada, Perl and Python All of these languages share core concepts Hence, by learning C, you are poised to learn other languages, such as Java or Python In this class, we will learn core programming concepts through the powerful C language Why C? It runs nearly as fast as assembly language code

Programming Process Write Program Compile Program Debug Program Run Program

Introduction to C Developed in 1972 by Dennis Ritchie at Bell Labs It is imperative programming language It provides: Efficiency, high performance and high quality software Flexibility and power Many high-level and low-level operations

Introduction to C Developed in 1972 by Dennis Ritchie at Bell Labs It is imperative programming language It provides: Stability and small size code Provide functionality through rich set of function libraries Gateway for other professional languages like C++ and Java

Introduction to C It is used: System software, Compilers, Editors, embedded systems, application programs Data compression, graphics and computational geometry, utility programs Databases, operating systems, device drivers, system level routines The real world still runs on C Most of legacy code in use are in C Many other programming languages are based on C

Introduction to C http://spectrum.ieee.org/computing/software/the-2015- top-ten-programming-languages C" Original comic is available here: http://xkcd.com/519/

Basic C variable types There are five basic data types in C Char: a A single byte capable of holding one character in the local character set Int: 3 An integer of unspecified size Float: 3.14 Single-precision floating point Double: 3.1415926 Double-precision floating point Void: Valueless special purpose type

Basic C variable types Type (32 bit) Smallest Value Largest Value short int -32,768(-2 15 ) 32,767(2 15-1) unsigned short int 0 65,535(2 16-1) Int -2,147,483,648(-2 31 ) 2,147,483,648(2 31-1) unsigned int 0 4,294,967,295 long int -2,147,483,648(-2 31 ) 2,147,483,648(2 31-1) unsigned long int 0 4,294,967,295

Variable assignment In C variables must be declared They are given values through assignments Assignment is done with the '=' operator Declarations int number_of_students; float average_gpa; Assignments number_of_students = 12; average_gpa = 3.9;

Types Variable assignment In C variables must be declared They are given values through assignments Assignment is done with the '=' operator Declarations int number_of_students; float average_gpa; Assignments number_of_students = 12; average_gpa = 3.9; Names Values

A Simple C Program #include <stdio.h> / Header files / int main(void) { printf ( Hello World!\n ) ; return 0; }

C Program compilation Compile: gcc o myhello hello.c Run:./myhello

C Program Analysis #include <stdio.h> / Header files / It is a preprocessor directive It tells computer to load contents of the file It allows standard input/output operations Comments are used to describe program Text surrounded by /* and */ is ignored by computer Lines starting with // are also ignored

int main (void) C Program Analysis C programs contain one or more functions, exactly one of which must be main Parenthesis used to indicate a function int means that main "returns" an integer value Braces ({ and }) indicate a block Bodies of all functions must be contained in braces printf ( Hello World!\n ) printf and scanf functions

prinf C Program Analysis Sends output to standard out General form printf(format descriptor, var1, var2, ); printf( %s\n, Hello world ); Translation: Print hello world as a string followed by a newline character printf( %d\t%f\n, j, k); Translation: Print the value of the variable j as an integer followed by a tab followed by the value of floating point variable k followed by a new line

scanf C Program Analysis Gets inputs from user General form scanf(format descriptor, &var1, &var2, ); scanf( %f, &i); Translation: Get floating point input i from user scanf( %d %f\n, &j, &k); Translation: Get the value of the variable j as an integer followed by the value of floating point variable k from user Blocks program until user enters input

C Program Analysis Some special characters are not visible directly in the output stream These begin with an escape character (\); \n newline \t horizontal tab \a alert bell \v vertical tab

C Program Operations Arithmetic operators + plus - minus * times / divided by #include <stdio.h> / Header files / int number1, number2, number3; int main(void) { scanf( Enter number1:%d, &number1); scanf( \nenternumber2:%d, &number2); number3 = number1 + number 2; printf ( \n Number1 + number 2 = %d\n, number3 ) ; number3 = number1 - number 2; printf ( \n Number1 - number 2 = %d\n, number3 ) ; number3 = number1 * number 2; printf ( \n Number1 * number 2 = %d\n, number3 ) ; } number3 = number1 / number 2; printf ( \n Number1 / number 2 = %d\n, number3 ) ; return 0;

C Program Comparators Relational operators: == is equal to!= is not equal to > greater than < less than >= greater than or equal to <= less than or equal to

C Program Logical Operators There are two logical operators in C logical or An expression formed with evaluates to true if any one of its components is true && logical and An expression formed with && evaluates to true if all of its components are true

In C Advance Data types Arrays (a list of data (all of the Same Data Type!)) int grades [ ] = {94, 78, 88, 90, 93, 87, 59}; Structures (a collection of named data referring to a single entity) structstudent { char Name [50] ; int id; float GPA; char major [25] ; } ;

Pointers in C Advance Data types Pointers are memory addresses Every variable has a memory address Symbol & means take the address of e.g., &x Symbol * means take the value of e.g., *p Symbol * is also used to denote a pointer type e.g., int *q;

Pointers in C Advance Data types Declaration of integer pointers and and an integer number int pointer1, * pointer2 ; int number1; Setting pointer1 equal to the address of number1 pointer1 = &number1; Setting pointer2 equal to pointer1 pointer2 = pointer1;

Abstraction Data Types in C++ Data structures A set of operations The interface is the only access mechanism to the data structures. Data Structures Operations Interface

Abstraction Data Type Data Structures Data Structures Operations Interface Operations Interface Data Structures Data Structures Operations Interface Operations Interface Data Structures Operations Interface

Object-Oriented Programming Example of an abstract data structure #include <iostream> using namespace std; class Robot_Rectangle { private: int width; int length;

Object-Oriented Programming Example of an abstract data structure #include <iostream> using namespace std; class Robot_Rectangle { private: int width; int length; Class keyword

Object-Oriented Programming Example of an abstract data structure #include <iostream> using namespace std; class Robot_Rectangle { private: int width; int length; Make them less visible from the outside

Object-Oriented Programming Example of an abstract data structure #include <iostream> using namespace std; class Robot_Rectangle { private: int width; int length; public: Robot_Rectangle (void) { width = 0; length = 0; } void set(int w, int l){ width = w; length = l; } int get_width(void) { return width; }

Object-Oriented Programming Example of an abstract data structure int get_length(void) { return length; } int area(void) { int calculated_area = 0; calculated_area = width*length; return calculated_area; } };

Conditional execution Conditional constructs provide the ability to control whether a statement list is executed If statement If if (Expression) Action true Expression false Action

Conditional execution Conditional constructs provide the ability to control whether a statement list is executed If statement If if (Expression) Action Expression true Action false int num1; int num2; printf("enter two integers: ); scanf( %d %d, &num1,&num2); if (num1 > num2) { int remember_num1 = num1; num1 = num2; num2 = remember_num1; } prinf( Inputs in sorted order: %d \t %d, num1, num2 );

Conditional execution Conditional constructs provide the ability to control whether a statement list is executed If statement if-else if (Expression) Action1 else Action2 true Expression false Action 1 Action 2

Conditional execution Conditional constructs provide the ability to control whether a statement list is executed If statement if-else if (Expression) Action1 else Action2 true Expression false Action 1 Action 2 int num1; int num2; printf("enter two integers: ); scanf( %d %d, &num1, &num2); int max; if (num1 > num2) { max = num1; } else { Max = num2 } prinf( Maximum number is: %d, max);

For loop While loop Do while loop Conditional execution

For loops Iteration The general format when using for loops is for ( initialization; loopcontinuationtest; increment ) Statement Initialize variable Condition Test the variable true statement Increment variable false

For loops Iteration The general format when using for loops is for ( initialization; loopcontinuationtest; increment ) Statement int counter; for(counter = 1; counter <= 10; counter++ ) printf( Current counter value %d\n, counter);

While loop Iteration while loop repeated until condition becomes false Initialization; while (loopcontinuationtest){ statement increment; } condition true statement false

While loop Iteration while loop repeated until condition becomes false Initialization; while (loopcontinuationtest){ statement increment; } int counter =1; //initialization while (counter <= 10){ //repetition condition printf( Current counter value %d\n, counter); counter++; //increment } condition true statement false

Do while Iteration do/while repetition structure is similar to the while structure Condition for repetition tested after the body of the loop is executed do { statement } while ( condition ); statement condition true false

Functions A Definition: A function is a named, independent section of C code that performs a specific task and optionally returns a value to the calling program or/and receives values(s) from the calling program There are two types of function Predefined functions Standard libraries like stdio.h, math.h User-defined functions Programmer created functions for specialized tasks e.g., int fibonacci(int n)

Functions Characteristics of a function Function header: Its has a return type, a unique name, and list of parameters with their types Return_type function_name (type1 parameter1, type2 parameter2 ){ variable declaration(s) statement(s) } Examples void function1 (int x, float y, char z) float function2 (float x, double y) int function3 (long size) void function4 (void)

Functions The rules govern the use of variables in functions: To use a variable in a function, it must be declared either in the function header or the function body For a function to obtain a value from the calling program (caller), the value must be passed as an argument (the actual value) unless it is a global value /* declare and define */ int exponential (int x) { int result = 1; int i; for (i = 0, i< x, i++) result *= 2; return result; } int main() { /* function call */ int y; y = exponential(3); }

Functions The rules govern the use of variables in functions: For a calling program (caller) to obtain a value from function, the value must be explicitly returned from the called function (callee) unless it is updated through a global variable /* declare and define */ int exponential (int x) { int result = 1; int i; for (i = 0, i< x, i++) result *= 2; return result; } int main() { /* function call */ int y; y = exponential(3); }

Recursion Often it is difficult to express a problem explicitly For example the Fibonacci sequence: 0,1,1,2,3,5,8,13,21,34,55, It is difficult to follow the logic of this sequence However, a recursive definition consisting of expressing higher terms in the sequence in terms of lower terms Recursive definition for {f n }: Initialization: f 0 = 0, f 1 = 1 Recursion: n = f n-1 + f n-2 for n > 1

Recursion Sometimes the best way to solve a problem is by solving a smaller version of the exact same problem first Recursion is a technique that solves a problem by solving a smaller problem of the same type The technique ends up with functions that call themselves (recursive functions)

Logic of recursive functions Recursive definition and inductive proofs are complement each other A recursive function has two parts Initialization analogous to induction base cases Recursion analogous to induction step Recursive definition for {f n }: Initialization: f 0 = 0, f 1 = 1 Recursion: n = f n-1 + f n-2 for n > 1

Factorial function Recursion Iterative implementation int Factorial(int n) { int count; int fact = 1; for(count = 2; count <= n; count++) fact = fact * count; return fact; }

Factorial function Recursion Recursive implementation int Factorial(int n) { if (n==0) // base case return 1; else return n * Factorial(n-1); }

Next Class Stack and Heap structures