0% found this document useful (0 votes)
24 views

MA 511: Computer Programming: Partha Sarathi Mandal

Here are the C codes for the assignments: 1. int gcd(int a, int b) { if(b==0) return a; else return gcd(b, a%b); } 2. int isFibonacci(int n) { int f1=0, f2=1, next; while(next < n) { next = f1 + f2; f1 = f2; f2 = next; } if(next == n) return 1; else return 0; } 3. double sinx(double x, int n) { double result = x; for(

Uploaded by

Naveen Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

MA 511: Computer Programming: Partha Sarathi Mandal

Here are the C codes for the assignments: 1. int gcd(int a, int b) { if(b==0) return a; else return gcd(b, a%b); } 2. int isFibonacci(int n) { int f1=0, f2=1, next; while(next < n) { next = f1 + f2; f1 = f2; f2 = next; } if(next == n) return 1; else return 0; } 3. double sinx(double x, int n) { double result = x; for(

Uploaded by

Naveen Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

MA 511: Computer Programming

Lecture 3:

http://www.iitg.ernet.in/psm/indexing_ma511/y10/index.html

Partha Sarathi Mandal


[email protected]
Dept. of Mathematics, IIT Guwahati
Semester 1, 2010-11
Last class highlight
• Example C Program
• Data types
• Variable declaration
• Operators
• Operands conversion

MA511: Computer Programming


Partha S Mandal, IITG
Type Casting
• The value of an expression can be converted
to a different data type if desire follows
• (data type) expression
• Example:
int i = 7;
float f = 8.5;
(i+f)%4 = invalid since i+f is floating point.
((int) (i+f)%4 =3

MA511: Computer Programming


Partha S Mandal, IITG
Variable
• Identifier, its used to represent some specific type (single data item) of
information within a portion of program.
main(){
int a, b, c;
char d;
a = 2;
b = 1;
d = ‘u’
c= a+b;
…..

a = 6;
b = 10;
d = ‘X’
c = a*b;
}

MA511: Computer Programming


Partha S Mandal, IITG
Unary operators
• Two unary operators:
– Increment operator: ++ (increased by 1)
++i equivalent i=i+1 //
i++ //value of the operand will altered after it is utilized
Example:
a=10; b=20;
x= ++a;
y=b++;
printf(“x = %d a = %d\n”, x, a) : x = 11 a = 11
printf(“y = %d b = %d\n”, y, b) : y = 20 b = 21
x = ++a equivalent to the following two sequence a = a + 1; then x = a;
Y = b++ equivalent to the following two sequence y = b; then b = b + 1;
X = a++ - ++a; undefined *dependent on compiler’s implementation+
– Decrement operator: -- (decreased by 1)
--i equivalent i=i-1 and i--
– sizeof(type): Example: printf(“integer: %d\n”, sizeof(float));
MA511: Computer Programming
Partha S Mandal, IITG
Relational and logical operators
• Operator Meaning
< less than
<= less than or equal to
> grater than
>= grater than or equal to
== equal to
!= not equal to
|| or
&& and
MA511: Computer Programming
Partha S Mandal, IITG
ASCII (American Code for information Interchange)
Table and Description

MA511: Computer Programming


Partha S Mandal, IITG
Conditional Statements
• int i = 7;
• float f = 5.5;
• char c = ‘w’ *w= 119 (ASCII) +
• Expression: (i>=6)&&(c==‘w’)
(i>=6)||(c==119)
(f<11)&&(i>100)
(c!=‘p’)||((i+f)<=10)
if(logical expression){
CS1
CS2

} /* end of if */

MA511: Computer Programming


Partha S Mandal, IITG
Hierarchy of operator precedence
Operator Category Operators Associativity
Unary operators ++ -- ! sizeof (type) R -> L
Arithmetic multiply, divide * / % L -> R
and remainder
Arithmetic add and subtract + - L -> R
Relational operators < <= > >= L -> R
Equality operators == != L -> R
Logical and && L -> R
Logical or || L -> R
Assignment operators = += -= *= /= %= R -> L

• i >= 6 && c == ‘w’


• i >= 6 || c ==119
• f < 11 && i > 100
• c != ‘p’ || i+f <= 10 MA511: Computer Programming
Partha S Mandal, IITG
Cont.. Conditional statements
if(logical expression){
CS1
CS2

False If logical True } /* end of if */
expression
if(logical expression){
E1 CS1
CS1
E2 CS2
CS2
. .

. .
}
. .
else{
E1
E2

} /* end of if */
MA511: Computer Programming
Partha S Mandal, IITG
Nested conditional statement
if(a > b){
if(c>d){
x=y;
}
False True else{
a>b
x=z;
}
else{
False True
c>d x=w;
}
} /* end of if */
x=w x=z x=y

MA511: Computer Programming


Partha S Mandal, IITG
for loop
i=0

for(i = 0; i <= 10; i= i+1){


False s1
i <= 10
s2
True …
{ s1;
S2; } /* end of for */

}

i=i+1

MA511: Computer Programming


Partha S Mandal, IITG
while loop

while(i <= 10){


False s1
i <= 10
s2
True …
{ s1;
S2; } /* end of while */

}

MA511: Computer Programming


Partha S Mandal, IITG
do while loop

do {
{ s1; s1
S2; s2
… …
}
}
while(i <= 10);

i <= 10
True
False

MA511: Computer Programming


Partha S Mandal, IITG
Assignment operators
• Five additional assignment operators:
+=, -=, *=, /=, %=
expression1 @ = expression2
is equivalent to
expression1 = expression1 @ expression2
Where @ = +, -, *, /, %
Example: i+=5 is equivalent to i = i + 5
i%=(j-2) is equivalent to i = i % (j-2)

MA511: Computer Programming


Partha S Mandal, IITG
Conditional operator
• expression1 ? expression2 : expression3
Example:
1. (i<0) ? 0 : 100
• expression i<0 is evaluated first. If it is true the entire
conditional expression takes on the value 0 otherwise
100.
2. min = (f<g)? f : g
3. c += (a>0 && a <= 10) ? ++a : a/b;

MA511: Computer Programming


Partha S Mandal, IITG
Array
• Variable identifier, refers to a collection of the same type of data items that all
have the same name.
• individual data items are represented by their corresponding array elements.
• Example:
int x*5+; // x*5+ is basically x*0+, x*1+, …, x*4+
float A[10], B[4][5];
for(i=0; i< 10; i=i+1){
scanf(“%d”, &A*i+);
}
for(i = 1; i <= 4; i = i + 1 ){
for(j = 1; j <= 5; j = j + 1){
scanf(“%d”, &B*i+*j+);
}
}
MA511: Computer Programming
Partha S Mandal, IITG
Assignments
• Write c-codes for
1. Write c-code for testing two given integer are relatively prime.
2. Write a program for testing a given integer is a Fibonacci
number.
3. Sinx = x – x3/3! + x5/5! – x7/7! + … x is in radians
i. c-code for sum of the first n terms (input x and n)
ii. Adding successive terms in the series until the value of the next
term smaller than 10-5 in magnitude.
4. Addition of two mxn matrices.
5. Multiplication of mxk and kxn matrices.
6. Transpose a square matrix.
7. Multiplication of two polynomial of degree m and n
respectively where coefficients are integer.

MA511: Computer Programming


Partha S Mandal, IITG

You might also like