1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

26
1 دا ام خ ن ب ی س ی و ن ه م ا رن ب ان ن زC (21814 ( Lecture 8 Chapters 8 & 9

Transcript of 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

Page 1: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

1

خدا بنام

نویسی برنامه )C (21814زبان

Lecture 8

Chapters 8 & 9

Page 2: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

2

نویسی برنامه هشتم - )C (21814زبان فصل

: فصلهشتم

اعداد از استفاده مورد در نکاتی

: اینفصل اساسی نکاتمحاسباتی • مسایل از تعدادی با برخورد نحوه

عدد دو مقایسه نحوه نظیریک • به اختصاصیافته مقدار شدن خارج نتایج

قبول قابل محدوده از متغیر

Page 3: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

3

نویسی برنامه هشتم - )C (21814زبان فصل

: عدد دو میان مقایسه

مشکل • صحیح، عدد دو حالتمقایسه در. ندارد وجود چندانی

آنها • نوع که حالتی در واقعی عدد دو مقایسه. نیست آسان نباشد، یکسان

Page 4: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

4

نویسی برنامه هشتم - )C (21814زبان فصل

:مثال

#include <stdio.h>

float w = 4.4;

double x = 4.4;

void main (void){

printf (" Is x== (double)w? %i \n",(x == (double)w));

printf (" Is (float)x == w? %i \n",((float)x == w));

}

Page 5: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

5

نویسی برنامه نهم - )C (21814زبان فصل

كوچك ) • اجزاي به بايستي functons)برنامه. شود شكسته

مي چيد • زير بصورت برنامه يك عمومي مانباشد:

• include commands for header files

• Constant definitions and type declarations

• Prototypes (function declarations( and one line functions

• main( (, which contains function calls

• Function definitions

Page 6: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

6

نویسی برنامه نهم - )C (21814زبان فصل

جز • به توابع ساير مورد اين ) )mainدر اگراز قبل توان ) )mainتوابع مي تعريفشوند،

declaration. حذفنمود آنها براي را

كه • دهند مي ترجيح نويسان برنامه معموالmain( (. شود نوشته تابع اولين عنوان به

Page 7: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

7

نویسی برنامه )C (21814زبان

توابع ) مورد در ):Functionsمطالبی

گرفته بحثقرار مورد مختلفی توابع تاکنوناند:

•int main(void(•printf(arg1, arg2, …(•scanf(arg1, arg2, …(•sqrt(arg1(•rand((•time(NULL(

Page 8: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

8

نویسی برنامه )C (21814زبان

توابع ) مورد در ):Functionsمطالبی

به • برنامه یک زدن صدا : فرآیند است زیر شرح

از – دیگری جای به برنامه اجرای انتقالبرنامه

برآوردنسرویسهایی – یا محاسبات انجامبرنامه برای دیگر

و – شده زده صدا تابع که مکانی به بازگشت. کار ادامه

Page 9: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

9

نویسی برنامه )C (21814زبان

: توابع از استفاده روشهای

1. x = function1(arg1, arg2);takes arguments, returns a value.

2. function2(arg1,arg2);takes arguments, returns nothing.

3. function3();takes no arguments, returns nothing.

Last two examples sometimes called subroutine (when nothing returned)

Page 10: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

10

نویسی برنامه )C (21814زبان

Three parts to writing functions

1.Declaration: tell the compiler about the function (function prototype).

2.Definition: write the code the function executes.

3.Call: Call (use) the function to do some work.

Page 11: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

11

نویسی برنامه )C (21814زبان

Declaration - Function Prototype

type function(parameter_list);for example:

double cube(double x); // x is optional

output valueis type double(return value(

formal parameter name(optional(

input valueis type (double(

Page 12: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

12

نویسی برنامه )C (21814زبان

(Definition) تعریف

double cube(double val) {

return val*val*val;

}

formal parameter -- holds input value from call. Implicitly declared and initialized variable!!

this function returnsa value of type double

value to return

Page 13: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

13

نویسی برنامه ) C (21814زبان

Call (Use)

int main(void) {

double x = 2.5, y;

y = cube(x);

printf("%f %f", x, y);

return 0;

}

Actual parameter -- the value passed to the function

Page 14: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

14

نویسی برنامه )C (21814زبان

How Do Functions Work

#include <stdio.h>double cube(double val); // function prototype

(declaration)

int main(void) {double x=2.5, y; // 1y = cube(x); // 2 function callprintf("%f %f", x, y); // 3fflush(stdout); // 4 forces output to console screenreturn 0; // 5

}

double cube(double val){ // function definitionreturn val*val*val; // 6

}Execution order is:1 2 6 2 3 4 5

input to function

output from function

Page 15: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

15

Program Structure – Big Picture.

#include <stdio.h>

int fcn1( int (;int fcn2( int (;

int main( void ( { // this is 'main' definition// body of 'main' includes calls to other fcns

}

int fcn1( int inVal( { // fcn1 definition// body of fcn1

}

int fcn2( int inVal( { // fcn1 definition// body of fcn2

}

Prototypes

main function

fcn1 function

fcn2 function

Preprocessor

etc.

Page 16: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

16

Program Style 1(this course(

#include <stdio.h> // libraries

int square(int a); // prototypes int cube(int a);

int main(void) { // main function int i = 1;

while(i < 10) {printf("%5d %5d %5d\n", i, square(i), cube(i)); // function calls++i;

}return 0;

}

int square(int a) { // square functionreturn a * a;

}

int cube(int a) { // cube functionreturn a * a * a;

}

نویسی برنامه )C (21814زبان

Page 17: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

17

Program Style 2(no prototypes(

#include <stdio.h> // libraries

int square(int a) { // square functionreturn a * a;

}

int cube(int a) { // cube function return a * a * a;

}

int main(void) { // main function int i = 1;

while(i < 10) { printf("%5d %5d %5d\n", i, square(i), cube(i)); ++i;

}return 0;

}

نویسی برنامه )C (21814زبان

Page 18: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

18

Function Call Details

int main(void) {double y, x = 2.5;y =cube(x);return 0;

}

double cube(double val) {return val*val*val;

}

1( store value of x in a temporary location, jump to function cube()

2( assign value of x to val

3( calculate cube of val and store in temporary place and jump back to calling line.

4( retrieve the cube value and assign to y.

Page 19: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

19

نویسی برنامه )C (21814زبان

با تابع یک زدن صفحه) Array Argument صدا383( کتاب

یک با تابع یک زدن آرایه argumentصدا بصورت: میشود انجام زیر بصورت

براکت – بدون آرایه نام تنها تابع، زدن صدا هنگام. میشود ذکر آن طول یا و اعضا از یکی یا ها،

نام & – از قبل اپراتور که نیست نیازی همچنین. شود ذکر آرایه

هنگان – بدون declareدر را ها براکت باید نمودن. نمایشداد آرایه طول ذکر

ندارد – مانعی شود ذکر آرایه طول اگر حالت این در. گیرد نمی نظر در را آن کامپایلر ولی

با – هایی آرایه با تابع از توان حالتمی این در. نمود استفاده متفاوت طول

Page 20: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

20

نویسی برنامه )C (21814زبان

با تابع یک زدن ) Array Argument صدا ادامه)مثال:

void Fname (double Arrayname[], int n(;

void getdata (double x[], int n(;

Page 21: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

21

نویسی برنامه )C (21814زبان

Page 22: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

22

نویسی برنامه )C (21814زبان

Page 23: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

23

نویسی برنامه )C (21814زبان

;

Page 24: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

24

نویسی برنامه )C (21814زبان

Recursive Function (Type I(: #include <stdio.h>

int a,N=1;

int Factoriel(int x);

void main (void){

printf(" Enter a number:");

scanf("%i",&a);

Factoriel(a);

printf("\nThe factoriel of %i is equal to %i.\n\n",a,N);

}

int Factoriel(int a){

N = N*a;

a=a-1;

if(a>1){

Factoriel(a);

}

return 0;

}

Page 25: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

25

نویسی برنامه )C (21814زبان

Recursive Function (Type II(: #include <stdio.h>

int a,N=1,W=1;

int Factoriel(int x);

void main (void){

printf(" Enter a number:");

scanf("%i",&a);

N= Factoriel(a);

printf("\nThe factoriel of %i is equal to %i.\n\n",a,N);

}

int Factoriel(int a){

W = W*a;

a=a-1;

if(a>1){

Factoriel(a);

}

return W;

}

Page 26: 1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.

26

نویسی برنامه )C (21814زبان

:5تمرین

. بگیرید نظر در را تاسمنصف دو ریختن مثالتصادفی متغییر دو Xاگر نتایج مجموع بیانگر

میانگین، محاسبه است مطلوب باشد، تاستصادفی واریانسمتغیر و معیار، . Xانحراف

کدامند؟ مد و میانه مثال این مورد در

برای آزمایشرا 100این 10000و 1000، ،10فایل یک در را نتایج و دهید انجام مرتبه

. کنید ذخیره خروجی