الف) تابع y = f(x) = X >= 0x -2 < x < 0 x3x3 X <= -2 x2x2 را بصورت يك تابع زبان C بنويسيد. ب) برنامه اي بنويسيد كه مقدار اعشاري x را از ورودي دريافت كند.

Slides:



Advertisements
Similar presentations
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Advertisements

Recursion Prog #include <stdio.h> #include<conio.h> main()
Chapter Five Functions
Sort the given string, without using string handling functions.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
C workshop #3 flow control / strings.
TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions.
0 Arrays (1/2) #include /* count digits, white space, others */ main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i
Review: midterm #9 #include void main(void) { int c; c = getchar(); if(c>=48){ if(c
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
1 Lecture04: Basic Types & Function Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Outline Symbolic Constants Character Input and Output if … else switch…case.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Lecture 5 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
1 Lecture04: Function Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Introduction to Computer Algorithmics and Programming Ceng 113 Program Control Statements.
به نام خدا برنامه سازی سمت سرور (php)
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Functions (1)
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
Introduction to Programming Lecture 4: Calculations.
Introduction to Programming Lecture 7: Repeating Statements.
آرايه Array آرايه مجموعه اى از متغيرهاى از يك نوع است كه با يك نام مشترك تحت استفاده قرار مى گيرند. يك عنصر بخصوص در يك آرايه با يك index (انديس ) مورد.
Administrative things
Introduction to Programming Lecture 6: Making Decisions.
Data Types Modifiers Base data type Size Sign signed int 2 B unsigned float 4 B short double 8 B long char 1 B.
اسامي شناسه ها (Identifier names) اسامي متغيرها ، توابع ، برچسب ها (labels) وبقيه اشياء تعريف شده توسط كاربر در C ، شناسه ( identifier ) ناميده مي شود.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
شرط و تصميم اصول كامپيوتر 1. 2 الگوريتم اقليدس E1: [find remainder] Divide m by n and let r be the remainder. Clearly, 0
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Order.
Sudeshna Sarkar, IIT Kharagpur 1 I/O in C + Misc Lecture –
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Introduction to Programming Lecture 5: Interaction.
Introduction to Programming Lecture 8: Functions.
Introduction to Programming Lecture 2: Algorithm Design.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
سیستمهای کنترل خطی پاییز 1389 بسم ا... الرحمن الرحيم دکتر حسين بلندي- دکتر سید مجید اسما عیل زاده.
CSE 110: Programming Language I Afroza Sultana UB 1230.
Tutorial #4 Summer 2005.
EKT120: Computer Programming
Chapter 6 - Functions modular programming general function format
Mathematical Functions
TMF1414 Introduction to Programming
Kontrola toka izvršenja
C Short Overview Lembit Jürimägi.
PGT 106: Computer Programming
Week 5 – Functions (1) EKT120: Computer Programming.
I/O in C + Misc Lecture Sudeshna Sarkar, IIT Kharagpur.
FUNCTIONS EXAMPLES.
EKT120: Computer Programming
Formatted and Unformatted Input/Output Functions
آرايه ها.
آشنايي با برنامه نويسي به زبان C++
Introduction to Programming
مباني كامپيوتر و برنامه سازي Basics of Computer and Programming
نمايش اعداد در کامپيوتر چهار عمل اصلي
بنام خدا زبان برنامه نویسی C (21814( Lecture 4 Chapter 5
שיעור רביעי: פונקציות, מבוא לרקורסיה
بنام خدا زبان برنامه نویسی C (21814( Lecture 3 Chapter 4
مثال : فلوچارتي رسم كنيد كه دو عدد از ورودي دريافت كرده بزرگترين عدد
Dr Tripty Singh Tutorial for Fuctions
Relational, Logical, and Equality Operators
Introduction to Problem Solving and Programming
FUNCTION ||.
Functions in C Math Library Functions Functions Function Definitions
Control Structure គោលបំណងនៃមេរៀន អ្វីជា Control structure ?
Presentation transcript:

الف) تابع y = f(x) = X >= 0x -2 < x < 0 x3x3 X <= -2 x2x2 را بصورت يك تابع زبان C بنويسيد. ب) برنامه اي بنويسيد كه مقدار اعشاري x را از ورودي دريافت كند و تابع f را صدا بزند سپس مقدار f(x) ( در برنامه اصلي ) چاپ شود نمونه مسائل امتحان :

double f(double x) { if( x >= 0 ) return x; else if(-2 < x < 0 ) /* if(-2 < x < 0 ) */ return pow( x, 3.0 ); else /* if( x <= -2 ) */ return pow( x, 2.0 ); } جواب الف)

#include /* mathematics functions */ double f(double x); main() { double x; scanf("%lf", &x); printf("f(%6.2f) = %6.2f ", x, f(x) ); } double f(double x) { if( x >= 0 ) return x; else if(-2 < x < 0 ) /* if(-2 < x < 0 ) */ return pow( x, 3.0 ); else /* if( x <= -2 ) */ return pow( x, 2.0 ); } جواب ب)

Switch دستور switch ( expression ) { case const-expr : statements default : statements }

#include main( ) { int c, i, nwhite, nother, ndigit[10] ; nwhite = nother = 0 ; for ( i = 0 ; i < 10 ; i++) ndigit[i] = 0 ;

while ( ( c = getchar() ) != EOF ) { switch ( c ) { case ‘0’ : case ‘1’ : case ‘2’ : case ‘3’ : case ‘4’ : case ‘5’ : case ‘6’ : case ‘7’ : case ‘8’ : case ‘9’ : ndigit[ c – ‘0’ ]++ ; break ; case ‘ ‘ : case ‘\n’ : case ‘\t’ : nwhite++ ; break ; default : nother++ ; break ; }

و علت استفاده از آن Function prototypeدرباره درباره return 0 ; در تابع main( )

توابع رياضي:Prototype is in math.h double acos( double arg) double asin( double arg) double atan( double arg) double atan2( double y, double x) زاويه قطبي نقطه (x,y) double sin( double arg) double cos( double arg) double tan( double arg) double log( double num) natural logarithm for num double log10( double num) the base 10 logarithm for num double pow( double x, double y) double floor( double num) largest integer not greater than num double fabs( double num) absolute value of num double exp( double arg) e arg double sqrt( double num) the square root of num

Prototype in stdlib.h int abs( int num ) absolute value long labs(long num) absolute value

#include void itoa(int, char* ); void reverse( char* ); main() { int n ; char st[20] ; scanf(“%d”, n); itoa( n, st ); for( sum = 0, i = 0 ; i < strlen(st) ; i++ ) if ( ‘0’ <= st[i] <= ‘9’ ) sum = sum + ( st[ i ] – ‘0’ ) ; printf(“%d”, sum ); } itoa.cنام برنامه

sqrt( ( pow( x, 2.0 ) + pow( y, 2.0 ) ) / ( ( 5 – x * y) / ( exp( 3.0 ) + fabs( x – y ) ) ) )

main() { int n, sum, i = 0 ; char st[20] ; scanf(“%d”, n); itoa( n, st ); if ( n < 0 ) i++ ; for( sum = 0 ; i < strlen(st) ; i++ ) { putchar('\t') ; putchar( st[i] ); sum = sum + ( st[ i ] - '0' ) ; printf("\nsum=%d\n", sum); } printf("%d", sum ); }

/* reverse: reverse string s in place */ void reverse(char s[]) { int c, i, j; for (i = 0, j = strlen(s)-1; i < j; i++, j--) { c = s[i]; s[i] = s[j]; s[j] = c; }

void itoa(int n, char s[]) { int i, sign; if ((sign = n) < 0) /* record sign */ n = -n; /* make n positive */ i = 0; do { /* generate digits in reverse order */ s[i++] = n % 10 + '0'; /* get next digit */ } while ((n /= 10) > 0); /* delete it */ if (sign < 0) s[i++] = '-'; s[i] = '\0'; reverse(s); }

#include

main() { int x, i, neven = 0, nodd = 0 ; clrscr(); for ( i = 0 ; i < 10 ; i++ ) { printf( “ Enter a number: \t “ ); scanf( “%d”, &x ) ; if ( (x % 2) == 0 ) ++neven ; else ++nodd ; }

printf(“ number of evens = %d\n”, neven); printf(“ number of odds = %d\n”, nodd); }

الف) تابعي بنويسيد كه عدد صحيح و غير منفي n را از ورودي دريافت نموده و فاكتوريل آنرا محاسبه نمايد. 0! = 1! = 1 n! = n * (n-1) * (n-2) * … * 2 * 1 n>=2

int fact( int n) { int i, p ; if( n < 0 ) return -1; else if ( n == 0 || n == 1 ) return 1; else { for( i = 1, p = 1 ; i <= n ; i++) p = p * i ; return p; } حل:

ب) برنامه اي بنويسيد كه يك عدد صحيح و نا منفي را از كاربر دريافت نموده و با استفاده از فراخواني تابع قسمت الف فاكتوريل آنرا محاسبه نموده و در خروجي چاپ نمايد. #include int fact( int ) ; main() { int t ; printf(“Please enter a non-negative integer number :\t”); scanf( “%d”, &t) ; printf( “%d”, fact( t ) ) ; }

int fact( int n) { int i, p ; if( n < 0 ) return -1; else if ( n == 0 || n == 1 ) return 1; else { for( i = 1, p = 1 ; i <= n ; i++) p = p * i ; return p; }

Recursive بازگشتي يعني يك تابع بطور مستقيم يا غير مستقيم خود را صدا كند. f f, g double f ( double x) { …. return f( x – 1 ) ; }

double f( double x ) { ….. return g( x – 1 ) ; } double g( double x ) { ….. return f( x – 1 ) ; }

الف) تابعي بنويسيد به نام fact كه عدد صحيح و غير منفي n را از ورودي دريافت نموده و فاكتوريل آنرا محاسبه نمايد.) به طور بازگشتي) 0! = 1! = 1 n! = n * (n-1) * (n-2) * … * 2 * 1 n>=2 حل: fact ( n ) = 1 n = 0 يا 1 n * fact ( n-1 ) n >= 2

int fact( int n ) { if ( n == 0 || n ==1 ) return 1; else if( n >= 2 ) return n * fact( n – 1 ) ; else /* n is negative */ return -1 ; }

تابعي بنويسيد به نام fibo كه جمله n ام سري فيبوناچي را محاسبه نمايد. سري فيبوناچي به اينصورت تعريف مي شود : حل: int fibo( int n) { if ( n == 1 || n==2 ) return 1 ; else if ( n >= 3 ) return fibo( n – 1 ) + fibo( n – 2 ) ; else /* n is negative */ return -1 ; } fibo( 1 ) = fibo ( 2 ) = 1 fibo (n) = fibo( n – 1) + fibo( n – 2 )

A= fabs( ( 2 * exp( x + y ) / sqrt( x) - ( pow(x, 2) + pow(y, 2 )) / ( x * log(y) ) ) / ( exp( sqrt( x + y ) ) / log ( sqrt ( pow( x, 2 ) + pow(y, 2) ) ) + pow(y, 3 ) / ( x – 10 ) ) )

A= fabs( ( 2* exp( x + y ) / sqrt( x ) - ( pow ( x, 2 ) + pow(y,2) ) / ( x * log(y) ) ) / ( exp( sqrt( x + y ) ) / log( sqrt ( pow ( x, 2 ) + pow(y,2) ) ) + pow( y, 3 ) / ( x – 10 ) ) )