Top Banner
Tutorial ICS431 – Introduction to C
28

Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Jan 02, 2016

Download

Documents

Polly Lyons
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Tutorial

ICS431 – Introduction to C

Page 2: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Example ( 1 )

int main(){

float radius, area;

printf(“Enter the radius : “);scanf(“%f”, &radius);area = radius * radius * 3.1415;printf(“area = %f\n”, area);

return 0;}

Page 3: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Formatting Specifier

(ٍSpecifierFormat

%fFloat

%d or %iInteger

%ldLong integer

%lfDouble

%cChar

%sString

%pAddress

Page 4: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Again: Example ( 1 )

#define PI 3.14159

int main(){

float radius, area;

printf(“Enter the radius : “);scanf(“%f”, &radius);area = radius * radius * PI;printf(“area = %f\n”, area);

return 0;}

Page 5: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Example ( 2 )

int main(){

int std_id;float grade;

printf(“Enter student ID: “); scanf(“%d”, &std_id);printf(“Enter his grade: “); scanf(“%f”, &grade);printf(“Student %d got “, std_id);if (grade >= 95.0) printf(“A+\n”);else if (grade >= 85.0) printf(“A\n”);else if (grade >= 75.0) printf(“B+\n”);else if (grade >= 70.0) printf(“B\n”);else if (grade >= 65.0) printf(“C\n”);else if (grade >= 60.0) printf(“D+\n”);else if (grade >= 50.0) printf(“D\n”);else printf(“F\n”);

}

Page 6: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Exercise ( 1 )

Write C code to solve the quadratic equation ax^2 + bx + c = 0.

- Consider only the real solutions.

- Check the input’s validity

- Make you program friendly interface.

- Square root function is sqrt().

Page 7: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Solution ( 1 )

int main(){

float a, b, c;float d, x1, x2;

printf(“Enter the coefficient of x^2: “); scanf(“%f”, &a);printf(“Enter the coefficient of x: “); scanf(“%f”, &b);printf(“Enter the constant value: “); scanf(“%f”, &c);

if (a == 0 && b == 0)printf(“Invalid data: no equation\n”);

else if (a == 0) {

x1 = -c / b;printf(“First order equation. x = %.2f\n”, x1);

}

Page 8: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Solution ( 1 )

else {d = b*b – 4*a*c;if (d < 0)

printf(“No real solution\n”);else if (d == 0) {

x1 = -b / (2*a);printf(“Single solution. x = %.2f\n”, x1);

}else {

x1 = (-b + sqrt(d)) / (2*a);x2 = (-b – sqrt(d)) / (2*a);printf(“ x[1] = %.2f\n”, x1);printf(“ x[2] = %.2f\n”, x2);

}}return 0;

}

Page 9: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Example ( 3 )

int main(){

int month;

printf(“Month number: “); scanf(“%d”, &month);printf(“Number of days is “);switch (month) {

case 1: case 3: case 5: case 7:case 8: case 10: case 12: printf(“31\n”);break;case 4: case 6: case 9: case 11: printf(“30\n”);break;case 2: printf(“28 or 29\n”); break;default: printf(“\nInvalid number\n”);

}return 0;

}

Page 10: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Example ( 4 )

int main(){

int number;int i;

printf(“Enter an integer number: “); scanf(“%d”, &number);printf(“The positive divisor(s) of %d is(are): “);for (i=2; i<number/2; i++)

if (number % i == 0) printf(“%d”, i);

return 0;}

Page 11: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Boolean type

C does not support boolean data type.

- However, boolean variables (in C) are integer variables.

- ZERO value means FALSE.

- NON-ZERO value means TRUE.

Page 12: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Exercise ( 2 )

Write a C code to determine if a given number is prime or not.

- You can use break or continue within the loop body.

Page 13: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Solution ( 2 )

int main(){

int number, i;int is_prime = 1; /* boolean variable */

printf(“Enter a number: “); scanf(“%d”, &number);for (i=2; i<number/2; i++)

if (number % i == 0) {is_prime = 0;break;

}if (is_prime)

printf(“%d is a prime number\n”, number);else

printf(“%d is not a prime number\n”, number);return 0;

}

Page 14: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Example ( 4 )

int is_prime (int number){

int flag = 1, i;

for (i=2; i<number/2; i++)if (number % i == 0) {

flag = 0;break;

}return flag;

}

int main(){

int number;

printf(“Enter a number: “); scanf(“%d”, &number);if (is_prime(number))

printf(“%d is a prime number\n”);else

printf(“%d is not a prime number\n”);return 0;

}

Page 15: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Declare before Use

Page 16: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Example ( 5a )

int main(){

int number;

printf(“Enter a number: “); scanf(“%d”, &number);if (is_prime(number))

printf(“%d is a prime number\n”);else

printf(“%d is not a prime number\n”);return 0;

}

int is_prime (int number){

int flag = 1, i;

for (i=2; i<number/2; i++)if (number % i == 0) {

flag = 0;break;

}return flag;

}

Page 17: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Example ( 5b )

#include <stdio.h>

int is_prime (int);

int main(){

int number;

printf(“Enter a number: “); scanf(“%d”, &number);if (is_prime(number))

printf(“%d is a prime number\n”);else

printf(“%d is not a prime number\n”);return 0;

}

int is_prime (int number){

int flag = 1, i;

for (i=2; i<number/2; i++)if (number % i == 0) {

flag = 0;break;

}return flag;

}

Page 18: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Global vs Local

• SCOPE

• LIFE TIME

• LINKAGE

Page 19: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

SCOPE

• Variable declared outside any function has global scope. Global variable

• Variable declared inside a function has local scope (within the function body). local variable

• Global variable with static modifier has scope within the file only.

Page 20: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

LIFE TIME

• Global variable has global life-time

• Local variable has local life-time

• Local variable with static modifier has global life-time.

Page 21: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

LINKAGE

• Internal linkage

• External linkage

Page 22: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Files

• SCOPE

• LIFE TIME

• LINKAGE

Page 23: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Standard files

• Any C program has THREE standard files– stdin (refer to keyboard)– stdout (refer to terminal window)– stderr (refer to terminal window)

• These files are declared in stdio.h

• fprintf() is used to print to a file

• fscanf() is used to read from a file

Page 24: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Standard files

• printf(“Area = %d\n”, area);

• fprintf(stdout, “Area = %d\n”, area);

• scanf(“%f”, &radius);

• fscanf(stdin, “%f”, &radius);

Page 25: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

User files

FILE *myfile;

myfile = fopen(“data.dat”, “w”);

printf(myfile, “Hello world!\n”);

Page 26: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

Integer Representation

• Integer values can be represented:– As decimal.

12 100 188– As octal.

014 0144 0274– As hexadecimal.

0xC 0x64 0xbc

Page 27: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

char

• The data type char means 1-byte integer.

• Characters are represented as 1-byte.

Page 28: Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius.

unsigned modifier

• unsigned char

• unsigned int OR unsigned

• unsigned long