CTSD Workshop Content

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 50

Workshop on

303105104 - Computational Thinking


for Structured Design-1
Part I

Revision of
Concepts
INTRODUCTION TO C LANGUAGE
lNTRODUCTlON

C is a remarkable language. Designed originally by Dennis Ritchie,


working at AT&T Bell Laboratories in New Jersey, it has increased in
use until now it may well be one of the most widely- written computer
languages in the world. C is a structured language. It allows variety of
programs in small modules. It is easy for debugging, testing, and
maintenance if a language is a structured one.
PROGRAM DEVELOPMENT STEPS
•The program development steps or phases that we follow a sequence of steps to develop a program in any
programming language.
•Generally, the program development steps contains 6 phases, they are as follows
1. Requirements
2. Analysis
3. Design
4. Coding
5. Testing
6. Maintenance
FEATURES OF C LANGUAGE
•Simple
•Machine Independent or Portable
•Mid-level programming language
•structured programming language
•Rich Library
•Memory Management
•Fast Speed
•Pointers
•Recursion
•Extensible
STRUCTURE OF C PROGRAM
Documentation – comments about program
Link- instructions to compiler
Definition- defines all symbolic constants
Global declaration- variable that is frequently used
Main()-
Declaration: all variables
Execution: programming
Subprogram- user defined functions
EXECUTION OF C PROGRAM
EXAMPLE
KEYWORDS
Auto Double Int Struct
Break Else Long Switch
Case Enum Register Typedef
Char Extern Return Union
Const Float Short Unsigned
Continue For Signed Void
Default Goto Sizeof Volatile
Do If Static while
DATATYPES
Data type Size (Bytes) Range Format Specifiers
Char 1 – 128 to 127 %c
Unsigned char 1 0 to 255 %c
Short or int 2 – 32,768 to 32, 767 %i or %d
Unsigned int 2 0 to 655355 %u
Float 4 3.4e – 38 to +3.4e +38 %f or %g
Long 4 = 2147483648 to %ld
2147483647
Unsigned long 4 0 to 4294967295 %lu
Double 8 1.7e – 308 to 1.7e+308 %lf
Long double 10 3.4e – 4932 to 1.1e+4932 %lf
OPERATORS

Type of Operator Symbolic


representation
Arithmetic operators +, -, *, /, %
Relational operators >, <, ==, >=, <=, !=
Logical operators &&, ||, !=
Increment and decrement operator ++ and --
Assignment operator =
Bitwise operator &, |, ^, >>, <<, ~
Comma operator ,
Conditional operator ?:
DECIЅION
ЅTATEMENTЅ
It checks the given condition and then executes its sub-block. The decision statement decides
the statement to be executed after the success or failure of a given condition.
Types:
1. If statement
2. If-else statement
3. Nested if-else statement
4. Break statement
5. Continue statement
6. Goto statement
7. Switch() statement
8. Nested switch ()case
9. Switch() case and Nested if
DECIЅION
ЅTATEMENTЅ Statement Syntax
If statement if(condition)
Statement;
If-else statement If (condition)
{
Statement 1;
Statement 2;
}
else
{
Statement 3;
Statement 4;
}
DECIЅION
ЅTATEMENTЅ
Nested if-else statement If (condition)
{
Statement 1;
Statement 2;
}
Else if (condition)
{
Statement 3;
Statement 4;
}
Else
{
Statement 5;
Statement 6;
}
DECIЅION
ЅTATEMENTЅ
Break statement Break;
Continue statement Continue;
Goto statement goto label;
Switch() statement Switch (variable or expression)
{
Case constant A:
Statement; Break;
Case constant B:
Statement; Break;
Default: Statement;
}
LOOP CONTROL ЅTATEMENTЅ
Loop is a block of statements which are repeatedly
executed for certain number of times.
Types
1. For loop
2. Nested for loops
3. While loop
4. do while loop
5. do-while statement with while loop
LOOP CONTROL ЅTATEMENTЅ
Statement Syntax
For loop For(initialize counter; test condition; re-evaluation parameter)
{
Statement;
Statement;
}
Nested for loop for(initialize counter; test condition; re-evaluation parameter)
{
Statement;
for(initialize counter; test condition; re-evaluation parameter)
Statement;
Statement;
}
}
LOOP CONTROL ЅTATEMENTЅ
While loop While (test condition)
{
Body of the loop
}
Do while loop do
{
Statement;
}
While(condition);
Do-while with while Do while(condition)
loop {
Statement;
}
While (condition);
ARRAYЅ

It is a collection of similar data types in which each element is


located in separate memory locations.
Types
1. One dimensional array
2. Two dimensional arrays
3. Three or multi dimensional arrays
ЅTRINGЅ

Character arrays are called strings. Group of


characters, digits, symbols enclosed within quotation
marks are called as strings.
STANDARD ЅTRINGЅ FUNTIONS

Functions Description
Strlen() Determines the length of a string
Strcpy() Copies astring from source to destination
Strncpy() Copies charcters of a string to another string upto the specified
length
Stricmp() Compares characters of two strings
Strcmp() Compares characters of two strings upto the specified length
Strncmp() Compares characters of two strings upto the specified length
Strnicmp() Compares characters of two strings upto the specified length
Strlwr() Converts uppercase characters of a string to lower case
Strupr() Converts lowercase characters of a string to upper case
Strdup() Duplicates a string
STANDARD ЅTRINGЅ FUNTIONS

Strchr() Determines the first occurrence of a given character in a string


Strrchr() Determines the last occurrence of a given character in a string
Strstr() Determines the first occurrence of a given string in another string
Strcat() Appends source string to destination string
Strrev() Reverses all characters of a string
Strset() Sets all characters of a string with a given argument or symbol
Strspn() Finds up to what length two strings are identical
Strpbrk() Searches the first occurrence of the character in a given string and
then displays the string starting from that charcter
FUNCTIONЅ
It is a self-contained block or a sub program of one or more
statements that performs a special task.
Declaration of functions Function_name
(argument/parameter) Argument
declaration;
{
Local variable declaration;
Statement1;
Statement 2; Return (value);
FUNCTIONЅ

Call by value
In this type, value of actual arguments is passed to the formal arguments and the operation is
done on the formal arguments. Any change made in the formal argument does not effect the actual
arguments because formal arguments are photo copies of actual arguments.

Call by reference
In this type, instead of passing values, addresses are passed. Function operates on address rather
than values. Here the formal arguments are pointers to the actual argument.
Pointers

Pointers are one of the core components of the C


programming language.

A pointer can be used to store the memory address of


other variables, functions, or even other pointers. The use
of pointers allows low-level memory access, dynamic
memory allocation, and many other functionality in C.
Part II

Hands On
Exercise
1. Program to find sum of two numbers.
#include<stdio.h>
#include<conio.h> void main()
{
int a,b,s;
clrscr();
printf(“Enter two no: ”);
scanf(“%d%d",&a,&b); s=a+b;
printf(“sum=%d”,s); getch();
}
Output:
Enter two no: 5 6
sum=11
1. Program to find the simple interest.
#include<stdio.h>
#include<conio.h>
void main()
{
int p,r,t,si;
clrscr();
printf(“enter principle, Rate of interest & time to find simple interest: ”);
scanf(“%d%d%d”,&p,&r,&t);
si=(p*r*t)/100;
printf(“simple intrest= %d”,si); getch();
}
Output:
enter principle, rate of interest & time to find simple interest: 500 5
2
simple interest=50
1. Program to calculate sum of 5 subjects and find percentage.
#include<stdio.h>
#include<conio.h>
void main()
{
int s1,s2,s3,s4,s5,sum,total=500;
float per;
clrscr();
printf(“enter marks of 5 subjects: ”);
scanf(“%d%d%d%d%d”,&s1,&s2,&s3,&s4,&s5)
; sum=s1+s2+s3+s4+s5;
printf(“sum=%d”,sum);
per=(sum*100)/total;
printf(“percentage=%f”,per);
getch();
}
Program to reverse a given number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a,r=0; clrscr();
printf(“enter any no to get its reverse: ”);
scanf(“%d”,&n);
while(n>=1)
{
a=n%10;
r=r*10+a;
n=n/10;
}
printf(“reverse=%d”,r); getch();
Program to show the use of conditional operator.
#include<stdio.h>
#include<conio.h>
void main()
{
printf(“enter value for a & b: ”);
scanf(“%d%d”,&a,&b);
(a>b)?printf(“a is greater”):printf(“b is reater”);
getch();
}
Program to find whether given no. is even or odd.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf(“enter any no: ”);
scanf(“%d”,&n);
if(n%2==0)
printf(“no is even”);
else
printf(“no is odd”);
getch();
}
8. Program to use switch statement. Display Monday to Sunday.
void main()
{
char ch;
printf(“enter m for Monday\nt for Tuesday\nw for Wednesday\nh for Thursday\nf for Friday\ns for Saturday\nu for Sunday);
scanf(“%c”,&ch); switch(ch)
{
case ‘m’:
case ‘M’: printf(“monday”); break;
case ‘t’:
case ‘T’: printf(“tuesday”); break;
case ‘w’:
case ‘W’: printf(“wednesday”); break;
case ‘h’:
case ‘H’: printf(“thursday”); break;
case ‘f ’:
case ‘F’: printf(“friday”); break;
case ‘s’:
case ‘S’: printf(“saturday”); break;
case ‘u’:
case ‘U’: printf(“sunday”); break;
default : printf(“wrong input”); break;
}
Program to display arithmetic operator using switch case.
void main()
{
int a,b,n,s,m,su,d;
printf(“enter two no’s : ”);
scanf(“%d%d”,&a,&b);
printf(“enter 1 for sum\n2 for multiply\n3for subtraction\n4 for division: ”); scanf(“%d”,&n);
switch(n)
{
case 1:
s=a+b; printf(“sum=%d”,s); break;
case 2:
m=a*b; printf(“multiply=%d”,m); break;
case 3:
su=a-b; printf(“subtraction=%d”,su); break;
case 4:
d=a/b; printf(“divission=%d”,d); break;
default: printf(“wrong input”); break;
}
getch();
Program to add two number using pointers.
#include<stdio.h>
#include<conio.h>
void main()
{
int *p1,*p2,sum;
printf(“enter two no’s: ”);
scanf(“%d%d”,&*p1,&*p2);
sum=*p1+*p2;
printf(“sum=%d”,sum);
getch();
}
Output:
enter two no’s: 10 20
sum=30
Program to find the maximum number in array using pointer.
void main()
{
int max,i,*a[5];
clrscr();
printf(“enter element for the array: ”);
for(i=0;i<5;i++)
scanf(“%d”,&*a[i]);
max=*a[0]; for(i=1;i<5;i++)
{
if(max<*a[i])
max=*a[i];
}
printf(“maximum no= %d”,max);
getch();
Program to find square of a number using functions.
#include<stdio.h>
#include<conio.h>
void main()
{
int rev(int);
int r,a; clrscr();
printf(“enter any no: ”);
scanf(“%d”,&a); r=rev(a);
printf(“square is : %d”,r);
getch();
}
int rev(int x)
{
return(x*x);
}
Program to swap two numbers using functions.
void swap(int a,int b)
#include<stdio.h>
{
#include<conio.h> int
void main() temp;
{
temp=a;
void swap(int,int);
a=b;
int a,b,r;
b=temp;
clrscr();
printf(“after swapping the value for a & b is : %d %d”,a,b);
printf(“enter value for a&b:
}
”);
scanf(“%d%d”,&a,&b);
swap(a,b);
getch();
}
Program to show call by value.
int swap(int x,int y)
void main()
{
{
int temp;
int a,b,swap();
temp=x;
a=5; b=10;
x=y;
printf(”value of a=%d & value of b=%d before
y=temp;
swap ”,a,b); swap(a,b);
}
printf(“\nvalue of a =%d & b=%d after
swap”,a,b);
}
Program to show call by reference.
#include<stdio.h> int swap(int *x,int *y)
#include<conio.h> {
int
void main()
temp;
{
int a,b,*aa,*bb,swap(); temp=*
x;
a=5;
*x=*y;
b=10;
*y=temp;
aa=&a;
}
bb=&b;
printf(“value of a= %d & value of b=%d before swap”,a,b);
swap(aa,bb);
printf(“\nvalue of a=%d & b=%d after swap”,a,b);}
Write a c program to calculate Area of Rectangle , Perimeter of a
// calculate area
Rectangle and Diagonal of a Rectangle.
a = l * b;
#include <stdio.h>
// calculate perimeter
#include <math.h>
p = 2 * (l + b);
int main() {
// calculate diagonal
// declare variables
d = sqrt(l * l + b * b);
float l, b, a, p, d;
// display result upto 3 decimal places
// take input
printf("Area: %0.3f\n", a);
printf("Enter the length of rectangle: ");
printf("Perimeter: %0.3f\n", p);
scanf("%f", & l);
printf("Diagonal: %0.3f\n", d);
printf("Enter the breadth of rectangle: ");
return 0;
scanf("%f", & b);
}
To insert a sub-string into a given main string from a given position.
#include<stdio.h> printf("Enter the position where the string is to be
#include<conio.h> inserted\n");
#include<string.h> scanf("%d", &n);
void main() for(i = n; i < l1; i++)
{ {
char str1[20], str2[20]; str1[i + l2] = str1[i];
int l1, l2, n, i; }
clrscr(); for(i = 0; i < l2; i++)
puts("Enter the string 1\n"); {
gets(str1); str1[n + i] = str2[i];
l1 = strlen(str1); }
puts("Enter the string 2\n"); str2[l2 + 1] = '\0';
gets(str2); printf("After inserting the string is %s", str1);
l2 = strlen(str2); getch();
}
To delete n Characters from a given position in a given string,
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
for(i = pos + n; i < l; i++)
{
{
char str[20];
str[i - n] = str[i];
int i, n, l, pos;
}
clrscr();
str[i - n] = '\0';
puts("Enter the string\n");
printf("The string is %s", str);
gets(str);
getch();
printf("Enter the position where the characters are to be deleted\n");
}
scanf("%d", &pos);
printf("Enter the number of characters to be deleted\n");
scanf("%d", &n);
l = strlen(str);
1. Write a C program to determine if the given string is a palindrome or not.
#include <stdio.h> int main()
#include <string.h> {
int checkpalindrome(char *s)
{ char s[1000];
int i,c=0,n;
n=strlen(s); printf("Enter the string: ");
for(i=0;i<n/2;i++) gets(s);
{
if(s[i]==s[n-i-1])
c++; if(checkpalindrome(s))
} printf("string is palindrome");
if(c==i) else
return 1; printf("string is not palindrome");
else
return 0; }
}
Write a C program that displays the position or index in the
string S where the string T begins, or -1 if S does not contain T.
void main() if(found)
{ {
char s[30], t[20]; printf("Second String is found in the First String at
char *found; %d position.\n", found - s);
clrscr(); }
puts("Enter the first string: "); else
gets(s); {
puts("Enter the string to be searched: "); printf("-1");
gets(t); }
found = strstr(s, t); }
1. Write a C program to generate Pascal's triangle.
#include <stdio.h>
void printPascal(int n)
{ int main()
for (int line = 1; line <= n; line++) { {
for (int space = 1; space <= n - line; space++) int n = 5;
printf(" "); printPascal(n);
// used to represent C(line, i) return 0;
int coef = 1; }
for (int i = 1; i <= line; i++) {
// The first value in a line
// is always 1
printf("%4d", coef);
coef = coef * (line - i) / i;
}
printf("\n");
}
}
1. Write a C program to construct a pyramid of numbers.
#include <stdio.h>
int main() {
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}
return 0;
}
Any
Doubt??
www.paruluniversity.ac.in

You might also like