By Senem Kumova Metin 1 DATA TYPES. by Senem Kumova Metin 2 DATA TYPE? …… x; // DECLARATION OF VARIABLE X printf(“Do you want to go on? \n”) printf(“Please.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
Addition Facts
Year 6 mental test 5 second questions
ZMQS ZMQS
Chapter 7: Arrays In this chapter, you will learn about
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.
LIST PROCESSING.
ABC Technology Project
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
Progam.-(6)* Write a program to Display series of Leaner, Even and odd using by LOOP command and Direct Offset address. Design by : sir Masood.
1 Chapter Eleven Arrays. 2 A Motivating Example main( ) { int n0, n1, n2, n3, n4; scanf(“%d”, &n0); scanf(“%d”, &n1); scanf(“%d”, &n2); scanf(“%d”, &n3);
Squares and Square Root WALK. Solve each problem REVIEW:
© 2012 National Heart Foundation of Australia. Slide 2.
1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.
While Loop Lesson CS1313 Spring while Loop Outline 1.while Loop Outline 2.while Loop Example #1 3.while Loop Example #2 4.while Loop Example #3.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
C Language.
1 of 31 Images from Africa. 2 of 31 My little Haitian friend Antoine (1985)
Addition 1’s to 20.
CS 240 Computer Programming 1
25 seconds left…...
Spring Semester 2013 Lecture 5
U1A L1 Examples FACTORING REVIEW EXAMPLES.
Week 1.
We will resume in: 25 Minutes.
Pointers and Arrays Chapter 12
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Introduction to Recursion and Recursive Algorithms
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 13 – Salary Survey Application: Introducing.
Senem KUMOVA METİN CS FALL 1 POINTERS && ARRAYS CHAPTER 6.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
CS115 Introduction to Programming
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Senem Kumova Metin // CS115 // FUNCTIONS continues CHAPTER 5.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Senem Kumova Metin // CS115 // FUNCTIONS CHAPTER 5.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Functions.
Lesson #6 Modular Programming and Functions.
Fundamental Programming
Functions Extra Examples.
CPS125.
Presentation transcript:

by Senem Kumova Metin 1 DATA TYPES

by Senem Kumova Metin 2 DATA TYPE? …… x; // DECLARATION OF VARIABLE X printf(“Do you want to go on? \n”) printf(“Please type Y for YES / N for NO”); scanf(“…….”, &x);

by Senem Kumova Metin 3 WHAT IS THE APPROPRIATE TYPE ? ….. x; printf(“Please make your choise”); printf(“1 Addition\n”); printf(“2 Subtraction\n”); printf(“3 Division\n”); scanf(“…..”, &x); …..

by Senem Kumova Metin 4 WHAT IS THE APPROPRIATE TYPE ? ….. x; …… y; …… result; printf(“Please give two numbers to divide”); scanf(“…..”, &x); scanf(“…..”,&y); result= ……. x/y; printf(“Result of division is …”, result);

by Senem Kumova Metin 5 WHAT IS THE APPROPRIATE TYPE ? …… …….. printf(“Please write your name and surname”); scanf(“…..”, name); scanf(“…..”, surname); printf(“your name is …..”, name); printf(“your surname is …..”,surname);

by Senem Kumova Metin 6 FLOW OF CONTROL IF STATEMENT SWITCH STATEMENT LOOPS

by Senem Kumova Metin 7 FILL IN THE BLANKS FILL IN THE BLANKS int x; int y; printf(“Please enter 2 numbers to compare”); scanf(“%d”,&x); scanf(“%d”,&y); ……. ……

by Senem Kumova Metin 8 FILL IN THE BLANKS FILL IN THE BLANKS int x; printf(“Please make your choise”); printf(“1 Addition\n”); printf(“2 Subtraction\n”); printf(“3 Division\n”); scanf(“ %d”, &x); …….

by Senem Kumova Metin 9 FILL IN THE BLANKS FILL IN THE BLANKS int x; int result =0; ……. printf(“Please enter 10 numbers to sum”); ………. printf(“Result is %d”,result);

by Senem Kumova Metin 10 FUNCTIONS REVIEW

by Senem Kumova Metin 11 return_type function_name (input_parameter_list) { declarations/* body of the function */ statements return …… } return_type : * Zero or one data type ( int, char, float etc.) * “void” keyword can be used if there is no return value * Write the type of the return variable… input_parameter_list : * Zero or more variables (as int x, float c etc.) * If more than one input parameter exist, then put commas between them * If there is no parameter, “void” keyword can be usedFUNCTIONS

by Senem Kumova Metin 12 FUNCTION :EXAMPLE1 double twice (double x) /*header=declaration*/ { /* body starts here */ return x*2;} /* double twice (double x) output input parameter parameters this function is called “twice()”, it will return a data of type double, it gets an input data of type double */

by Senem Kumova Metin 13 FUNCTION :EXAMPLE2 If there is no return value, use “void” keyword as an output parameter If there is no input parameter, use “void” as an input parameter or do not write anything void menu(void) { printf(“ A\n ”); printf(“ B\n ”); printf(“ C\n ”); } void menu() { printf(“ A\n ”); printf(“ B\n ”); printf(“ C\n ”); }

by Senem Kumova Metin 14 FUNCTION :EXAMPLE3 int add1( int x, int y) {return x+y;} int add2( int x, int y) {int result; // local variables can be // declared in functions result =x+y; return result;}

by Senem Kumova Metin 15 FUNCTION :EXAMPLE4 int sum( int a, int b) { /* If-else, for, while, do while etc. statements can be used in functions */ int i; int s=0; for (i=a;i<b;i++) s=s+i; return s; }

by Senem Kumova Metin 16 Return statement The return statement may or may not include an expression The return statement terminates the execution of the function, and closes memory space opened for all local variables (kills all local variables of function) EXAMPLE: float f(int a, char b) { int i; ….. return i; /* value of i will be converted to float */ /* return (i); */ /* return value can also be written in braces */ }

by Senem Kumova Metin 17 return statement : EXAMPLES char func1( int a, int b) { return a+b; } double my_sqrt( int a ) { return sqrt(a); } int my_max1( int y, int z) { return y>z? y:z ; } int my_max2 (int y, int z, int t, int x) { int a, b; a= my_max1(y,z); b=my_max1(t,x); return my_max1(a,b);} Functions can call other functions

by Senem Kumova Metin 18 FUNCTION PROTOTYPES Each function has to be declared before it is used There are two ways to achieve this rule.. –Define the functions before used –Write the function prototype of the function before used ( Just declaration not definition) Function prototype includes header information : –Return type –Function name –Input parameter list

by Senem Kumova Metin 19 FUNCTION PROTOTYPES : EXAMPLES char func1( int a, int b, int c); double func2( char a ); int func3( int y[], int z); int funct4(int y[], char z[], int t, int x);

by Senem Kumova Metin 20 Function Definition Order EXAMPLE 1: #include int func_2(); int func_1(); int func_3(); void main(void) { func_1(); func_2(); func_3(); } int func_2(){…}; int func_1(){…}; int func_3(){…}; EXAMPLE 2: #include int func_2(); int func_1() { func_3(); } int func_3(); void main(void) { func_2(); func_1(); } int func_2(){…}; int func_3(){…};

by Senem Kumova Metin 21 CALL BY VALUE If a variable is passed to a function, the stored value in the calling environment will not be changed!!! void my_increment (int n) { n=n+1; } void main(void) {int n=9; printf(“%d\n”,n); my_increment(n); // call function my_increment() // by value printf(“%d\n”,n); }

by Senem Kumova Metin 22 CALL BY REFERENCE If a variable’s address is passed to a function, the stored value in the calling environment may be changed!!! void my_increment (int * n) { *n=*n+1; } void main(void) {int n=9; printf(“%d\n”,n); my_increment(&n); // call function my_increment() // by value printf(“%d\n”,n); }

by Senem Kumova Metin 23 RECURSION An algorithmic technique where a function, in order to accomplish a task, calls itself with some part of the task If a function calls itself, then it is called recursive !!! There are two parts to the definition of a recursive solution: –Base case: Simple, non-recursive solution –General method: Uses simpler version of the problem to solve harder problem

by Senem Kumova Metin 24 RECURSION EXAMPLE 1 // ITERATION int sum(int N) { int i; int SUM =0; for (i=1; i<=N; i++) SUM=SUM+i; return SUM; } // RECURSION int sum(int N) { if(N==1) return 1; else return(N+sum(N-1));} /* BASE CASE : N==1  1 METHOD : sum(N) = N + sum(N-1) if BASE CASE else METHOD */

by Senem Kumova Metin 25 RECURSION EXAMPLE 2 BASE CASE  N=1 return 1 METHOD  sum(N)= sum(N-1)+1/N double sum(int N) {if(N==1) return 1; else return (sum(N-1)+1/N); }

by Senem Kumova Metin 26 RECURSION vs ITERATION Iteration in computing is the repetition of a process within a computer program Recursion is a method of defining functions in which the function being defined is applied within its own definition

by Senem Kumova Metin 27 EXAMPLE: RECURSION & ITERATION Write a function that prints numbers from 1 to N using iteration Convert your function to work in a recursive way void main( ) { print(5); } //

by Senem Kumova Metin 28 /* ITERATION */ void print( int N) { int i; for (i=1;i<=N;i++) printf(“%d\t”,i); } /* RECURSION*/ void print( int N) { if(N==1) printf(“1\t”); else {print(N-1); printf(“%d\t”,N); }

by Senem Kumova Metin 29 FACTORIAL EXAMPLE: RECURSION & ITERATION /*iterative version n! = 1*2*3* … * n = n*(n-1)*(n-2) * …*3*2*1 */ int factorial (int n) { int product = 1; for(;n>1;--n) product = product*n; return product;} /* recursive version n! = n * (n-1)!*/ int factorial(int n) { if(n<=1) return 1; else return ( n*factorial(n-1)); } /* BASE CASE : n<=1  1 METHOD : n!=n*(n-1)! */ factorial(3) = 3 * factorial(2) = 3 * ( 2 * factorial(1) ) = 3 * ( 2 * ( 1 * factorial(0) )) = 3 * ( 2 * ( 1 * 1 ) )) = 6