Problem Solving Notes
Problem Solving Notes
2.Compile-time Errors:-
1.Syntax errors:-
1.Rules of c programming lang are violated
2. int a: //syntax error
2.semantic errors:-
1.Comply with the rules of the programming but not
meaningful to the compiler
2.a*b=c//Does not seem correct
3.logical errors:-
1.errors are unexpected and undesirable output
2.not detected/occurs due to incorrect statements
3.c=a+b//to be
4.c=a*b//mistake
4.linker errors:-
1.Occur when the linker is not able to find the function
definition
2.printf
3.not included #include<stdio.h>
3.Linker
4.Logical
1.Compiler converts High level language into Machine level language
2.exceptional handling
3.Syntax:(Comes under Syntax Error)
Datatype variablename;
eg:
int a;
4.Semantic Error:-
no meaning to the compiler
eg:
a*b=c/no meaning
C=a*b/more meaning (a is operand, * is operator)
5.Logical error:-
can'tbe detected by compiler
eg:
C=a+b/to be done
C=a*b/by mistake
6.Linker Error:-
occur when the linker isnot able to find the function definition
eg:
Printf
not included #include<stdio.h>
7.Testing and debugging approaches:-
1.Unit tests- Checks the single module
2.Integration test- Two units are combined and checked
3.System Test- To test the entire system
8.Debugging approaches:-
1.brute force
2.back tracking
3.cause elimination
1.C programming:-
1.C was devolpedin 1970 by Dennis ritchie
2.Taxonomy of c
1.AlGOL-Algorithmic language
2.BCPL-Basic combined programming language
3.B-Ken Thompson
4.C-Traditional C
5.K&RC
6.ANSI C
7.C89
8.C95
9.C99
3.Cis high level language
4.has 32 keywords
5.it is structured programming language
1.Documentation section
2.Comments-1.Single line comment(//), 2.Multiline comment(/*....)
3.Link section(preprocessor directive)
ex., #include <stdio.h>
4.Definition section
ex.,# define PI 3.14
5.Global declaration section
6.Main function
{
Declaration part
Executable part
}
7. cc greet.c
8. ./a.out
9. nano display.c
1.rep.lit (online coding resource)
2.Character set in C
3.Keywords in C language
4.identifiers and data types
5.Basic Data Types in C:-
1.int
2.float
3.double
4.char
6.declaring a variable:-
syntax-:- datatype variablename,
Eg. int a;, float g;
7.write a program to assign a interger value!
8.syntax- printf("control string", variablename);
1.Basic data types in c:-
1.int
2.float
3.double
4.char stores characters
2.Attributes of data type:-
1.keyword
2.size
3.range
4.contol format
3.how float or double values are stored in memory?
MSD-----------------------------LSB
1.floating point must be converted binary
2.make the converted binary to normalize form(format= 1.significant
bit*2exponent)
3.Add bias to exponent
1.scanf function
2.did programming on code.karunya.edu
2.
// arithmetic operations
#include<stdio.h>
void main()
{
int a,b,a1,b1,a2,a3,b2,b3;
printf("enter the value of a and b");
scanf("%d%d,&a,&b);
printf("Add=%d",a+b);
printf("enter the value of a1 and b1");
scanf("%d%d,&a1,&b1);
printf("Sub=%d,a1-b1);0
printf("enter the value of a2 and b2");
scanf("%d%d,&a2,&b2);
printf("Multi=%d,a2*b2);
printf("enter the value of a3 and b3");
scanf("%d%d,&a3,&b3);
printf("Div=%d,a3/b3);
}
1.scanf()-
1.syntax:-
scanf("conrtl str1,,,")
2.the prototype of the control string can be
%[*][width][modifiers]type
3.rules to use a scanf function
2.Some programms
Write a program to print circumference of a circle (2*3.14*radius)
#include<stdio.h>
int main() {
int rad;
float PI = 3.14, area, ci;
ci = 2 * PI * rad;
printf("\nCircumference : %f ", ci);
return (0);
}
Write a program to convert fahrenheit to degree celsius
#include<stdio.h>
void main()
{
float celsius,fahrenheit;
int main()
{
float x1, y1, x2, y2, distance;
1.Operators in C:-
1. A + B ==> where + is operator, A,B are operands
2. Arithmetic operator
+,-,*,/,% (modulus) finds remainder
3. Relational operator
<(less than),>(greater than),<=(less than equal to),>=(greater than
equal to)
4. Equality operator
==, !=, returns 1 if true, o if false
5. Logical operator
AND(&&), OR(||), NOT(!) returns results as true or false
1.Unary operator:-
they act as single operands
Unary Minus(-)
a=-(b)
increment operator (++)
Prefix operand ++a
postfix....... a++
Decrement operator (--)
1.Assignment operator: -
1. =,+=,-=,*=,/=,%=,**=
2.Comma operator
3.Precedence
4. 3*4%5/2
12%5/2
2/2
1
Write a program to calculate the bill amount for an item given it's quantity sold,
value,discount,tax!
#include<stdio.h>
int main()
{
int q,v,d,t,Amt;
printf("Enter the quantity%d",q);
scanf("\n%d",&q);
printf("\nEnter the Value%d",v);
scanf("\n%d",&v);
printf("\nEnter the Discount ",d);
scanf("\n%d",&d);
printf("\nEnter the Tax ",t);
scanf("\n%d",&t);
Amt=q*v-d+t;
printf("\nThe net amount of the products are %d",Amt);
}
int main()
{
Loop:
..
..
goto Loop;
return0
}
4.Program scope
can access a variable through out the program
5.File Scope
static
6. Storage class
Where the data is stored
how long the storage allocation continue
where the variable can be accessed
variable is automatically initialized
Today we did programming but suddenly something happened to ma’am she left!
Iterative Statement:
A statement to be printed more than 1 time.
For loop
While loop
Do-while loop
Four essentials elements in loop
Loop control variable
Initialization: Initial value
Condition
Step value: Increment value
While LOOP:
Syntax:
While(condition)
{
Statement;
}
Statement x:
Ex:
#include<stdio.h>
void main ()
{int i=1;
while(i<=5)
{
printf(“\nHello”);
i++;
}
}
Do-While LOOP:
Syntax:
Statement x;
Do
{
Statement block;
} while (condition);
Statement y;
Ex:
#include<stdio.h>
Void main ()
{int i=1;
Do
{
Printf(“\nHello”);
i++;
} while(i<=5);
}
// Program to print numbers from 1 to 10 using WHILE LOOP and DO-WHILE
LOOP
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
Int i=0; Int i=0;
While(i<=10) Do
{ {
printf (“\n %d”,i); printf(“\n %d”,i);
i=i+1; i=i+1;
} } while(i<=10);
Return 0; Return 0;
} }
For loop:
Syntax
For (initialization; condition; increment/decrement/update)
{
Statement block;
}
Statement y;
Example:
#include<stdio.h>
Void main()
{
int i, n=5;
for(i=1; i<=n; i++)
{
printf(“\nhello”);
}
LAB Session:
#include <stdio.h>
#include <math.h>
int main()
{
int a,b,c,d;
float r1,r2;
printf("Enter values for a,b,c: ");
scanf("%d%d%d",&a,&b,&c);
d=(b*b) - (4*a*c);
if (d==0)
{
r1=-b/(2*a);
r2=r1;
printf("The roots are real and equal");
printf("\nThe root is %f",r2);
}
else if(d > 0)
{
r1= (-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("The roots are real and unequal");
printf("\nRoot 1 is%f",r1);
printf("\nRott 2 is %f",r2);
}
else
{
r1=-b/(2*a);
r2=sqrt(-d)/(2*a);
printf("The roots are imaginary");
}
}
#include<stdio.h>
#include<math.h>
void main()
{
float weight,height,metric_height,bmi;
printf("Enter your weight in kilograms and height in centimetres : " );
scanf("%f%f",&weight,&height);
metric_height=height/100;
bmi=weight/(metric_height*metric_height);
if (bmi<15)
printf("Starvation");
else if (bmi>15.0 && bmi<17.6)
printf("Anorexic");
else if (bmi>17.5 && bmi<18.6)
printf("Under weight");
else if (bmi>18.5 && bmi<25.0)
printf("Ideal");
else if (bmi>24 && bm
i<30.0)
printf("Over weight");
else if (bmi>29 && bmi<40)
printf("Obese");
else
printf("Morbidly obese");
}
Nested Loop:
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
printf("\n");
for(j=1;j<=1;j++)
{
printf("%d",j);
}
}
}
Break:
Continue:
Goto Statement:
Used to transfer toa specific label
Label must reside in the same function
The syntax for goto statement
goto label:
Function Components:
Function Definition:
Ex Program:
#include<stdio.h>
void add(int,int);
void main()
{
int a,b;
printf("\nEnter two numbers: ");
scanf("%d%d",&a,&b);
add(a,b);
printf("\nEnd of program");
}
Functions:
1. Pre-defined
2. User defined
Ex:
#include<stdio.h>
void greet();
void main()
{
greet();
}
void greet()
{
printf("Welcome");
}
Output:
Welcome
#include<stdio.h>
float area(float w,float l);
void main()
{
float a,w,l;
printf("Enter width: ");
scanf("%f",&w);
printf("Enter length: ");
scanf("%f",&l);
a=area(w,l);
printf("\n%f",a);
}
float area(float w,float l)
{
return w*l;
}
#include<stdio.h>
double power(double a, double b);
void main()
{
double a,b,p;
printf("Enter two numbers : ");
scanf("%lf%lf",&a,&b);
p=power(a,b);
printf("%.2lf",p);
}
Recursion:
Infinite Execution:
void main()
{
printf(“hai\n”);
main();
}
Arrays:
#include<stdio.h>
int main()
{
int i=0,n,arr[20];
printf("\n Enter the number of elements : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("\n The array elements are ");
for(i=0;i<n;i++)
printf("Arr[%d]=%d\t",i,arr[i]);
}
Output:
Enter the number of elements : 1
2
The array elements are Arr[0]=2
#include<stdio.h>
int fact(int n);
void main()
{
int n,r,f1,f2,f3,ncr;
read:
printf("Enter Value for N: \nEnter Value for R: ");
scanf("%d%d",&n,&r);
if(n<r)
{
printf("\nN should be greater than R");
goto read;
}
f1=fact(n);
f2=fact(r);
f3=fact(n-r);
ncr=f1/(f2*f3);
printf("%d",ncr);
}
int fact(int n)
{
int f=1;
if(n>1)
f=n*fact(n-1);
return f;
}
#include<stdio.h>
#include<string.h>
int main()
{
char str[20];
int n,i,j;
printf("ENter a string: ");
scanf("%s",str);
n=strlen(str);
i=0;
j=n-1;
while(i<=j)
{
if(str[i]!= str[j])
break;
i++;
j--;
}
if(i>j)
printf("The string is a palindrome\n");
else
printf("The string is not a palindrome\n");
}
Structure:
It can store elements of different data types.
Syntax:
struct< tagname or structure name>
{
Structure members
};
b1 is structure variable!
#include<stdio.h>
struct book
{
char title[20];
int pages;
}b1;
void main()
{
printf("Enter title ");
scanf("%s",b1.title);
printf("\nEnter no. of pages ");
scanf("%d",&b1.pages);
printf("\nTitle : %s",b1.title);
printf("\nNo. of pages : %d",b1.pages);
}
#include<stdio.h>
struct details
{
char name[20];
int age;
char registno[20];
char gender[10];
}b1;
int main()
{
printf("Enter your name: ");
scanf("%s",b1.name);
printf("Enter your age: ");
scanf("%d",&b1.age);
printf("Enter your Register Number: ");
scanf("%s",b1.registno);
printf("Enter your Gender: ");
scanf("\n%s",b1.gender);
printf("\n%s",b1.name);
printf("\n %d",b1.age);
printf("\n%s",b1.registno);
printf("\n%s",b1.gender);
}
Also
#include<stdio.h>
struct student
{
char name[25];
int age;
};
struct student s1={"john",25};
struct student s2={"kumar",45};
if(s1.age == s2.age)
{
printf("BOTH ARE SAME");
}
else
{
printf("NOT EQUAL");
}
write a program using struct tofind biggest among three members
#include<stdio.h>
struct big
{
int a;
int b;
int c;
}s1={50,25,20};
int main()
{
if(s1.a>s1.b && s1.a>s1.c)
{
printf("A is Big");
}
else if(s1.b>s1.a && s1.b>s1.c)
{
printf("B is Big");
}
else
{
printf("C is Big");
}
}
ARRAY OF STRUCTURES:
#include<stdio.h>
int main()
{
enum type{alphabet,digit,special};
enum type c,t;
printf("Enter a Character: ");
scanf("%c",&c);
if(isalpha(c))
{
t=alphabet;
}
else if(isdigit(c))
{
t=digit;
}
else
{
t=special;
}
switch(t)
{
case alphabet:
printf("\nits an ALPHABET %c",c);
break;
case digit:
printf("\nits an DIGIT %c",c);
break;
case special:
printf("\nits an SPECIAL CHARACTER %c",c);
break;
}
Pointer:
Sum of ARRAY elements in “Array”.
#include<stdio.h>
struct cust
{
int custId;
char custName[50];
long int contactNo;
char custAddress[100];
char custDob[10];
int custAge;
float custMaxamt;
};
int main()
{
int i,n;
struct cust c[50];
printf("Enter the number of records to store\n");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter customer Id,customer Name, contact No, customer Address,
customer Dob,customer age, customer Maximum Amount details one by one\n");
scanf("%d%s%ld%s%s%d
%f",&c[i].custId,c[i].custName,&c[i].contactNo,c[i].custAddress,c[i].custDob,&
c[i].custAge,&c[i].custMaxamt);
}
printf("****************************CUSTOMER PROFILE
****************************\n");
printf("Customer ID\t Customer Name\t Contact No\t, Customer Address\t,
Customer DOB\t, Customer Age\t, Customer Maximum Amount\n");
for(i=0;i<=n-1;i++)
{
printf("%d\t%s\t%ld\t%s\t%s\t%d\t
%f\n",c[i].custId,c[i].custName,c[i].contactNo,c[i].custAddress,c[i].custDob,c[i].
custAge,c[i].custMaxamt);
}
printf("************************* End of Customer Profile
*************************");
}
#include<stdio.h>
int main()
{
FILE *fp;
int regno;
char name[20];
float marks;
double fees;
int n,i;
fp=fopen("E:\\a.dat","w");
printf("Enter the number of students :\n");
for(i=1;i<=n;i++)
{
printf("Enter regno :");
scanf("%d",®no);
printf("Enter name :");
scanf("%s",name);
printf("Enter percentage of marks:");
scanf("%f",&marks);
printf("Enter the fees: :");
scanf("%lf",&fees);
fprintf(fp,"%d\t%s\t%f\t%lf\n",regno,name,marks,fees);
}
fclose(fp);
fp=fopen("E:\\a.dat","r");
printf("\nThe student details are\n");
while(fscanf(fp,"%d%s%f%lf",®no,name,&marks,&fees)>0)
{
printf("\nRregno : %d",regno);
printf("\nName : %s",name);
printf("\nPercentage of marks : %f",marks);
printf("\nFees : %lf",fees);
printf("\n---------------------------\n");
fclose(fp);
}
}