Program List
Program List
File: quadratic.c
#include <stdio.h>
#include <math.h>
void main(){
float a,b,c,d,r1,r2;
printf("\nEnter the coefficients a b c : ");
scanf("%f%f%f" ,&a,&b,&c);
if (a==0) {
printf("\nNot a quadratic equation");
}
d=b*b-4*a*c;
r1=-b/(2*a);
if (d==0){
printf("\nThe roots are real and equal");
printf("\nThe root =%f \n",r1);
}
else if(d>0){
r2=sqrt(d)/(2*a);
printf("\nThe roots are real and distinct");
printf("\nFirst root=%f \nSecond root=%f \n",r1+r2,r1-r2);
}else {
r2= sqrt(fabs(d))/(2*a);
printf("\nThe roots are imaginary");
printf("\nFirst root =%f +i%f", r1,r2);
printf("\nSecond root=%f -i%f \n", r1,r2);
}
}
2 Rank List
File: ranklist.c
#include <stdio.h>
#include <string.h>
#define NMAX 20
1
#define NAMELENGTH 10
struct studentType{
char name[NAMELENGTH];
int marks;
};
void main(){
int n,i,j,marks;
char name[NAMELENGTH];
struct studentType students[NMAX];
printf("\nEnter number of students (< %d): ",NMAX);
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\nEnter name and marks of sudent#%d: ",i+1);
scanf("%s %d",students[i].name,&students[i].marks);
}
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++){
if(students[j].marks < students[j+1].marks){
strcpy(name,students[j].name);
strcpy(students[j].name,students[j+1].name);
strcpy(students[j+1].name,name);
marks=students[j].marks;
students[j].marks=students[j+1].marks;
students[j+1].marks=marks;
}
}
printf("\nRank List");
printf("\n%7s %-11s %5s","Rank","Name","Marks");
for(i=0;i<n;i++){
printf("\n%7d %-11s %5d",i+1,students[i].name,students[i].marks);
}
printf("\n");
}
2
Enter name and marks of sudent#4: epsilon 96
Rank List
Rank Name Marks
1 epsilon 96
2 beta 89
3 gamma 78
4 alpha 34
#include<stdio.h>
#include<math.h>
#define PI 3.141593
void main() {
float x,xr;
printf("\n enter the value of x in degree :");
scanf("%f" ,&x );
xr= x*PI/180;
printf("\n sine(%f) =%f \t tan(%f) =%f", x,sin(xr),x,tan(xr));
printf("\n e to the power of -%f is %f",x,exp(-x));
}
4 Ploting Functions
To plot a function f (x), we will create a text file with x-values in the first
column and the corresponding f (x) in the second column. In the examples
we discuss in this section, we assume the filename to be ’data.dat’. There are
many softwares available to produce graphs from the data files. Here, we use
the program Gnuplot.
4.1 Gnuplot
This program can be invoked from the terminal using the command gnuplot.
This will start the program with the prompt gnuplot>. The terminal on gnuplot
start-up looks like the following
$ gnuplot
G N U P L O T
Version 4.6 patchlevel 6 last modified September 2014
3
Build System: Linux x86_64
Example
4
gnuplot> exit
File: plotsin.c
#include<stdio.h>
#include<math.h>
#define PI 3.141593
void main()
{
FILE *fp;
float xstart,xend,xstep,xr,x;
printf("\n enter initial and final values of x and step in degrees :
");
scanf("%f %f %f",&xstart, &xend, &xstep);
fp=fopen("sin.dat","w");
fprintf(fp,"#x \t sin(x) ");
for(x=xstart;x<=xend;x+=xstep){
xr=x*PI/180;
fprintf(fp,"\n %f %f",x,sin(xr));
}
fclose(fp);
}
The contents of the file can be viewed using a texteditor, for example gedit or
scratch.
5
The graph can be plotted using the following commands in gnuplot
$ gnuplot
G N U P L O T
Version 4.6 patchlevel 6 last modified September 2014
Build System: Linux x86_64
The graph can be saved into a PNG file named ‘sin.png’ using the following
set of commands
gnuplot> set terminal png
Terminal type set to ’png’
Options are ’nocrop font "/usr/share/fonts/truetype/liberation
6
/LiberationSans-Regular.ttf,12" fontscale 1.0 size 640,480 ’
gnuplot> set output ’sin.png’
gnuplot> replot
File: plottan.c
#include<stdio.h>
#include<math.h>
#define PI 3.141593
void main()
{
FILE *fp;
float xstart,xend,xstep,xr,x;
printf("\n enter initial and final values of x and step in degrees :
");
scanf("%f %f %f",&xstart, &xend, &xstep);
fp=fopen("tan.dat","w");
fprintf(fp,"#x \t tan(x) ");
for(x=xstart;x<=xend;x+=xstep){
xr=x*PI/180;
fprintf(fp,"\n %f %f",x,tan(xr));
}
fclose(fp);
}
This creates the file ‘tan.dat’ from which Gnuplot creates the graph
7
4.4 Plot exp(−x)
File: plotexp.c
#include<stdio.h>
#include<math.h>
void main()
{
FILE *fp;
float x,xstart,xend,xstep;
printf("\n enter initial and final values of x and step : ");
scanf("%f %f %f",&xstart, &xend, &xstep);
fp=fopen("exp.dat","w");
fprintf(fp,"#x \t exp(-x) ");
for(x=xstart;x<=xend;x+=xstep){
fprintf(fp,"\n %f %f",x,exp(-x));
}
fclose(fp);
}
8
This creates the file ‘exp.dat’ from which Gnuplot creates the graph
File: matrix.c
#include<stdio.h>
#include<stdlib.h>
#define ROWS 10
#define COLS 10
9
for(i=0;i<m;i++){
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
void main(){
int a[ROWS][COLS],b[ROWS][COLS],c[ROWS][COLS];
int m,n,p,q,i,j,k;
for(i=0;i<m;i++){
for(k=0;k<q;k++){
c[i][k]=0;
for(j=0;j<n;j++)
c[i][k]=c[i][k]+a[i][j]*b[j][k];
}
}
10
2 2
First matrix
3 4
7 8
second matrix
10 2
1 7
#include <stdio.h>
void main(){
float a1,a2,a3,b1,b2,b3,c1,c2,c3,scalarproduct=0;
printf("\nEnter (3D)vector a:");
scanf("%f %f %f",&a1,&a2,&a3);
printf("\nEnter (3D)vector b:");
scanf("%f %f %f",&b1,&b2,&b3);
scalarproduct=a1*b1+a2*b2+a3*b3;
c1=a2*b3-a3*b2;
c2=a3*b1-a1*b3;
c3=a1*b2-a2*b1;
The compilation and execution in the terminal looks like the following
11
$ gcc vecproducts.c -o vecproducts
$ ./vecproducts
Scalrproduct= 8.000000
File: trapezoidal.c
#include <stdio.h>
void main(){
int n;
double a,b;
printf("\nEnter number of intervals( >2),initial and final points:
");
scanf("%d %lf %lf",&n,&a,&b);
if((n%2)) n++;
printf("\nTrapezoidal rule: n=%d, integral=%lf\n",n,trapz(fn,a,b,n));
}
12
Trapezoidal rule: n=20, integral=0.785294
File: simpson.c
#include <stdio.h>
#include <math.h>
void main(){
int n;
double a,b;
printf("\nEnter number of intervals(even, >2),initial and final
points: ");
scanf("%d %lf %lf",&n,&a,&b);
if((n%2)) n++;
printf("\nSimpson rule: n=%d, Integral=%lf\n",n,simpson(fn,a,b,n));
}
File: rk2.c
13
#include <stdio.h>
void main(){
float y,t,yout,h,tstart,tend,k1,k2;
FILE *fp;
y=10;
h=0.1;
tstart=0;
tend=10;
fp=fopen("rk2.dat","w");
fprintf(fp,"\n%f %f",tstart,y);
for(t=tstart;t<tend;t+=h){
k1=f(y,t);
k2=f(y+h*k1,t+h);
yout=y+(k1+k2)*h/2;
y=yout;
fprintf(fp,"\n%f %f",t+h,y);
}
fclose(fp);
}
14
15