Introduction to Computer and Programing - Lecture 04

12
Introduction to Computers and Programming (CSC103) Lecture 04

description

 

Transcript of Introduction to Computer and Programing - Lecture 04

Page 1: Introduction to Computer and Programing - Lecture 04

Introduction to Computers and

Programming (CSC103)

Lecture 04

Page 2: Introduction to Computer and Programing - Lecture 04

2

void main (void) {

printf("Testing...\n..1\n...2\n....3\n") } Compilation Result?

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

value1 = 50; value2 = 25; int value1, value2, sum; sum = value1 + value2; printf("The sum of %d and %d is %d\n", value1, value2, sum);

} Compilation Result?

1

2

Page 3: Introduction to Computer and Programing - Lecture 04

3

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

printf ("Testing...\n..1\n...2\n....3\n"); } Output?

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

int value1, value2, sum; value1 = 50; value2 = 25; sum = value1 + value2; printf("The sum of %d and %d is %d\n", value1, value2, sum);

} Output?

1

2

Page 4: Introduction to Computer and Programing - Lecture 04

Arithmetic Instructions in C

4

int ad ; float kot, deta, alpha, beta, gamma; ad = 3200 ; kot = 0.0056 ; deta = alpha * beta / gamma + 3.2 * 2 / 5 ; ---------------------------------------------------------------------------------

Here, *, /, -, + are the arithmetic operators.

= is the assignment operator.

2, 5 and 3200 are integer constants.

3.2 and 0.0056 are real constants.

ad is an integer variable.

kot, deta, alpha, beta, gamma are real variables.

Page 5: Introduction to Computer and Programing - Lecture 04

Variable Declaration and Use

5

Correct Incorrect

float a = 1.5, b = a + 3.1; float b = a + 3.1, a = 1.5;

int a, b, c, d ; a = b = c = 10 ;

int a = b = c = d = 10;

int x, y, z; x = y * z;

int x, y, z; y * z = x;

Cannot Use a Variable before declaring it!

Page 6: Introduction to Computer and Programing - Lecture 04

Integer and Float Conversion

6

Implicit and Explicit Conversions

An arithmetic operation between an integer and integer

always yields an integer result.

An operation between a real and real always yields a real

result.

An operation between an integer and real always yields a real

result. In this operation the integer is first promoted to a real

and then the operation is performed. Hence the result is real.

Page 7: Introduction to Computer and Programing - Lecture 04

7

int i ;

float b ;

i = 3.5 ;

b = 30 ;

What would be the value of i and b?

Think of Integer and Float Conversion

Page 8: Introduction to Computer and Programing - Lecture 04

8

k is an integer variable

a is a real/float variable

Page 9: Introduction to Computer and Programing - Lecture 04

Receiving User Input To get the input from the user we use a library function called scanf()

The format of scanf() is;

scanf(“<format specifier>”, &<variable name>);

format specifier shows what type of data you want to input or get from user

& before the variable name is must

& is an “Address of Operator”

It gives the location number used by the variable in memory

&num tells the scanf() function at which memory location should it store the value supplied by the user

a blank, a tab or a new line (pressing enter) must separate the values supplied to scanf()

Example: scanf(“%d %d %f”, &p,&n,&r);

9

Page 10: Introduction to Computer and Programing - Lecture 04

Example

#include<stdio.h>

void main()

{

int num;

printf(“Enter an Integer: ”);

scanf(“%d”, &num);

printf(“You entered the number %d”, num);

}

Output?

10

Page 11: Introduction to Computer and Programing - Lecture 04

Example # 2

11

#include<stdio.h>

void main() {

int x, y;

scanf("%d%d",&x,&y);

printf(“You have entered x=%d, y=%d",x,y);

}

Output?

Page 12: Introduction to Computer and Programing - Lecture 04

Evaluate yourself!

12

Point out the errors, if any, in the following C statements:

(a) int = 314.562 * 150 ;

(b) name = ‘Ajay’ ;

(c) varchar = ‘3’ ;

(d) 3.14 * r * r * h = vol_of_cyl ;

(e) k = ( a * b ) ( c + ( 2.5a + b ) ( d + e ) ;

(f) m_inst = rate of interest * amount in rs ;