Fundamentals of Programming & Procedural Programming

Size: px
Start display at page:

Download "Fundamentals of Programming & Procedural Programming"

Transcription

1 Universität Duisburg-Essen PRACTICAL TRAINING TO THE LECTURE Fundamentals of Programming & Procedural Programming Session Eight: Math Functions, Linked Lists, and Binary Trees Name: First Name: Tutor: Matriculation-Number: Group-Number: Date: Prof. Dr.Ing. Axel Hunger Dipl.-Ing. Joachim Zumbrägel Universität Duisburg-Essen Faculty of Engineering, Department Electrical Engineering and Information Technology Computer Engineering Procedural Programming/Lab8 1

2 Math Functions There are various calculations which involve operations other than the usual four basic operations of addition, subtraction, multiplication and division. For this, C contains a library special for mathematical functions. Though to use these functions the header file <cmath> should be first included. The <cmath> header file provides a collection of functions that enable you to perform common mathematical calculations. For example, you can calculate the square root of with the function call sqrt(3600.0) The above expression evaluates to Function arguments can be constants, variables or more complex expressions. If a = 12.0, b = 4.0 and c = 7.0, then the statements root = sqrt(a + b * c); printf("the sqrt of a + b * c is %f", root); calculate and print the square root of * 7.0. Exercise 8.1: What is the result of the expression given in the above program? Procedural Programming/Lab8 2

3 The function ceil(x) Description: This function rounds x to the smallest integer which is not less than x. If we want to ceil the value 5.2 to nearest integer, we can code as: ceil(5.2); This will give us a result as 6. Exercise 8.2: What is the result after the following function is performed? ceil(-5.8); The function cos(x) Description: This function gives us the trigonometric cosine of x in radians. If we want to calculate cos of 0, we can code as: cos(0.0); This will give us a result as 1.0. Exercise 8.3: What is the result after the following function is performed? Procedural Programming/Lab8 3

4 The function exp(x) Description: This function gives us the exponential function e x. If we want to calculate e 1.0, we can code as: exp(1.0); This will give us a result as Exercise 8.4: What is the result after the following function is performed? exp(5.0); The function fabs(x) Description: This function gives us the absolute value of x. If we want to calculate the absolute value of 5.1, we can code as: fabs(5.1); This will give us a result as 5.1. Exercise 8.5: What is the result after the following function is performed? fabs(-4.67); Procedural Programming/Lab8 4

5 The function floor(x) Description: This function rounds x to the largest integer which is not greater than x. If we want to floor the value 9.2, we can code as: floor(9.2); This will give us a result as 9.0. Exercise 8.6: What is the result after the following function is performed? floor(-5.9); The function fmod(x,y) Description: This function gives us the remainder of x/y as a floating point number. If we want to calculate 2.6/1.2 as a float, we can code as: fmod(2.6,1.2); This will give us a result as 0.2. Exercise 8.7: What is the result after the following function is performed? fmod(9.6,3.2); Procedural Programming/Lab8 5

6 The function log(x) Description: This function gives us the natural logarithm of x using base e. If we want to calculate the logarithm of , we can code as: log( ); This will give us a result as 1.0. Exercise 8.8: What is the result after the following function is performed? log( ); The function log10(x) Description: This function gives us the logarithm of x using base 10. If we want to calculate the logarithm of 10.0 using base 10, we can code as: log10(10.0); This will give us a result as 1.0. Exercise 8.9: What is the result after the following function is performed? log10(200.0); Procedural Programming/Lab8 6

7 The function pow(x,y) Description: This function gives x raised to power y (x y ). If we want to calculate 3 raised to power 5, we can code as: pow(3,5); This will give us a result as 243. Exercise 8.10: What is the result after the following function is performed? pow(5,.2); The function sin(x) Description: This function gives us the trigonometric sine of x radians. If we want to calculate the sin of 0, we can code as: sin(0.0) This will give us a result as 0. Exercise 8.11: What is the result after the following function is performed? sin(5.5); Procedural Programming/Lab8 7

8 The function sqrt(x) Description: This function gives us the square root of x (where x is a nonnegative value) If we want to calculate the square root of 9.0, we can code as: sqrt(9.0); This will give us a result as 3.0. Exercise 8.12: What is the result after the following function is performed? sqrt(5.0); The function tan(x) Description: This function gives us the trigonometric tangent of x in radians. If we want to calculate the tan of 0.0, we can code as: tan(0.0); This will give us a result as 0. Exercise 8.13: What is the result after the following function is performed? tan(5.5); Procedural Programming/Lab8 8

9 The following program demonstrates the use of pow() function using C syntax. /* example: using pow() function */ #include <stdio.h> #include <math.h> int main() float base; float exponent; float num; printf("enter a value for the base: "); scanf("%f", &base); printf("enter a value for the exponent: "); scanf("%f", &exponent); printf("%f raised to the power %f is %f", base, exponent, pow(base,exponent)); return 0; Procedural Programming/Lab8 9

10 Exercise 8.13: What is the value of i after each of the following functions are performed? a) i = fabs(4.5); b) i = floor(4.5); c) i = fabs(0.0); d) i = ceil(0.0); e) i = fabs(-5.2); f) i = ceil(-5.2); Procedural Programming/Lab8 10

11 Linked Lists A linked list is a list of elements in which the elements of the list can be placed anywhere in the memory, and these elements are linked with each other using an explicit link field. This is done by storing the address of the next element in the link field of the previous element. Successive elements are connected by pointers. The last element points to NULL. A linked list can grow or shrink in size during execution of a program and can be made just as long as required. The programmer writes a struct definition which consists of variables holding information about something, and then has a pointer to a struct of its type. Each of these individual struct in the list is commonly known as a node. data next data next data next data next Figure: A Linked List The singly linked list is the easiest of the linked list, which has one link per node. Basic operations of a linked list Create: Creating a linked list Insert: Inserting a new element at the end of the list Append: Appending or adding a node at the end of the list Delete: Deleting a node from the list. Count: Counting the number of nodes of a linked list Procedural Programming/Lab8 11

12 Creating a Linked list The linked list is created by using an insert() function. The insert() function takes a pointer to an existing list as the first parameter, and a data value with which the new node is to be created as the second parameter. It creates the new node by using the data value and then appends it to the end of the list. It then returns a pointer to the first node of the list. To begin with, the list is empty, so the pointer to the starting node is NULL. Therefore, when insert is called the first time, the new node created by the insert function becomes the start node. The insert() function terminates when it creates a new node with the supplied data value and appends it to the end of the list. Inserting a Node in a List This function takes the start node and data to be inserted as arguments. New node is inserted at the end so, it iterates through the list till we encounter the last node. Then, it allocates memory for the new node and puts data in it. Lastly, it stores the address in the next field of the new node as NULL. A next B next C next D next X Item to be inserted A next B next C next D next X Item to be inserted Procedural Programming/Lab8 12

13 Deleting a node from the list This function takes the starting node of the linked list as the pointer and the data to be deleted as the arguments. It first, goes to the node for which the node next to it has to be deleted, if that node points to NULL then the element to be deleted is not present in the list. Else, the pointer now points to a node and the node next to it has to be removed, it declares a temporary node (temp) which points to the node which has to be removed. It then stores the address of the node next to the temporary node in the next field of the node pointer. Thus, by breaking the link it removes the node which is next to the pointer (which is also the temp). Because it deleted the node, it no longer requires the memory used for it, so the free() function will deallocate that memory. Item to be deleted A next B next C next D next Deleted item A next B next C next D next Procedural Programming/Lab8 13

14 The following example shows us how to insert a node, delete a node, to display the elements of the list, and to determine the size of the linked list after it is created: #include<stdio.h> #include<stdlib.h> struct node int data; struct node *next; *head; /* A function to append a node at the end of the linked list */ void append(int num) struct node *temp,*right; temp = (struct node *)malloc(sizeof(struct node)); temp->data = num; right = (struct node *)head; while(right->next!= NULL) right = right->next; right->next = temp; right = temp; right->next = NULL; void add(int num) struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); temp->data = num; if (head == NULL) head = temp; head->next = NULL; else temp->next = head; head = temp; void addafter(int num, int loc) int i; struct node *temp,*left,*right; right = head; for(i=1;i<loc;i++) Procedural Programming/Lab8 14

15 left = right; right = right->next; temp = (struct node *)malloc(sizeof(struct node)); temp->data = num; left->next = temp; left = temp; left->next = right; return; /* A function to insert a node in a singly linked list */ void insert(int num) int c = 0; struct node *temp; temp = head; if(temp == NULL) add(num); else while(temp!=null) if(temp->data < num) c++; temp = temp->next; if(c == 0) add(num); else if(c<nodecount()) addafter(num,++c); else append(num); /* A function to delete a node in a singly linked list */ int delete(int num) struct node *temp, *prev; temp = head; while(temp!=null) if(temp->data == num) Procedural Programming/Lab8 15

16 if(temp == head) head = temp->next; free(temp); return 1; else prev->next = temp->next; free(temp); return 1; else prev = temp; temp = temp->next; return 0; /* A function to display the values of nodes in a singly linked list */ void display(struct node *r) r = head; if(r == NULL) return; while(r!= NULL) printf("%d ", r->data); r = r->next; printf("\n"); /* A function to count the number of nodes in a linked list */ int nodecount() struct node *n; int count = 0; n = head; while(n!= NULL) n = n->next; count++; return count; Procedural Programming/Lab8 16

17 /* the main function */ int main() int choice,num; struct node *n; head = NULL; while(1) printf("\nlinked list Operations\n"); printf(" \n"); printf("1.insert\n"); printf("2.display\n"); printf("3.size\n"); printf("4.delete\n"); printf("5.exit\n"); printf("please enter your choice: "); if(scanf("%d",&choice)<=0) printf("enter only an Integer\n"); exit(0); else switch(choice) case 1: printf("enter the data values to be placed in a node: "); scanf("%d", &num); insert(num); break; case 2: if(head == NULL) printf("the list is Empty\n"); else printf("the data values in the list are: "); display(n); break; case 3: printf("the size of the list is: %d\n", nodecount()); break; case 4: if(head == NULL) printf("the list is Empty\n"); else Procedural Programming/Lab8 17

18 printf("enter the value to be deleted: "); scanf("%d", &num); if(delete(num)) printf("%d deleted successfully\n", num); else printf("%d not found in the list\n", num); break; case 5: return 0; return 0; default: printf("invalid option!\n"); The function malloc(); is used to allocate a certain amount of memory during the execution of a program. If the request is granted, the operating system will reserve the requested amount of memory. The space is not initialized and may contain any values initially. Again a pointer is returned to the start of the area if successful, otherwise NULL. When the amount of memory is not needed anymore, you must return it back to the operating system by using the function free(). Procedural Programming/Lab8 18

19 Exercise 8.14: Write a C program which deletes a node from a singly linked list with the minimum value. Procedural Programming/Lab8 19

20 Trees Apart from linked lists, trees are another type of dynamical data structures also used in C. They are used on a collection of data items to put into a hierarchical structure. Binary Trees A binary tree is a special case of the tree in which no node of the tree can have a degree of more than 2. The binary tree is made of nodes, where each node contains a "left" pointer, a "right" pointer, and a data element. The "root" pointer points to the topmost node in the tree. The left and right pointers point to smaller subtrees on either side of the node. A null pointer represents a binary tree with no elements, i.e. the empty tree. Root 5 Root node Left and Right subtree pointers Figure: A Binary Tree Procedural Programming/Lab8 20

21 Creating a Binary Search Tree In C, the binary tree is built with a node type like: struct binarytree int val; struct binarytree *right; struct binarytree *left; ; The following example shows us how to create a binary search tree. It creates the binary search tree by using a function called insert, which creates a new node with the data value given to it as an argument, and inserts it into an already existing tree whose root pointer is also passed as an argument. #include<stdlib.h> #include<stdio.h> struct binarytree int val; struct binarytree *right; struct binarytree *left; ; typedef struct binarytree node; /* A function to insert a node in the binary tree */ void insert(node **btree, node *item) if(!(*btree)) *btree = item; return; if(item->val < (*btree)->val) insert(&(*btree)->left, item); else if(item->val > (*btree)->val) insert(&(*btree)->right, item); /* A function to print the value of a node in the binary tree */ Procedural Programming/Lab8 21

22 void print(node *btree) if(btree->left) print(btree->left); printf("%d\n",btree->val); if(btree->right) print(btree->right); int main() node *currentvalue, *root; int i; root = NULL; for(i = 1; i <= 20; i++) currentvalue = (node*)malloc(sizeof(node)); currentvalue->left = currentvalue->right = NULL; currentvalue->val = rand(); insert(&root, currentvalue); print(root); return 0; Procedural Programming/Lab8 22

23 Exercise 8.15: Write a C program which uses two functions, first to count the number of leaf nodes of the binary tree and then the second one to delete all the leaf nodes of that binary tree. Procedural Programming/Lab8 23

Procedural Programming

Procedural Programming Exercise 6 (SS 2016) 04.07.2015 What will I learn in the 6. exercise Math functions Dynamic data structures (linked Lists) Exercise(s) 1 Home exercise 4 (3 points) Write a program which is able to handle

More information

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

6-1 (Function). (Function) !*+!#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x (Function) -1.1 Math Library Function!"#! $%&!'(#) preprocessor directive #include !*+!"#!, Function Description Example sqrt(x) square root of x sqrt(900.0) is 30.0 sqrt(9.0) is 3.0 exp(x) log(x)

More information

Procedural Programming & Fundamentals of Programming

Procedural Programming & Fundamentals of Programming & Fundamentals of Programming Exercise 4 (SS 2018) 12.06.2018 What will I learn in the 5. exercise Files Math functions Dynamic data structures (linked Lists) Exercise(s) 1 Files A file can be seen as

More information

Functions. Systems Programming Concepts

Functions. Systems Programming Concepts Functions Systems Programming Concepts Functions Simple Function Example Function Prototype and Declaration Math Library Functions Function Definition Header Files Random Number Generator Call by Value

More information

Fundamentals of Programming & Procedural Programming

Fundamentals of Programming & Procedural Programming Universität Duisburg-Essen PRACTICAL TRAINING TO THE LECTURE Fundamentals of Programming & Procedural Programming Session Four: Functions: Built-in, Parameters and Arguments, Fruitful and Void Functions

More information

C Functions. 5.2 Program Modules in C

C Functions. 5.2 Program Modules in C 1 5 C Functions 5.2 Program Modules in C 2 Functions Modules in C Programs combine user-defined functions with library functions - C standard library has a wide variety of functions Function calls Invoking

More information

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design What is a Function? C Programming Lecture 8-1 : Function (Basic) A small program(subroutine) that performs a particular task Input : parameter / argument Perform what? : function body Output t : return

More information

CT 229 Java Syntax Continued

CT 229 Java Syntax Continued CT 229 Java Syntax Continued 06/10/2006 CT229 Lab Assignments Due Date for current lab assignment : Oct 8 th Before submission make sure that the name of each.java file matches the name given in the assignment

More information

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 5. Playing with Data Modifiers and Math Functions Getting Controls

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 5. Playing with Data Modifiers and Math Functions Getting Controls Readin from and Writint to Standart I/O BIL104E: Introduction to Scientific and Engineering Computing Lecture 5 Playing with Data Modifiers and Math Functions Getting Controls Pointers What Is a Pointer?

More information

Procedural Programming

Procedural Programming Universität Duisburg-Essen PRACTICAL TRAINING TO THE LECTURE Procedural Programming Session Five: Arrays Name: First Name: Tutor: Matriculation-Number: Group-Number: Date: Prof. Dr.Ing. Axel Hunger Dipl.-Ing.

More information

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

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4 BIL 104E Introduction to Scientific and Engineering Computing Lecture 4 Introduction Divide and Conquer Construct a program from smaller pieces or components These smaller pieces are called modules Functions

More information

C++ Programming Lecture 11 Functions Part I

C++ Programming Lecture 11 Functions Part I C++ Programming Lecture 11 Functions Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department Introduction Till now we have learned the basic concepts of C++. All the programs

More information

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1 Chapter 6 Function C++, How to Program Deitel & Deitel Spring 2016 CISC1600 Yanjun Li 1 Function A function is a collection of statements that performs a specific task - a single, well-defined task. Divide

More information

Chapter 4: Basic C Operators

Chapter 4: Basic C Operators Chapter 4: Basic C Operators In this chapter, you will learn about: Arithmetic operators Unary operators Binary operators Assignment operators Equalities and relational operators Logical operators Conditional

More information

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson) Lecture 9 Functions Dr M Kasim A Jalil Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson) Objectives In this chapter, you will learn: To understand how to construct programs modularly

More information

ECET 264 C Programming Language with Applications

ECET 264 C Programming Language with Applications ECET 264 C Programming Language with Applications Lecture 10 C Standard Library Functions Paul I. Lin Professor of Electrical & Computer Engineering Technology http://www.etcs.ipfw.edu/~lin Lecture 10

More information

Introduction to Programming

Introduction to Programming Introduction to Programming session 9 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2010 These slides are created using Deitel s slides Sahrif University of Technology Outlines

More information

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University Lesson #3 Variables, Operators, and Expressions Variables We already know the three main types of variables in C: int, char, and double. There is also the float type which is similar to double with only

More information

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial CSI31 Lecture 5 Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial 1 3.1 Numberic Data Types When computers were first developed, they were seen primarily as

More information

C Programs: Simple Statements and Expressions

C Programs: Simple Statements and Expressions .. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar.. C Programs: Simple Statements and Expressions C Program Structure A C program that consists of only one function has the following

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 03 - Stephen Scott (Adapted from Christopher M. Bourke) 1 / 41 Fall 2009 Chapter 3 3.1 Building Programs from Existing Information

More information

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information. Chapter 3 Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus Lecture 03 - Introduction To Functions Christopher M. Bourke cbourke@cse.unl.edu 3.1 Building Programs from Existing

More information

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions Computer Science & Engineering 150A Problem Solving Using Computers Lecture 03 - Functions Stephen Scott (Adapted from Christopher M. Bourke) Fall 2009 1 / 1 cbourke@cse.unl.edu Chapter 3 3.1 Building

More information

Standard Library Functions Outline

Standard Library Functions Outline Standard Library Functions Outline 1. Standard Library Functions Outline 2. Functions in Mathematics #1 3. Functions in Mathematics #2 4. Functions in Mathematics #3 5. Function Argument 6. Absolute Value

More information

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch Lecture 3 CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions Review Conditions: if( ) / else switch Loops: for( ) do...while( ) while( )... 1 Examples Display the first 10

More information

C++ Overview. Chapter 1. Chapter 2

C++ Overview. Chapter 1. Chapter 2 C++ Overview Chapter 1 Note: All commands you type (including the Myro commands listed elsewhere) are essentially C++ commands. Later, in this section we will list those commands that are a part of the

More information

CSE123. Program Design and Modular Programming Functions 1-1

CSE123. Program Design and Modular Programming Functions 1-1 CSE123 Program Design and Modular Programming Functions 1-1 5.1 Introduction A function in C is a small sub-program performs a particular task, supports the concept of modular programming design techniques.

More information

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 6: User-Defined Functions I In this chapter, you will: Objectives Learn about standard (predefined) functions and discover

More information

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =?

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =? Lecture 14 Math in C Daily Puzzle Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =? Daily Puzzle SOLUTION Eleven plus two = twelve plus one Announcements

More information

Assoc. Prof. Dr. Tansu FİLİK

Assoc. Prof. Dr. Tansu FİLİK Assoc. Prof. Dr. Tansu FİLİK Computer Programming Previously on Bil 200 Midterm Exam - 1 Midterm Exam - 1 126 students Curve: 49,78 Computer Programming Arrays Arrays List of variables: [ ] Computer Programming

More information

C++ PROGRAMMING SKILLS Part 3 User-Defined Functions

C++ PROGRAMMING SKILLS Part 3 User-Defined Functions C++ PROGRAMMING SKILLS Part 3 User-Defined Functions Introduction Function Definition Void function Global Vs Local variables Random Number Generator Recursion Function Overloading Sample Code 1 Functions

More information

Introduction to Programming

Introduction to Programming Introduction to Programming Department of Computer Science and Information Systems Tingting Han (afternoon), Steve Maybank (evening) tingting@dcs.bbk.ac.uk sjmaybank@dcs.bbk.ac.uk Autumn 2017 Week 4: More

More information

Functions and an Introduction to Recursion Pearson Education, Inc. All rights reserved.

Functions and an Introduction to Recursion Pearson Education, Inc. All rights reserved. 1 6 Functions and an Introduction to Recursion 2 Form ever follows function. Louis Henri Sullivan E pluribus unum. (One composed of many.) Virgil O! call back yesterday, bid time return. William Shakespeare

More information

ANSI C Programming Simple Programs

ANSI C Programming Simple Programs ANSI C Programming Simple Programs /* This program computes the distance between two points */ #include #include #include main() { /* Declare and initialize variables */ double

More information

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types Introduction to Computer Programming in Python Dr William C Bulko Data Types 2017 What is a data type? A data type is the kind of value represented by a constant or stored by a variable So far, you have

More information

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries Hazırlayan Asst. Prof. Dr. Tansu Filik Computer Programming Previously on Bil 200 Low-Level I/O getchar, putchar,

More information

Chapter 3 - Functions

Chapter 3 - Functions Chapter 3 - Functions 1 Outline 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number

More information

Structured programming

Structured programming Exercises 7 Version 1.0, 17 November, 2016 Table of Contents 1. Functions................................................................... 1 1.1. Reminder from lectures..................................................

More information

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Python Lists: Example 1: >>> items=[apple, orange,100,25.5] >>> items[0] 'apple' >>> 3*items[:2] Python Lists: Lists are Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). All the items belonging to a list can be of different data type.

More information

C Program Structures

C Program Structures Review-1 Structure of C program Variable types and Naming Input and output Arithmetic operation 85-132 Introduction to C-Programming 9-1 C Program Structures #include void main (void) { } declaration

More information

Summary of basic C++-commands

Summary of basic C++-commands Summary of basic C++-commands K. Vollmayr-Lee, O. Ippisch April 13, 2010 1 Compiling To compile a C++-program, you can use either g++ or c++. g++ -o executable_filename.out sourcefilename.cc c++ -o executable_filename.out

More information

Fundamentals of Programming & Procedural Programming

Fundamentals of Programming & Procedural Programming Universität Duisburg-Essen PRACTICAL TRAINING TO THE LECTURE Fundamentals of Programming & Procedural Programming Session Seven: Strings and Files Name: First Name: Tutor: Matriculation-Number: Group-Number:

More information

Data accessing is faster because we just need to specify the array name and the index of the element to be accssed.. ie arrays are simple to use

Data accessing is faster because we just need to specify the array name and the index of the element to be accssed.. ie arrays are simple to use Module 3 Linear data structures and linked storage representation Advantages of arrays Linear data structures such as stacks and queues can be represented and implemented using sequential allocation ie

More information

MYSQL NUMERIC FUNCTIONS

MYSQL NUMERIC FUNCTIONS MYSQL NUMERIC FUNCTIONS http://www.tutorialspoint.com/mysql/mysql-numeric-functions.htm Copyright tutorialspoint.com MySQL numeric functions are used primarily for numeric manipulation and/or mathematical

More information

Programming Fundamentals for Engineers Functions. Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. Modular programming.

Programming Fundamentals for Engineers Functions. Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. Modular programming. Programming Fundamentals for Engineers - 0702113 7. Functions Muntaser Abulafi Yacoub Sabatin Omar Qaraeen 1 Modular programming Your program main() function Calls AnotherFunction1() Returns the results

More information

Using Free Functions

Using Free Functions Chapter 3 Using Free Functions 3rd Edition Computing Fundamentals with C++ Rick Mercer Franklin, Beedle & Associates Goals Evaluate some mathematical and trigonometric functions Use arguments in function

More information

Chapter 6 - Notes User-Defined Functions I

Chapter 6 - Notes User-Defined Functions I Chapter 6 - Notes User-Defined Functions I I. Standard (Predefined) Functions A. A sub-program that performs a special or specific task and is made available by pre-written libraries in header files. B.

More information

Chapter 2. Outline. Simple C++ Programs

Chapter 2. Outline. Simple C++ Programs Chapter 2 Simple C++ Programs Outline Objectives 1. Building C++ Solutions with IDEs: Dev-cpp, Xcode 2. C++ Program Structure 3. Constant and Variables 4. C++ Operators 5. Standard Input and Output 6.

More information

Maths Functions User Manual

Maths Functions User Manual Professional Electronics for Automotive and Motorsport 6 Repton Close Basildon Essex SS13 1LE United Kingdom +44 (0) 1268 904124 info@liferacing.com www.liferacing.com Maths Functions User Manual Document

More information

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra Linked List in Data Structure By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra Linked List Like arrays, Linked List is a linear data

More information

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review CS 2060 Variables Variables are statically typed. Variables must be defined before they are used. You only specify the type name when you define the variable. int a, b, c; float d, e, f; char letter; //

More information

Methods CSC 121 Fall 2014 Howard Rosenthal

Methods CSC 121 Fall 2014 Howard Rosenthal Methods CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Understand what a method is in Java Understand Java s Math Class Learn the syntax of method construction Learn both void methods and methods that

More information

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects Function Input and Output Input through actual parameters Output through return value Only one value can be returned Input/Output through side effects printf scanf 2 tj Function Input and Output int main(void){

More information

Introduction to C Language

Introduction to C Language Introduction to C Language Instructor: Professor I. Charles Ume ME 6405 Introduction to Mechatronics Fall 2006 Instructor: Professor Charles Ume Introduction to C Language History of C Language In 1972,

More information

Engineering Problem Solving with C++, Etter/Ingber

Engineering Problem Solving with C++, Etter/Ingber Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs C++, Second Edition, J. Ingber 1 Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input

More information

LAB 7 FUNCTION PART 2

LAB 7 FUNCTION PART 2 LAB 7 FUNCTION PART 2 School of Computer and Communication Engineering Universiti Malaysia Perlis 1 OBJECTIVES 1. To differentiate the file scope and block scope. 2. To write recursive function. 3. To

More information

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR 1 Functions Functions are everywhere in C Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Introduction Function A self-contained program segment that carries

More information

Function I/O. Last Updated 10/30/18

Function I/O. Last Updated 10/30/18 Last Updated 10/30/18 Program Structure Includes Function Declarations void main(void){ foo = fun1(a, b); fun2(2, c); if(fun1(c, d)) { } } Function 1 Definition Function 2 Definition 2 tj Function Input

More information

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura CS 2060 Week 4 1 Modularizing Programs Modularizing programs in C Writing custom functions Header files 2 Function Call Stack The function call stack Stack frames 3 Pass-by-value Pass-by-value and pass-by-reference

More information

UNIT 4: Linked List Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.

UNIT 4: Linked List Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru. UNIT 4: Linked List Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru. 1 Table of Contents 1. Array Implementation of Linked List...3 2. Queue using Array Implementation

More information

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from INTRODUCTION TO C++ FUNCTIONS Original slides are from http://sites.google.com/site/progntut/ Dept. of Electronic Engineering, NCHU Outline 2 Functions: Program modules in C Function Definitions Function

More information

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5. Week 2: Console I/O and Operators Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.1) CS 1428 Fall 2014 Jill Seaman 1 2.14 Arithmetic Operators An operator is a symbol that tells the computer to perform specific

More information

Methods: A Deeper Look

Methods: A Deeper Look 1 2 7 Methods: A Deeper Look OBJECTIVES In this chapter you will learn: How static methods and variables are associated with an entire class rather than specific instances of the class. How to use random-number

More information

www.thestudycampus.com Methods Let s imagine an automobile factory. When an automobile is manufactured, it is not made from basic raw materials; it is put together from previously manufactured parts. Some

More information

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine

More information

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction Functions Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur Programming and Data Structure 1 Function Introduction A self-contained program segment that

More information

Functions and Recursion

Functions and Recursion Functions and Recursion 1 Outline Introduction Program Components in C++ Math Library Functions Functions Function Definitions Function Prototypes Header Files Random Number Generation Example: A Game

More information

OUTLINE. Review Functions Pointers Function calls Array Pointers Pointer Arrays Data Structures and dynamic Memory Function Pointers quicksort example

OUTLINE. Review Functions Pointers Function calls Array Pointers Pointer Arrays Data Structures and dynamic Memory Function Pointers quicksort example OUTLINE Review Functions Pointers Function calls Array Pointers Pointer Arrays Data Structures and dynamic Memory Function Pointers quicksort example FUNCTIONS Functions enable grouping of commonly used

More information

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP Operators and Expression Dr Muhamad Zaini Yunos JKBR, FKMP Arithmetic operators Unary operators Relational operators Logical operators Assignment operators Conditional operators Comma operators Operators

More information

Variable and Data Type 2

Variable and Data Type 2 The Islamic University of Gaza Faculty of Engineering Dept. of Computer Engineering Intro. To Computers (LNGG 1003) Lab 3 Variable and Data Type 2 Eng. Ibraheem Lubbad March 2, 2017 Python Lists: Lists

More information

Programming in MATLAB

Programming in MATLAB trevor.spiteri@um.edu.mt http://staff.um.edu.mt/trevor.spiteri Department of Communications and Computer Engineering Faculty of Information and Communication Technology University of Malta 17 February,

More information

Beginning C Programming for Engineers

Beginning C Programming for Engineers Beginning Programming for Engineers R. Lindsay Todd Lecture 2: onditionals, Logic, and Repetition R. Lindsay Todd () Beginning Programming for Engineers Beg 2 1 / 50 Outline Outline 1 Math Operators 2

More information

(created by professor Marina Tanasyuk) FUNCTIONS

(created by professor Marina Tanasyuk) FUNCTIONS FUNCTIONS (created by professor Marina Tanasyuk) In C++, a function is a group of statements that is given a name, and which can be called from some point of the program. The most common syntax to define

More information

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING APS 105 Computer Fundamentals Midterm Examination October 20, 2011 6:15 p.m. 8:00 p.m. (105 minutes) Examiners: J. Anderson, T. Fairgrieve,

More information

Introduction to Python, Cplex and Gurobi

Introduction to Python, Cplex and Gurobi Introduction to Python, Cplex and Gurobi Introduction Python is a widely used, high level programming language designed by Guido van Rossum and released on 1991. Two stable releases: Python 2.7 Python

More information

Solution for Data Structure

Solution for Data Structure Solution for Data Structure May 2016 INDEX Q1 a 2-3 b 4 c. 4-6 d 7 Q2- a 8-12 b 12-14 Q3 a 15-18 b 18-22 Q4- a 22-35 B..N.A Q5 a 36-38 b N.A Q6- a 39-42 b 43 1 www.brainheaters.in Q1) Ans: (a) Define ADT

More information

Downloaded from Chapter 2. Functions

Downloaded from   Chapter 2. Functions Chapter 2 Functions After studying this lesson, students will be able to: Understand and apply the concept of module programming Write functions Identify and invoke appropriate predefined functions Create

More information

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long LESSON 5 ARITHMETIC DATA PROCESSING The arithmetic data types are the fundamental data types of the C language. They are called "arithmetic" because operations such as addition and multiplication can be

More information

Programming in C Quick Start! Biostatistics 615 Lecture 4

Programming in C Quick Start! Biostatistics 615 Lecture 4 Programming in C Quick Start! Biostatistics 615 Lecture 4 Last Lecture Analysis of Algorithms Empirical Analysis Mathematical Analysis Big-Oh notation Today Basics of programming in C Syntax of C programs

More information

Outline. Functions. Functions. Predefined Functions. Example. Example. Predefined functions User-defined functions Actual parameters Formal parameters

Outline. Functions. Functions. Predefined Functions. Example. Example. Predefined functions User-defined functions Actual parameters Formal parameters Outline Functions Predefined functions User-defined functions Actual parameters Formal parameters Value parameters Variable parameters Functions 1 Functions 2 Functions Predefined Functions In C++ there

More information

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University C Programming Notes Dr. Karne Towson University Reference for C http://www.cplusplus.com/reference/ Main Program #include main() printf( Hello ); Comments: /* comment */ //comment 1 Data Types

More information

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED Outline - Function Definitions - Function Prototypes - Data

More information

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES BBM 201 DATA STRUCTURES Lecture 8: Dynamically Allocated Linked Lists 2017-2018 Fall int x; x = 8; int A[4]; An array is stored as one contiguous block of memory. How can we add a fifth element to the

More information

1001ICT Introduction To Programming Lecture Notes

1001ICT Introduction To Programming Lecture Notes 1001ICT Introduction To Programming Lecture Notes School of Information and Communication Technology Griffith University Semester 1, 2015 1 M Environment console M.1 Purpose This environment supports programming

More information

Methods (Deitel chapter 6)

Methods (Deitel chapter 6) Methods (Deitel chapter 6) 1 Plan 2 Introduction Program Modules in Java Math-Class Methods Method Declarations Argument Promotion Java API Packages Random-Number Generation Scope of Declarations Methods

More information

Intrinsic Functions Outline

Intrinsic Functions Outline Intrinsic Functions Outline 1. Intrinsic Functions Outline 2. Functions in Mathematics 3. Functions in Fortran 90 4. A Quick Look at ABS 5. Intrinsic Functions in Fortran 90 6. Math: Domain Range 7. Programming:

More information

Methods (Deitel chapter 6)

Methods (Deitel chapter 6) 1 Plan 2 Methods (Deitel chapter ) Introduction Program Modules in Java Math-Class Methods Method Declarations Argument Promotion Java API Packages Random-Number Generation Scope of Declarations Methods

More information

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT PROGRAMMING WITH MATLAB DR. AHMET AKBULUT OVERVIEW WEEK 1 What is MATLAB? A powerful software tool: Scientific and engineering computations Signal processing Data analysis and visualization Physical system

More information

Arithmetic. 2.2.l Basic Arithmetic Operations. 2.2 Arithmetic 37

Arithmetic. 2.2.l Basic Arithmetic Operations. 2.2 Arithmetic 37 2.2 Arithmetic 37 This is particularly important when programs are written by more than one person. It may be obvious to you that cv stands for can volume and not current velocity, but will it be obvious

More information

Lecture 9 - C Functions

Lecture 9 - C Functions ECET 264 C Programming Language with Applications Lecture 9 C Functions Paul I. Lin Professor of Electrical & Computer Engineering Technology http://www.etcs.ipfw.edu/~lin Lecture 9- Prof. Paul I. Lin

More information

Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers Python Programming, 3/e 1 Objectives n To understand the concept of data types. n To be familiar with the basic

More information

Methods CSC 121 Fall 2016 Howard Rosenthal

Methods CSC 121 Fall 2016 Howard Rosenthal Methods CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand what a method is in Java Understand Java s Math Class and how to use it Learn the syntax of method construction Learn both void methods

More information

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers Functions 1 Function n A program segment that carries out some specific, well-defined task n Example A function to add two numbers A function to find the largest of n numbers n A function will carry out

More information

Fall Semester (081) Dr. El-Sayed El-Alfy Computer Science Department King Fahd University of Petroleum and Minerals

Fall Semester (081) Dr. El-Sayed El-Alfy Computer Science Department King Fahd University of Petroleum and Minerals INTERNET PROTOCOLS AND CLIENT-SERVER PROGRAMMING Client SWE344 request Internet response Fall Semester 2008-2009 (081) Server Module 2.1: C# Programming Essentials (Part 1) Dr. El-Sayed El-Alfy Computer

More information

Data Type Fall 2014 Jinkyu Jeong

Data Type Fall 2014 Jinkyu Jeong Data Type Fall 2014 Jinkyu Jeong (jinkyu@skku.edu) 1 Syntax Rules Recap. keywords break double if sizeof void case else int static... Identifiers not#me scanf 123th printf _id so_am_i gedd007 Constants

More information

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store) Lecture 29 p.1 Faculty of Computer Science, Dalhousie University CSCI 2132 Software Development Lecture 29: Linked Lists 19-Nov-2018 Location: Chemistry 125 Time: 12:35 13:25 Instructor: Vlado Keselj Previous

More information

Quick MATLAB Syntax Guide

Quick MATLAB Syntax Guide Quick MATLAB Syntax Guide Some useful things, not everything if-statement Structure: if (a = = = ~=

More information

The Number object. to set specific number types (like integer, short, In JavaScript all numbers are 64bit floating point

The Number object. to set specific number types (like integer, short, In JavaScript all numbers are 64bit floating point Internet t Software Technologies JavaScript part three IMCNE A.A. 2008/09 Gabriele Cecchetti The Number object The JavaScript Number object does not allow you to set specific number types (like integer,

More information

ALGORITHM 2-1 Solution for Exercise 4

ALGORITHM 2-1 Solution for Exercise 4 Chapter 2 Recursion Exercises 1. a. 3 * 4 = 12 b. (2 * (2 * fun1(0) + 7) + 7) = (2 * (2 * (3 * 0) + 7) + 7) = 21 c. (2 * (2 * fun1(2) + 7) + 7) = (2 * (2 * (3 * 2) + 7) + 7) = 45 2. a. 3 b. (fun2(2, 6)

More information

Product Price Formula extension for Magento2. User Guide

Product Price Formula extension for Magento2. User Guide Product Price Formula extension for Magento2 User Guide version 1.0 Page 1 Contents 1. Introduction... 3 2. Installation... 3 2.1. System Requirements... 3 2.2. Installation...... 3 2.3. License... 3 3.

More information