fundamentals of programming - kntu...fundamentals of programming lecture 6 decision making. decision...

23
Fundamentals of Programming Lecture 6 Decision making

Upload: others

Post on 25-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

Fundamentals of ProgrammingLecture 6

Decision making

Page 2: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

decision making تصمیم گیری

#include <stdio.h>

int main() {

int a,b;

scanf("%d %d", &a, &b);

if (a > b) { printf("a is bigger than b\n"); } return 0;}

prog11.c

Page 3: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

decision making تصمیم گیری

#include <stdio.h>

int main() {

int a,b;

scanf("%d %d", &a, &b);

if (a > b) { printf("a is bigger than b\n"); } return 0;}

prog11.c

Page 4: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

decision making تصمیم گیری

#include <stdio.h>

int main() {

int a,b;

scanf("%d %d", &a, &b);

if (a > b) { printf("a is bigger than b\n"); } return 0;}

prog11.c

Page 5: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

decision making تصمیم گیری

#include <stdio.h>

int main() { int a,b;

scanf("%d %d", &a, &b);

if (a > b) { printf("a is bigger than b\n"); } else { printf("a is not bigger than b\n"); }

return 0;}

prog12.c

Page 6: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

decision making تصمیم گیری

#include <stdio.h>

int main() { int a,b;

scanf("%d %d", &a, &b);

if (a > b) { printf("a is bigger than b\n"); } else { printf("a is not bigger than b\n"); }

return 0;}

prog12.c

Page 7: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

decision making تصمیم گیری

#include <stdio.h>int main() { int a,b;

scanf("%d %d", &a, &b);

if (a > b) { printf("a is bigger than b\n"); } else if (a < b) { printf("a is smaller than b\n"); } else { printf("a equals b\n"); }

return 0;}

prog13.c

Page 8: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

Comparing variables مقایسھ متغیرھا

Page 9: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

Practice

برنامھ ای بنویسید کھ دو عدد را بگیرد و بیشینھ آن دو را چاپ کند.

Write a C program reading two integers and printing their maximum.

Page 10: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

Practice: maximum

#include <stdio.h>

int main() { int a,b; scanf("%d %d", &a, &b);

if (a > b) { printf("%d\n",a); } else { printf("%d\n",b); }

return 0;}

prog14.c

Page 11: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

Practice: maximum

#include <stdio.h>

int main() { int a,b; scanf("%d %d", &a, &b);

if (a > b) { printf("%d\n",a); } else { printf("%d\n",b); }

return 0;}

prog14.c

Page 12: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

Practice: maximum

#include <stdio.h>

int main() { int a,b,max;

scanf("%d %d", &a, &b);

max = a; if (b > a) { max = b; }

printf("%d\n",max); return 0;}

prog15.c

Page 13: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

Practice: absolute value

برنامھ ای بنویسید کھ یک عدد صحیح را بگیرد و قدر مطلق آن را چاپ کند.

Write a C program reading an integers and printing its absolute value.

Page 14: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

Practice: absolute value#include <stdio.h>int main() { int a;

scanf("%d", &a);

if (a > 0) { printf("%d\n",a); } else { printf("%d\n",-a); } return 0;}

prog16.c

Page 15: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

Practice: absolute value

#include <stdio.h>int main() { int a;

scanf("%d", &a);

if (a > 0) { printf("%d\n",a); } else { printf("%d\n",-a); } return 0;}

prog16.c

Page 16: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

Practice: absolute value

#include <stdio.h>int main() {

int a;

scanf("%d", &a); if (a < 0) { a = -a; }

printf("%d\n",a); return 0;}

prog17.c

Page 17: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

Practice: piecewise functions

http://math2.uncc.edu/~bjwichno/spring2010/math1121/Lecture_Notes/unit_1/Lectures/lec_piecewise_rule.htm

Write a program that reads x and prints f(x).

Page 18: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

Practice: piecewise functions

http://math2.uncc.edu/~bjwichno/spring2010/math1121/Lecture_Notes/unit_1/Lectures/lec_piecewise_rule.htm

Write a program that reads x and prints f(x).

#include <stdio.h>int main() { int x,y; scanf("%d", &x);

if (x < 0) { y = x*x; } else { if (x <= 3) { y = 2; } else { y = 4 - x; } } printf("%d\n",y); return 0;}

prog18.c

Page 19: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

Practice: piecewise functions

http://math2.uncc.edu/~bjwichno/spring2010/math1121/Lecture_Notes/unit_1/Lectures/lec_piecewise_rule.htm

Write a program that reads x and prints f(x).

#include <stdio.h>int main() { int x,y; scanf("%d", &x);

if (x < 0) { y = x*x; } else if (x <= 3) { y = 2; } else { y = 4 - x; }

printf("%d\n",y); return 0;}

prog19.c

Page 20: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

Practice: piecewise functions

http://math2.uncc.edu/~bjwichno/spring2010/math1121/Lecture_Notes/unit_1/Lectures/lec_piecewise_rule.htm

Write a program that reads x and prints f(x).

#include <stdio.h>int main() {

float x,y; scanf("%f", &x); if (x < 0) { y = x*x; } else if (x <= 3) { y = 2; } else { y = 4 - x; }

printf("%f\n",y); return 0;}

prog20.c

Page 21: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

Practice: piecewise functions

http://math2.uncc.edu/~bjwichno/spring2010/math1121/Lecture_Notes/unit_1/Lectures/lec_piecewise_rule.htm

Write a program that reads x and prints f(x).

#include <stdio.h>int main() {

float x,y; scanf("%f", &x); if (x < 0) { y = x*x; } else if (x <= 3) { y = 2; } else { y = 4 - x; }

printf("%f\n",y); return 0;}

prog20.c

Page 22: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

Practice: piecewise functions

http://math2.uncc.edu/~bjwichno/spring2010/math1121/Lecture_Notes/unit_1/Lectures/lec_piecewise_rule.htm

Write a program that reads x and prints f(x).

#include <stdio.h>int main() {

float x,y; scanf("%f", &x); if (x < 0) { y = x*x; } else if (x <= 3) { y = 2; } else { y = 4 - x; }

printf("%f\n",y); return 0;}

prog20.c

Page 23: Fundamentals of Programming - KNTU...Fundamentals of Programming Lecture 6 Decision making. decision making یرﯾﮔ مﯾﻣﺻﺗ

Practice: piecewise functions

http://math2.uncc.edu/~bjwichno/spring2010/math1121/Lecture_Notes/unit_1/Lectures/lec_piecewise_rule.htm

Write a program that reads x and prints f(x).

#include <stdio.h>int main() {

double x,y; scanf("%lf", &x); if (x < 0) { y = x*x; } else if (x <= 3) { y = 2; } else { y = 4 - x; }

printf("%f\n",y); return 0;}

prog20.c