What Exactly a null pointer? and pointer and NULL different or same?

Well Friends, According to the ISO C99 standard: An integer constant expression with value 0, or such an expression cast to type void *, is called null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer is called a null pointer ( guaranteed to compare unequal to a pointer to any object or function).


Difference between null pointer and NULL

While null pointer is a pointer which is guaranteed not to point to any object , NULL is just a macro which defines null pointer.

C Program to print its own source code as an Output



main()
{
char *x = "main()
{
char *x = %c%s%c;
printf(x,34,x,34);
}";
printf(x,34,x,34);
}

C Program without using main() Function

# include
# include
# define pop(u,v,w,x,y,z) w ## z ## v ## x
# define demofun pop(s,i,m,n,t,a)

int demofun()
{
printf("I run without Main...");
}

code in c++ that defines a button class to draw a button


#include
#include
#include
#include
#include
#include

class button
{
private:
int x;
int y;
char *s;
public:
void draw()
{
if (textwidth(s)>50)
{
setfillstyle(SOLID_FILL, 7);
bar(x, y, x+textwidth(s)+4, y+20);
setcolor(15);
line(x, y, x+textwidth(s)+4, y);
line(x, y, x, y+20);
setcolor(8);
line(x+textwidth(s)+4, y, x+textwidth(s)+4, y+20);
line(x, y+20, x+textwidth(s)+4, y+20);
setcolor(0);
outtextxy(x+2, y+5, s);
return;
}
setfillstyle(SOLID_FILL, 7);
bar(x, y, x+45, y+20);
setcolor(15);
line(x, y, x+45, y);
line(x, y, x, y+20);
setcolor(8);
line(x+45, y, x+45, y+20);
line(x, y+20, x+45, y+20);
setcolor(0);
outtextxy(x+4, y+5, s);
};
void set(int a, int b, char *c)
{
x=a;
y=b;
strcpy(s, c);
};
};
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
button c;
initgraph(&gdriver, &gmode, "");
c.set(100, 100, "Aymen");
c.draw();
delay(5000);
closegraph();
return 0;
}

Pattern Matching in C


#include
#include
#include

void sgrep(char *filename, char *pattern);

int main(int argc, char *argv[])
{
char *filename;
FILE *file;
char *pattern;
int hit;
if(argc<2)
{
printf("missing argument\n");
}
else
{
if(argc==2)
{
filename = argv[1];
printf("Pattern to search for:\n");
scanf("%s",pattern);
sgrep(filename,pattern);
//printf("MAIN: hit=%d\n",hit);

}
}

return 0;
}
void sgrep(char *filename, char *pattern)
{
//Search file for the first character in the pattern
int i;
FILE *file;
char *chars = "1";
char *filedata;
char first;
int index;
int j;
int k;
int hit;
if((file=fopen(filename,"r"))==NULL)
{
printf("cannot open file %s\n",filename);
}
else
{
file=fopen(filename,"r");
char *data = (char*)malloc(100);
while(chars)
{
chars = fgets(data,100,file);

if(chars)
{
for(i=0;i

HORNER rule to calculate value of a polynomial in C



int horner_rule(int poly[],int n,int x,int i){
if(i==n)
return poly[i];
else
return poly[i]+x*horner_rule(poly,n,x,i+1);
}
/*for equ like 2*x
*poly[]={0,2};
*/
int poly[]={1,2,3};// polynomial in the form 1+2*x+3*x*x....
int x0;
printf("enter value at which you want to calculate this polynomial :");
scanf("%d",&x0);
printf("%d",horner_rule(poly,(sizeof(poly))/sizeof(poly[0])-1,x0,0));
return 0;
}

Creating C Program with Friend Functions and Function Overloading


#include
using namespace std;


int inputArraySetA[20];
int inputArraySetB[20];
//int inputArrayUnion[20];
int sizeSetA = 0;
int sizeSetB = 0;
//int sizeUnion = 0;
int temp = 0;




class Set
{
protected:
int x;
int y;
public:
Set();
Set(int px,int py);
void AddElement(int v);
void RemoveElement(int v);
void Print();
friend void Union(int inputArraySetA[20], int inputArraySetB[20]);
friend void Intersect(int inputArraySetA[20], int inputArraySetB[20]);
friend void Minus(int inputArraySetA[20], int inputArraySetB[20]);


};


//--------------------------------------------------------------------------------------------------------------------
void Union(int inputArraySetA[20], int inputArraySetB[20])
{


}


//--------------------------------------------------------------------------------------------------------------------

int main()
{



cout << "Enter First set of numbers, (-1) to quit: "< for (int i = 0; i < 20; i++)
{
cin >> temp;
if (temp == -1)
{
break;
}
else
{
inputArraySetA[i] = temp;
sizeSetA++;
}
}



cout << "Enter Second set of numbers, (-1) to quit: "< for (int i = 0; i < 20; i++)
{
cin >> temp;
if (temp == -1)
{
break;
}
else
{
inputArraySetB[i] = temp;
sizeSetB++;
}
}


cout << "Set A is ";
for (int i = 0; i < sizeSetA; i++)
{
cout << inputArraySetA[i] << " ";
}
cout << "\nSet B is ";
for (int i = 0; i < sizeSetB; i++)
{
cout << inputArraySetB[i] << " ";
}


return 0;
}


Creating Isosceles Triangle in C

Creating Isosceles Triangle



#include
#define Mx 20
#define My 20
void draw(int base,int height);

int main()
{
// draw(5,4);
// draw(5,4);
// draw(15,6);
int a=2,h=0;
char c;
while(a%2==0){
printf("Enter the base:");
scanf("%d",&a);
if (a%2==0)
printf("only odd length is allowed\n");
getchar();
}
while (h==a || h<2){
printf("Enter the height :");
scanf("%d",&h);
if (h==a)
printf("\nthe height you entered makes an Equilateral triangle.\n");
if (h<2)
printf("\nheight must be greater than 1.\n");
getchar();
}

draw(a,h);
getchar();
}
void draw(int base,int height){
if(base%2==0) {
printf("The length of the base its not an odd number \n");
return ;
}
int i=0,j;
int a=i;
int b=base,t;
// (base>height)?t=base:t=height;
int howmanystars=(base)/(height-1);
float stars=height-1;
stars=(float)(base)/stars-0.5;
if (howmanystars<=stars)
howmanystars++;
int h=1;
for (i=0;i<=height;i++){
if (i==height-1)
h=base;
for (int k=0;k<=(base-h+1)/2;k++)
printf(" ");

for (j=0;j<=h;j++)
printf("*");
h=h+howmanystars;

printf("\n");
}

}

Cal volume of Cylinder using function in C

#include 
#include

#define PI 3.142

int main () {

int radius;
int height;
int volume;
int program;

printf ("Enter 2 to start and 8 to quit\t");
scanf ("%d", &program);

while (program !=9)
{
printf ("Press \n 1 for radius \n 3 for height \n 5 to volume \n 9 to quit:\t");
scanf ("%d", &program);

if (program==1) {
printf ("Enter the value in radius\t");
scanf ("%d", &radius); }

if (program==3) {
printf ("Enter the value for height:\t");
scanf ("%d", &height); }

if (program==5) {
volume = PI * radius * radius * height;
printf ("\n VOLUME OF CYLINDER WITH RADIUS %dUNITS: & HEIGHT %dUNITS: IS %d CUBIC UNITS\n", radius, height, volume);
printf ("\n Do you wish to calculate the Volume of another Cylinder?\n"); }
}
system ("pause");
return 0;
}

Points on Pointers

  • A pointer containing garbage can not be detected in certain cases
  • Abundance of operators are to be used carefully with pointers
  • Passing pointers to double dimension arrays need other than first size to be specified
  • Pointer to auto array of a function can not be passed back

Relation between Pointers and Structures in C Language

Relation between Pointers and Structures in C :-

  • struct inventory { char name[30];
  • int number;
  • float price; }
  • product[2],*p;
  • p = product; // assign zeroth element
  • p -> name is same as (*p).name
  • ++ptr->count increments count
  • (++ptr)->count increments ptr and then return count
  • ptr++->count is legal and increments ptr after accessing count
  • *ptr->p fetches whatever p points to
  • *ptr->p++ increments p after accessing whatever it points to
  • (*ptr->p)++ increments whatever p points to
  • *ptr++->p increments ptr after accessing whatever it points to

Relation between Pointers and Functions in C

Relation between Pointers and Functions

  • Passing a pointer to a function is known as call by reference and function works on actual variables in that case
  • void copy(char *s1, char*s2) { while ((*s1++ = *s2++) != ‘\0’ }
  • Pointer parameters are very common in string functions like above
  • *s++ and *++s are different!
  • type (*ptr)() is a pointer to a function with return type as type
  • It is different than type *ptr()
  • double (*p1)(), mul();
  • p1 = mul;
  • (*p1) (x,y) is same as mul(x,y)
  • It is used in system programming

Pointers and Char Strings


Pointers and Char Strings

  • char name[20], *cptr = name; is valid
  • while (*cptr != ‘\0’) or (*cptr) is true until the end of the string is reached
  • while (*cptr); length = cptr – name;
  • char *name; name = “Delhi” is accepted
  • Char arrays with variable row length are called ragged arrays, better handled by ptrs

Relationship between Pointers and Arrays in C

  • Array name is a pointer to itself
  • ANSI standard makes &a = a
  • int x[10], *p = x; makes p = &x[0]
  • while ( p < &x[4]) { sum+=*p; p++ }
  • The compiler allocates contiguous space for all the elements row-wise in multidimensional arrays
  • int x[10][20] and int *p = x is allowed
  • When we increment i by 1, p will be incremented by the size of the row and not the size of the element itself
  • p is pointer to first row, p+i is pointer to ith row, *(p+i) is the first element of the ith row
  • *(p+i) + j is jth element of ith row
  • *(*(p+i)+j) value stored in cell (i,j)

Pointer Comparison in C

  • p1 > p2 is true if p1 is having higher memory location than p2
  • p1 == p2 and p1 !=p2 are also accepted as valid
  • pointers to related variables like array or string elements makes sense
  • Pointers can not be used in division, multiplication or addition

Pointer Expressions in C

  • y = *p * *q // (*p) * (*q)
  • sum += *p , *p2 = *p2 + 10
  • z = 5 * - *p2/ *p1 //(5 * (-(*p2)))/(*p1)
  • in above /* is not acceptable
  • p+1, p – 4, p++, p- - are allowed
  • p2 –p1 for the same array are no of elements in between them

Accessing a Variable Through Pointer in C

  • The indirection operator (*) is used to access the value of a variable by its ptr
  • * can be remembered as value at address
  • int n = *p // int *p = &quantity is done
  • int n = *&quantity // is = quantity
  • *5445 where 5445 is a valid location does not yield the content at that address
  • *x=25 changes value of f indirectly

Declaring and Initializing Pointers in C

  • int *p; // p contains address of an int
  • float *x // x contains address of a float
  • p = &quantity // quantity is an int variable
  • p = &f where f is float is an erroneous statement, because *p results in a wrong ans
  • int x, *p = &x; // initializes p to address of x
  • int *p=&x, x is invalid

Accessing The Address of a variable in Pointers in C

Accessing The Address of a variable

  • The actual location of a variable in the memory is system dependent
  • It may vary throughout the execution
  • Address operator is used to access it
  • &125, int x[10], &x, &(x+y) are illegal use of address operator
  • &x[0] or &[x+i] are valid operations

Address of a variable, Declaring and Initializing Pointers, Accessing a Variable Through Pointer, Pointer Expressions, Pointer Comparison.

So it will show you details of the : - Address of a variable, Declaring and Initializing Pointers, Accessing a Variable Through Pointer, Pointer Expressions, Pointer Comparison, Pointers and Arrays, Pointers and Char Strings, Pointers and Functions, Pointers and Structure.

Understanding Pointers


  • Computer memory is a sequential collection of storage cells for data and instructions
  • Each cell, has an address associated with it
  • The addresses are numbered contiguously
  • The last address depends on memory size
  • During execution system always associates name of a variable with it’s address


Program to perform some basic operations strcmp, strcat, strlen, strcpy on string in C Programming

This program shows Program to perform some basic operations of inbuilt function like strcmp, strcat, strlen, strcpy on string in C Programming.





#include
#include
#include

int xstrlen ( char * ) ;
void xstrcpy ( char *, char * ) ;
void xstrcat ( char *, char * ) ;
int xstrcmp ( char *, char * ) ;
void show ( char * ) ;

void main( )
{
char s1[ ] = "kicit" ;
char s2[ ] = "Nagpur" ;
char s3[20] ;
int len ;

clrscr( ) ;

printf ( "\nString s1: %s", s1 ) ;
len = xstrlen ( s1 ) ;
printf ( "\nlength of the string s1: %d", len ) ;

printf ( "\nString s2: %s", s2 ) ;

xstrcpy ( s3, s1 ) ;
printf ( "\nString s3 after copying s1 to it: %s", s3 ) ;

xstrcat ( s3, s2 ) ;
printf ( "\nString s3 after concatenation: %s", s3 ) ;

if ( xstrcmp ( s1, s2 ) == 0 )
printf ( "\nThe strings s1 and s2 are similar" ) ;
else
printf ( "\nThe strings s1 and s2 are not similar" ) ;

getch( ) ;
}

/* finds the length of the string */
int xstrlen ( char *s )
{
int l = 0 ;
while ( *s )
{
l++ ;
s++ ;
}
return l ;
}

/* copies source string s to the target string t */
void xstrcpy ( char *t, char *s )
{
while ( *s )
{
*t = *s ;
t++ ;
s++ ;
}
*t = '\0' ;
}

/* concatenates the two strings */
void xstrcat ( char *t, char *s )
{
while ( *t )
t++ ;
while ( *s )
*t++ = *s++ ;
*t = '\0' ;
}

/* compares two strings s and t for equality */
int xstrcmp ( char *s, char *t )
{
while ( *s == *t )
{
if ( ! ( *s ) )
return 0 ;
s++ ;
t++ ;
}
return ( *s - *t ) ;
}


Simple program to assign values to many variables in one line in C language

Simple program to assign values to many variables in one line in C language, in one line multiple values in c .



#include

main()
{
int i,j,k;
k = (i = 4, j = 5);
printf("k = %d",k);
}


Program to Show example of the 2D array of int using pointers.in cpp

Program to Show example of the 2D array of int using pointers in C++ Language, two dimensional array with pointers.

#include
#include


main( )
{
clrscr();

constint r=3;
constint c=3;

int array[r][c],i,j;

cout<<"\n Enter the contents of the 2D array row by row are :\n"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cin>>*(*(array+i)+j);

cout<

Program to illustrate the functions returning pointers in c++


#include
#include

int *function();

main()
{
clrscr();

int *p;

p=function();

cout<<"\n Value return by the function returning pointer :"<<<"\t value of *p = "<<*p<

Functions with arguments but no return values in C

C Program to show Functions with arguments but no return values example Code is given below :


/* prototypes */
void printline (char c);
voidvalue (float, float, int);

main( )
{
float principal, inrate;
int period;

printf("Enter principal amount, interest");
printf(" rate, and period \n");
scanf("%f %f %d",&principal, &inrate, &period);
printline('Z');
value(principal,inrate,period);
printline('C');
}



void printline(char ch)
{
int i ;
for(i=1; i <= 52; i++)
printf("%c",ch);
printf("\n");
}
voidvalue(float p, float r, int n)
{
int year ;
float sum ;
sum = p ;
year = 1;
while(year <= n)
{
sum = sum * (1+r);
year = year +1;
}
printf("%f\t%f\t%d\t%f\n",p,r,n,sum);
}




Output

Enter principal amount, interest rate, and period
5000 0.12 5
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
5000.000000 0.120000 5 8811.708984
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC

Use of break statement in a program in C Language


main()
{
int m;
float x, sum, average;

printf("This program computes the average of a
set of numbers\n");
printf("Enter values one after another\n");
printf("Enter a NEGATIVE number at the end.\n\n");
sum = 0;
for (m = 1 ; m < = 1000 ; ++m)
{
scanf("%f", &x);
if (x < 0)
break;
sum += x ;
}

average = sum/(float)(m-1);
printf("\n");
printf("Number of values = %d\n", m-1);
printf("Sum = %f\n", sum);
printf("Average = %f\n", average);
}




Output

This program computes the average of a set of numbers
Enter values one after another
Enter a NEGATIVE number at the end.

21 23 24 22 26 22 -1

Number of values = 6
Sum = 138.000000
Average = 23.000000

Program to display series 2,4,16,256... using while loop in C


#include
#include

int main()
{
int n,x; // declare the variable longdouble i=2; //declare the variable of long double type
clrscr();
printf("Enter the number : ");
scanf("%d",&n);
printf("\n");
x=1;
while(x<=n) // loop will be execute till the value of x I less or equal n
{
printf("%.2Lf\n",i); // print the value of I upto 2 decimals only
x++;
i*=i;
}
getch();
return 0;
}




Output:

Enter the number : 4

2.00
4.00
16.00
256.00

Program to check whether a number is a member of given list or not in Artificial Intelligence In C


domains
list=integer*

predicates

findnum(integer,list)


clauses

findnum(X,[]):-
write("\nNumber Is Not Found").

findnum(X,[X|Tail]):-
write("\nNumber Is Found").

findnum(X,[Y|Tail]):-
findnum(X,Tail).




OUT PUT
=======

Goal: findnum(3,[1,2,3,4,5])

Number Is Found

Yes

----------------------------

Goal: findnum(6,[1,2,3,4,5])

Number Is Not Found

Yes

----------------------------

Goal: findnum(2,[1,2,2,1])

Number Is Found

Yes

PROGRAM THAT CHECK WHETHER THE GIVEN NUMBER IS PRIME IN PARALLEL in C Programming



#include "forkjoin.h"
#include "shared.h"

main()
{
int a[10],n;
int *flag,fect=1,nproc;
int i,pid,shmid;
printf("Enter the value of the no. n : ");
scanf("%d",&n);
printf("Enter the no. of process : ");
scanf("%d",&nproc);
flag=(int *) create_memory(sizeof(int)*nproc,&shmid);
for(i=0;i<=nproc;i++)
flag[i]=0;
pid=create_process(nproc);
for(i=pid+2;i<=n;i+=nproc)
{
if(n%i==0)
flag[pid]=1;
}
join_process(nproc,pid);
for(i=0;i<=nproc;i++)
{
if(flag[i]==1)
{
printf("Given no. is not prime\n");
exit(1);
}
}
printf("Given no. is prime\n",fect);
clear_memory(&shmid);
}

Size and Range of Basic Data Types in C Language

Size and Range of Basic Data Types

Data Type

Range of Values

char or signed char

-128 to 127

unsigned char

0 to 255

int or signed int

-32768 to 32767

unsigned int

0 to 65536

long int

-2,147483,648 to + ..47

float

3.4e-38 to 3.4e+38

double

1.7e-308 to 1.7e+308


Floating Point Types

  • They are stored in 32 bits in all 16 or 32 bit machines with 6 digits of precision
  • Double provides more accuracy than float
  • It uses 64 bits giving a precision of 14 digits
  • It represents the same data type as float but with double precision, hence double
  • To extend the precision further, one may use long double with 80 bits

Program of BISECTION METHOD in C Language


#include
#include
#include
#define ESP 0.001
#define F(x) (x)*(x)*(x) + (x)*(x) + (x) + 7
void main()
{
int i = 1;
float x0,x1,x2;
double f1,f2,f0,t;
clrscr();
printf("\nEnter the value of x0: ");
scanf("%f",&x0);

printf("\nEnter the value of x1: ");
scanf("%f",&x1);
printf("\n__________________________________________________________________\n");
printf("\niteration\t x0\t x1\t x2\t f0\t f1\t f2");
printf("\n___________________________________________________________________\n");
do
{
x2=(x0+x1)/2;
f0=F(x0);
f1=F(x1);
f2=F(x2);
printf("\n%d %f %f %f %lf %lf %lf", i, x0,x1,x2,f0,f1,f2);
if(f0*f2<0)
{
x1=x2;
}
else
{
x0=x2;
}
i++;
}while(fabs(f2)>ESP);
printf("\n__________________________________________________________\n");
printf("\n\nApp.root = %f",x2);
getch();
}




OUTPUT :


Enter the value of x0: -2

Enter the value of x0: -1

Enter the value of x1: -2

__________________________________________________________

x0 x1 x2 f0 f1 f2
__________________________________________________________

-1.000000 -2.000000 -1.500000 -5.000000 2.000000 -1.750000
-1.500000 -2.000000 -1.750000 -1.750000 2.000000 0.062500
-1.500000 -1.750000 -1.625000 -1.750000 0.062500 -0.859375
-1.625000 -1.750000 -1.687500 -0.859375 0.062500 -0.402344
-1.687500 -1.750000 -1.718750 -0.402344 0.062500 -0.170898
-1.718750 -1.750000 -1.734375 -0.170898 0.062500 -0.054443
-1.734375 -1.750000 -1.742188 -0.054443 0.062500 0.003967
-1.734375 -1.742188 -1.738281 -0.054443 0.003967 -0.025253
-1.738281 -1.742188 -1.740234 -0.025253 0.003967 -0.010647
-1.740234 -1.742188 -1.741211 -0.010647 0.003967 -0.003341
-1.741211 -1.742188 -1.741699 -0.003341 0.003967 0.000313
__________________________________________________________


App.root = -1.741699

C Language Program to evaluate fibonacci series and checking whether or not it is fibonacci number




#include
#include

void main()
{
longint a=0,b=1,i,sum,n,j=0,m;
clrscr();
printf(" Please Enter value for N: ");
scanf("%ld",&n);
printf("\n PLEASE ENTER THE CHECKING NO.: ");
scanf("%ld",&m);
printf("\n\n\t\t\t IT IS FEBONACCI SERIES \n ");
printf("%ld ,",a);
printf("%ld ,",b);
for(i=2;i<=n;i++)
{
sum = a+b;
a = b;
b = sum;
if(sum==m)
j=1;
printf("%ld ,",sum);
}
if(j==1)
printf("\n\n\t\t\t %ld IT IS FEBONNACI NUMBER. ",m);
else
printf("\n\n\t\t\t %ld IT IS NOT FEBONNACI NUMBER. ",m);
getch();
}



Output :


Please Enter valuefor N: 9

PLEASE ENTER THE CHECKING NO.: 21

IT IS FEBONACCI SERIES


0 ,1 ,1 ,2 ,3 ,5 ,8 ,13 ,21

21 IT IS FEBONNACI NUMBER.

program to print the factorial of first five elements of the fibonacci series



# include
# include


long fibonacci(long);
long factorial(long);

int main( )
{
clrscr( );

cout<<"\n The first five elements of the fibonacci series are : ";

for(int count_1=0;count_1<5;count_1++)
cout<
cout<<"\n\n The factorial first five elements of the fibonacci series are : ";

for(int count_2=0;count_2<5;count_2++)
cout<
getch( );
return 0;
}

/*************************************************************************//*************************************************************************///------------------------ Function Definitions -----------------------///*************************************************************************//*************************************************************************//*************************************************************************///-------------------------- fibonacci(long) --------------------------///*************************************************************************/long fibonacci(long number)
{
if(number==0 || number==1)
return number;

elsereturn fibonacci(number-1)+fibonacci(number-2);
}

/*************************************************************************///-------------------------- factorial(long) --------------------------///*************************************************************************/long factorial(long number)
{
int count=1;
int factorial=1;

if(number==0)
factorial=1;

else
{
do
{
factorial=factorial*count;
count++;
}
while(count<=number);
}

return factorial;
}



Program to display dimond shape triangle in C Programming


#include
#include
void main(){
int i,j,k,m,num,c;
clrscr();
printf("*****Logical Square*****\n");
printf("Enter number of lines u want : ");
scanf("%d",&num);
printf("\n\n");
//for top partfor(i=1;i<=num;i++)
{
c=num;
for(j=1;j<=(num-i)+2;j++)
putchar(' ');
for(k=1;k<=i;k++)
printf("%d",c--);
c++;
for(m=c;m<=num;m++)
{
printf("%d",++c);
}
printf("\n");
}

//for bottom partfor(i=1;i<=num;i++)
{
c=num;
for(j=num;j>=(num-i);j--)
putchar(' ');
for(k=c;k>i;k--)
printf("%d",k);
for(m=k;m<=c;m++)
printf("%d",m);
printf("\n");
}

getch();
}



Program to display numeric triangle for user given value in C++ Programming Language


#include
#include
void main()
{
int i,num,j;
clrscr();
cout<<"Enter number for corresponding output:- ";
cin>>num;
for(i=1;i<=num;i++)
{
for(j=1;j<=i;j++)
{
cout< }
cout<<"\n";
}
num--;
getch();
}





Program to display inputted string in ascending triangle and descending triangle in C





#include
#include
void main(){
char str[]="C Programming For You..";
int i,choice;
while(1){
clrscr();
printf("*****C Programming For You*****\n\n");
printf("1) Ascending style\n");
printf("2) Decending style\n");
printf("3) Exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
printf("\n\n");
switch(choice){
case 1: //Ascending Stylefor(i=0;str[i]!='\0';i++)
printf("%.*s\n",i,str);
printf("%.*s\n",i,str);
getch();
break;
case 2: //Decending Stylefor(i=0;str[i]!='\0';i++);
while(i>0){
printf("%.*s\n",i,str);
i--;
}
getch();
break;
case 3: exit(1);
default : printf("\n\nInvalid choice\n\n");
getch();
}
}
}


PROGRAM USNG WHILE LOOP FOR THE FIBONANSI SERIES AS SHOWN IN DESCRIPTION in C Programming

#include 
#include
#include


void main()
{
int n,i,j;
clrscr();
printf("Enter the No :- ");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
printf("\n");
for(j=1;j<=i;j++)
{
printf("%3d",i);
}
}
getch();
}



********
OUTPUT
********

Enter the No :- 7

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7

PROGRAM USNG WHILE LOOP FOR THE FIBONANSI SERIES IN TRIANGLE SERIES in C Language


#include
#include
#include
void main()
{
int n,i,j,m,k;
clrscr();
printf("Enter the No :- ");
scanf("%d",&n);
m=n;
for(i=1;i<=n;i++)
{
printf("\n\t");
for(k=1;k<=m;k++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%2d",i);
}
m--;
}
getch();
}



********
OUTPUT
********

Enter the No :- 6


1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6

Program to solve the Towers of Hanoi Problem (using Recursive Algorithm) in C++ Programming

# include 
# include

void move(constint n,constint fromTower,constint toTower,constint spareTower)
{
if(n>0)
{
move((n-1),fromTower,spareTower,toTower);
cout<<"\t Move the Top Disk from Tower-"<<<" to Tower-"<<<"\n";
move((n-1),spareTower,toTower,fromTower);
}
}
int main( )
{
clrscr( );
cout<<"\n\t ************** TOWERS OF HANOI **************\n"<<<"\t The Mystery of Towers of Hanoi is as follows : \n"<<<"\n\t *************************************************";
getch( );
return 0;
}

program to show Recursive Function operations of fibonacci series in C Programming

#include
#include
#include

void fibo(int *);
void fact(int *);
void rev(char *);

void main()
{
void input(void);
clrscr();
input();
// fibo(n);
getch();
}

void input(void)
{
FILE *fp;
int i,*no,ctr;
char * str,choice;

fp=fopen("input1.txt","w");
ctr=0;
while(1)
{
printf("\n DO YOU WANT TO ENTER AGAIN::");
fflush(stdin);
scanf("%c",&choice);
if(choice=='y')
{
printf("\n ENTER THE NO FOR FIBONACCII AND FACTORIAL::");
scanf("%d",no);
printf("\n ENTER THE STRING FOR REVESE::");
scanf("%s",str);
fprintf(fp,"%d %s\n",*no,str);
ctr++;
}
elsebreak;
}
fclose(fp);
fp=fopen("input1.txt","r");
for(i=1;i<=ctr;i++)
{
fscanf(fp,"%d %s",no,str);
fibo(no);
fact(no);
rev(str); // printf("\nfrom file %d %s",*no,str);
}
fclose(fp);
}
void fibo(int * n)
{
FILE * fout;
int fibonaccii(int*,int*,int*,int *,FILE *);
int fno1=0,sno1=1,*fno,*sno,*tno;
fno=&fno1;
sno=&sno1;
fout=fopen("output1.txt","a");
fprintf(fout,"\n FIBONACII SERICE OF %d",*n);
fprintf(fout,"\n%d\t%d",*fno,*sno);
fibonaccii(fno,sno,tno,n,fout);
fclose(fout);
}
int fibonaccii(int *fno,int *sno,int *tno,int * n,FILE * fout)
{
*tno=*fno+*sno;
*fno=*sno;
*sno=*tno;
if(*tno>*n)
return(0);
else
{
fprintf(fout,"\t%d",*tno);
fibonaccii(fno,sno,tno,n,fout);
}
return(0);
}

void fact(int * no1)
{
FILE * fout;
int factorial(int *,int,int *,FILE *);
int ctr1=1,*ctr,no;
ctr=&ctr1;
fout=fopen("output1.txt","a");
fprintf(fout,"\n FACTORIAL OF %d",*no1);
fprintf(fout,"\n%d",*ctr);
ctr1++;
ctr=&ctr1;
no=*no1;
factorial(no1,no,ctr,fout);
// fprintf(fout,"\n%d",*no1);
fclose(fout);
}

int factorial(int * no1,int no,int * ctr,FILE * fout)
{
if(no%*ctr==0)
{
fprintf(fout,"\t%d",*ctr);
no=no / *ctr;
factorial(no1,no,ctr,fout);
}
else
{
*ctr=*ctr+1;
if(no<=1)
{
fprintf(fout,"\t%d",*no1);
return(0);
}
factorial(no1,no,ctr,fout);
}
}
void rev(char * str)
{
FILE * fout;
int *ctr;
int reverse1(int * ,char *,FILE *);
fout=fopen("output1.txt","a");
fprintf(fout,"\n REVERSE OF %s IS::",str);
*ctr=strlen(str);
reverse1(ctr,str,fout);
fclose(fout);
}
int reverse1(int * ctr,char * str,FILE * fout)
{
char * rev=str+*ctr-1;
if(*ctr!=0)
{
fprintf(fout,"%c",*rev);
*ctr=*ctr-1;
reverse1(ctr,str,fout);
}
elsereturn(0);
}




**************************INPUT*************************************
5 jolly




**************************OUTPUT*************************************
FIBONACII SERICE OF 5
0 1 1 2 3 5
FACTORIAL OF 5
1 5
REVERSE OF jolly IS::ylloj

Program that implements circular queue as an array in C Language



#include
#include

#define MAX 10

void addq ( int *, int, int *, int * ) ;
int delq ( int *, int *, int * ) ;
void display ( int * ) ;

void main( )
{
int arr[MAX] ;
int i, front, rear ;

clrscr( ) ;

/* initialise data member */

front = rear = -1 ;
for ( i = 0 ; i < MAX ; i++ )
arr[i] = 0 ;

addq ( arr, 14, &front, &rear ) ;
addq ( arr, 22, &front, &rear ) ;
addq ( arr, 13, &front, &rear ) ;
addq ( arr, -6, &front, &rear ) ;
addq ( arr, 25, &front, &rear ) ;

printf ( "\nElements in the circular queue: " ) ;
display ( arr ) ;

i = delq ( arr, &front, &rear ) ;
printf ( "Item deleted: %d", i ) ;

i = delq ( arr, &front, &rear ) ;
printf ( "\nItem deleted: %d", i ) ;

printf ( "\nElements in the circular queue after deletion: " ) ;
display ( arr ) ;

addq ( arr, 21, &front, &rear ) ;
addq ( arr, 17, &front, &rear ) ;
addq ( arr, 18, &front, &rear ) ;
addq ( arr, 9, &front, &rear ) ;
addq ( arr, 20, &front, &rear ) ;

printf ( "Elements in the circular queue after addition: " ) ;
display ( arr ) ;

addq ( arr, 32, &front, &rear ) ;

printf ( "Elements in the circular queue after addition: " ) ;
display ( arr ) ;

getch( ) ;
}

/* adds an element to the queue */
void addq ( int *arr, int item, int *pfront, int *prear )
{
if ( ( *prear == MAX - 1 && *pfront == 0 ) || ( *prear + 1 == *pfront ) )
{
printf ( "\nQueue is full." ) ;
return ;
}

if ( *prear == MAX - 1 )
*prear = 0 ;
else
( *prear )++ ;

arr[*prear] = item ;

if ( *pfront == -1 )
*pfront = 0 ;
}

/* removes an element from the queue */
int delq ( int *arr, int *pfront, int *prear )
{
int data ;

if ( *pfront == -1 )
{
printf ( "\nQueue is empty." ) ;
return NULL ;
}

data = arr[*pfront] ;
arr[*pfront] = 0 ;

if ( *pfront == *prear )
{
*pfront = -1 ;
*prear = -1 ;
}
else
{
if ( *pfront == MAX - 1 )
*pfront = 0 ;
else
( *pfront )++ ;
}
return data ;
}

/* displays element in a queue */
void display ( int * arr )
{
int i ;
printf ( "\n" ) ;
for ( i = 0 ; i < MAX ; i++ )
printf ( "%d\t", arr[i] ) ;
printf ( "\n" ) ;
}


Program that performs add, edit, delete, display and search date from file in C


#include
#include

struct biodata
{
int recno,age;
char name[20],sex;
float salary;
};


void main(){
void addData(void);
void delData(void);
void showAll(void);
void showRecord(void);
void alterData(void);

char choice;
clrscr();

while(1)
{
clrscr();
textcolor(BLACK);
cprintf(" R E S U M E \r\n");
printf("\n\n*****ENTER YOUR CHOICE*****\n");
printf("1) ADD DATA\n");
printf("2) DELETE DATA\n");
printf("3) SHOW ALL\n");
printf("4) SHOW RECORD\n");
printf("5) ALTER DATA\n");
printf("6) Exit \n");
printf("Enter your choice : ");
fflush(stdin);
choice = getche();
switch(choice){
case'1' : //call add data
addData();
break;
case'2' : //call delete databreak;
case'3' : //call show all data
showAll();
break;
case'4' : //call show record
showRecord();
break;
case'5' : //call alter databreak;
case'6' :
case 27 : clrscr();
gotoxy(25,10);
_setcursortype(_NOCURSOR);
textcolor(LIGHTMAGENTA);
cprintf("THANKS FOR USING THIS SOFTWARE");
getch();
exit(1);
}
}

}


//Adding Record to Filevoid addData(){
FILE *fp;
struct biodata obj;
fp = fopen("biodata.txt","a+t");
clrscr();
printf("\n*****ADDING DATA*****\n");
printf("\nEnter Record No : ");
scanf("%d",&obj.recno);
printf("Enter Name : ");
fflush(stdin);
scanf("%s",obj.name);
printf("Enter age : ");
scanf("%d",&obj.age);
fflush(stdin);
printf("Enter Sex : ");
scanf("%c",&obj.sex);
printf("Enter Salary : ");
scanf("%f",&obj.salary);
fscanf(stdin,"%d %s %d %c %f",&obj.recno,obj.name,&obj.age,&obj.sex,&obj.salary);
fprintf(fp,"%d %s %d %c %f",obj.recno,obj.name,obj.age,obj.sex,obj.salary);
fclose(fp);
}

void showRecord(){
FILE *fp;
struct biodata obj;
int rec;
long pos;
fp = fopen("biodata.txt","r");
clrscr();
printf("\n*****SHOWING SPECIFIC RECORD*****\n");
printf("\nEnter Record No : ");
scanf("%d",&rec);
pos = rec * sizeof(obj);
fseek(fp,pos,SEEK_SET);
if(feof(fp)==0)
printf("\n\nNO DATA FOUND\n");
else{
fscanf(fp,"%d %s %d %c %f",&obj.recno,obj.name,&obj.age,&obj.sex,&obj.salary);
printf("\n\n\tRecord No : %d\n",obj.recno);
printf("\tName : %s\n",obj.name);
printf("\tAge : %d\n",obj.age);
printf("\tSex : %c\n",obj.sex);
printf("\tSalary : %f\n",obj.salary);
}
getch();
fclose(fp);
}


void showAll()
{
FILE *fp;
struct biodata obj;
int totrec,i;
fp = fopen("biodata.txt","r");
clrscr();
fseek(fp,0,SEEK_END);
totrec=ftell(fp)/sizeof(obj);
printf("\n*****SHOWING ALL RECORD*****\n");
printf("\n\nRecord_No\tName\t\tAge\tSex\tSalary\n\n");
printf("\n\n%d\n",totrec);
for(i=1;i<=totrec;i++){
fscanf(fp,"%d %s %d %c %f",&obj.recno,obj.name,&obj.age,&obj.sex,&obj.salary);
fprintf(stdout,"%-15d %-15s %-8d %-2c %10.2f\n",obj.recno,obj.name,obj.age,obj.sex,obj.salary);
}
getch();
fclose(fp);
}



Program to copy the contents of one array to another Without Shared Memory in C Language



# include
# include "forkjoin.h"int main()
{
int arr1[10]={1,1,1,1,1,1,1,1,1,1};
int arr2[10]={2,2,2,2,2,2,2,2,2,2};
int id;
int iCount;
/* Copy Array-1 to Array-2 Using 2 Processes */
id=process_fork(2);
if(id==0)
{
/* Parent Process */
for(iCount=0;iCount<10;iCount=iCount+2)
{
arr2[iCount]=arr1[iCount];
}
}
else
{
/* Child Process */for(iCount=1;iCount<10;iCount=iCount+2)
{
arr2[iCount]=arr1[iCount];
}
}

process_join(2,id);

/* Here Child Process is Completed and We have not made Array 2 Shared So Changes made by Child Process is not visible in Parent Process */

printf("\n Array 2 ... \n");

for(iCount=0;iCount<10;iCount++)
{
printf("arr2[%d] : %d\n",iCount,arr2[iCount]);
}

return 0;
}



Program to create histogram using threading in C language

#include
#include
#include

int a[100], size, N;
int min, max;
int grno;
float grsize;
int histo[50];

int start=0;
pthread_mutex_t lock;
pthread_cond_t cond;

void * work()
{
int i, temp;

pthread_mutex_lock(&lock);
if(start==0)
pthread_cond_wait(&cond, &lock);
pthread_mutex_unlock(&lock);

for(;;)
{
pthread_mutex_lock(&lock);
i=size-1;
size--;
pthread_mutex_unlock(&lock);

if(size<0) { pthread_mutex_lock(&lock); pthread_cond_wait(&cond, &lock); pthread_mutex_unlock(&lock); } if(i<0) break; if(min>a[i])
min=a[i];
if(max<0) break; temp=(float)((a[i]-min))/grsize; if(temp >(grno-1))
temp= grno-1;
histo[temp]++;
}
}

int main()
{
pthread_t id;
void * status;
int i, nthread;
int temp1, temp2;

pthread_mutex_init(&lock,0);
pthread_cond_init(&cond,0);

printf("Enter the size of the array :: ");
scanf("%d",&size);
N=size;

printf("\nEnter the %d elements of array\n",N);
for(i=0;i=0)
sleep(1);

grsize=(float)(max-min)/(float)grno;
size=N;

pthread_mutex_lock(&lock);
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&lock);


while(size>=0)
sleep(1);

pthread_join(id,status);

printf("\nThe histogram is as follows::\n");
temp1=min;
temp2=ceil(grsize);

for(i=0;i %d = %d\n",temp1, temp2,histo[i]);
temp1=temp2+1;
temp2+=ceil(grsize);
}
else
printf("For %d and up values = %d ",temp1, histo[i]);
}

pthread_mutex_destroy(&lock);

printf("\n");
return 0;
}







OUTPUT:

--------
Enter the size of the array :: 4

Enter the 4 elements of array
1
2
3
4

Enter the total number of groups required in histogram :: 4

Enter the total number of threads :: 3

The histogram isas follows::
For 1 -> 1 = 1
For 2 -> 2 = 1
For 3 -> 3 = 1
For 4 and up values = 1

Program to show two-dimensional arrays in C Programming


#define MAXGIRLS 4
#define MAXITEMS 3
main()
{
intvalue[MAXGIRLS][MAXITEMS];
int girl_total[MAXGIRLS] , item_total[MAXITEMS];
int i, j, grand_total;

/*.......READING OF VALUES AND COMPUTING girl_total ...*/


printf("Input data\n");
printf("Enter values, one at a time, row-wise\n\n");

for( i = 0 ; i < MAXGIRLS ; i++ ) { girl_total[i] = 0; for( j = 0 ; j < MAXITEMS ; j++ ) { scanf("%d", &value[i][j]); girl_total[i] = girl_total[i] + value[i][j]; } } /*.......COMPUTING item_total..........................*/ for( j = 0 ; j < MAXITEMS ; j++ ) { item_total[j] = 0; for( i =0 ; i < MAXGIRLS ; i++ ) item_total[j] = item_total[j] + value[i][j]; } /*.......COMPUTING grand_total.........................*/ grand_total = 0; for( i =0 ; i < MAXGIRLS ; i++ ) grand_total = grand_total + girl_total[i]; /* .......PRINTING OF RESULTS...........................*/ printf("\n GIRLS TOTALS\n\n"); for( i = 0 ; i < MAXGIRLS ; i++ ) printf("Salesgirl[%d] = %d\n", i+1, girl_total[i] ); printf("\n ITEM TOTALS\n\n"); for( j = 0 ; j < MAXITEMS ; j++ ) printf("Item[%d] = %d\n", j+1 , item_total[j] ); printf("\nGrand Total = %d\n", grand_total); }





Output :

Input data
Enter values, one at a time, row_wise

310 257 365
210 190 325
405 235 240
260 300 380

GIRLS TOTALS

Salesgirl[1] = 950
Salesgirl[2] = 725
Salesgirl[3] = 880
Salesgirl[4] = 940

ITEM TOTALS

Item[1] = 1185
Item[2] = 1000
Item[3] = 1310

Grand Total = 3495

Program that prints triangle of numbers in reverse pattern in C Programming


#include
#include
void main()
{
int i,n,j;
clrscr();
printf("\n Please Give The Value of N: ");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(j=i;j>=1;j--)
printf("%d ",j);
printf("\n");
}
getch();
}









****************************** OUTPUT *********************************

Please Give The Value of N: 5


5 4 3 2 1
4 3 2 1
3 2 1
2 1
1

Program to print triangle of numbers in reverse pattern in C Programming

#include
#include
void main()
{
int i,n,j;
clrscr();
printf("\n Please Give The Value of Number of *: ");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
printf("%d ",j);
printf("\n");
}
getch();
}




****************************** OUTPUT *********************************


Please Give The Value of N: 7


1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1


***********************************************************************

C Project to create a Notepad Editor Utility With Menu Bar In C



#include
#include
#include
#include
#include
#include
char far *vid=0xB8000000;
char a[50000],*str;
int track[2000],fp=1,lp=21,cl=1,p=0,t=0,eat;

/*This function writes a character into video memory.*/
writechar(char ch,int attr,int row,int col)
{
char far *v;
v=vid+row*160+col*2;
*v=ch;
v++;
*v=attr;
}/*End wrotechar*/
//-----------------------------------------------------------------------

/*This function draws a box of given attribute & size*/
drawbox(int r1,int c1,int r2,int c2,int attr)
{ int i,j;
for(i=r1;i<=r2;i++)
{ for(j=c1;j<=c2;j++)
writechar(' ',attr,i,j);
}
}/*End drawbox*/
//------------------------------------------------------------------------

/*This function draws the screen at starting*/
drawscreen()
{ drawbox(0,0,0,79,120);
drawbox(24,0,24,79,120);
drawbox(1,0,23,79,16);
doubleborder(1,0,23,79,31);
}/*End drawscreen*/
//------------------------------------------------------------------------

/*This function refreshes the screen*/
refreshscreen()
{ int r,c;
drawbox(2,1,22,78,16);

}/*End Refreshscreen*/
//-------------------------------------------------------------------------

/*This function draws the double border*/
doubleborder(int r1,int c1,int r2,int c2,int attr)
{ int r,c;
writechar(201,attr,r1,c1);
writechar(187,attr,r1,c2);
writechar(200,attr,r2,c1);
writechar(188,attr,r2,c2);


for(c=(c1+1);c<=(c2-1);c++)
writechar(205,attr,r1,c);

for(c=(c1+1);c<=(c2-1);c++)
writechar(205,attr,r2,c);

for(r=(r1+1);r<=(r2-1);r++)
writechar(186,attr,r,c1);

for(r=(r1+1);r<=(r2-1);r++)
writechar(186,attr,r,c2);

}/*End doubleborder*/
//---------------------------------------------------------------------------

/*This function draws border of given attribute & size*/
drawborder(int r1,int c1,int r2,int c2,int attr)
{ int r,c;

writechar(218,attr,r1,c1);
writechar(191,attr,r1,c2);
writechar(192,attr,r2,c1);
writechar(217,attr,r2,c2);

r=r1;
for(c=c1+1;c<=c2-1;c++)
writechar(196,attr,r,c);

r=r2;
for(c=c1+1;c<=c2-1;c++)
writechar(196,attr,r,c);

c=c1;
for(r=r1+1;r<=r2-1;r++)
writechar(179,attr,r,c);

c=c2;
for(r=r1+1;r<=r2-1;r++)
writechar(179,attr,r,c);

}/*End drawborder*/
//---------------------------------------------------------------------------

/*This function draws a line of given attribute*/
drawline(int r,int c1,int c2,int attr)
{ int i;
writechar(195,attr,r,c1);
writechar(180,attr,r,c2);
for(i=c1+1;i<=c2-1;i++)
writechar(196,attr,r,i);
}/*End drawline*/
//---------------------------------------------------------------------------

/*This function writes a string in given row & column with given attribute*/
writestring(char *p,int attr,int r,int c)
{ int l,i;
l=strlen(p);
for(i=0;i<=l;i++)
{ writechar(*(p+i),attr,r,c);
c++;
}
}/*End writestring*/
//---------------------------------------------------------------------------

/*This function saves the content of videomemory from r1,c1 to r2,c2
into main memory using char buffer*/
char *savemem(int r1,int c1,int r2,int c2,char *buff)
{ char far *v;
int i,j,k=0,size;

size=(r2-r1+1)*(c2-c1+1)*2;
buff=(char *)malloc(size);

for(i=r1;i<=r2;i++)
{ for(j=c1;j<=c2;j++)
{ v=(vid+i*160+j*2);
*(buff+k)=*v;
k++;
v++;
*(buff+k)=*v;
k++;
}
}
return(buff);
}/*End savememo*/
//---------------------------------------------------------------------------

/*This function restores the content of main memory into videomemory
from r1,c1 to r2,c2 using char buffer*/
restoremem(int r1,int c1,int r2,int c2,char *buff)
{ char far *v;
int i,j,k=0;


for(i=r1;i<=r2;i++)
{ for(j=c1;j<=c2;j++)
{ v=(vid+i*160+j*2);
*v=*(buff+k);
k++;
v++;
*v=*(buff+k);
k++;

}
}
free(buff);
}/*End restorememory*/
//---------------------------------------------------------------------------
savefile()
{ FILE *fp;
int i,j;
//char *str="c:\\tc\\ex1";
fp=fopen(str,"w");
if(fp==NULL)
{ return;
}
i=0;
j=track[0];
while(j= { fputc(a[j],fp);
j++;
if(j==track[i+1])
{ fputc('\n',fp);
i++;
}
}

fputc('\n',fp);
fclose(fp);
}
//------------------------------------------------------------------------
saveasfile()
{ FILE *fp;
int i,j,r1,c1;
char *buff;
buff=savemem(8,18,16,58,buff);
drawbox(8,18,16,58,120);
doubleborder(8,18,16,58,127);
drawbox(10,21,10,55,16);
setcuserpos(10,21);

gets(str);
restoremem(8,18,16,58,buff);

fp=fopen(str,"w");
if(fp==NULL)
{ return;
}
i=0;
j=0;
while(j= { fputc(a[j],fp);
j++;
if(j==track[i+1])
{ fputc('\n',fp);
i++;
}
}

fputc('\n',fp);
fclose(fp);

}
//------------------------------------------------------------------------
openfile()
{ FILE *fp;
int i,j,r1,c1;
char *buff,ch;
buff=savemem(8,18,16,58,buff);
drawbox(8,18,16,58,120);
doubleborder(8,18,16,58,127);
drawbox(10,21,10,55,16);
setcuserpos(10,21);

gets(str);
restoremem(8,18,16,58,buff);
//writestring(str,31,1,30);
fp=fopen(str,"r");
p=0;
t=0;
track[0]=0;
while(!feof(fp))
{ ch=fgetc(fp);
if(ch!='\n')
{ a[p]=ch;
p++;
}
else
{ track[t+1]=p;
t++;
}
}
fclose(fp);
fp=1;
lp=21;
cl=1;
display();
}
//------------------------------------------------------------------------
/*This function opens the filemenu*/
int Filemenu(char *buff)
{ int i,sc,ac,c,r,r1,c1,flag;
char *s[]={"New","Open","Save","Save as","Close","Print","Exit"};
char *s1[]={"Create a new file",
"Load an existing file from the disk",
"Save current file",
"Save current file with new name",
"Close current file",
"Prints currently loaded file to printer",
"Exit the editor"
};
union REGS inregs,outregs;

setcursersize(32,10); /*Hides the curser*/
buff=savemem(1,1,12,20,buff);
drawbox(1,1,11,18,120);
drawbox(2,19,12,20,7);
drawbox(12,3,12,18,7);
drawborder(1,2,11,17,112);

r=2; c=4;
for(i=0;i=<7;i++)
{ writestring(s[i],112,r,c);
r++;
if(i==4)
{ drawline(r,2,17,112);
r++;
}
if(i==5)
{ drawline(r,2,17,112);
r++;
}
}


sc=72; flag=2;
while(1)
{ if(sc==72) /*Up arrow*/
{ if(flag==1)
flag=7;
else
flag--;
}
else /*Down arrow*/
{ if(sc==80)
{ if(flag==7)
flag=1;
else
flag++;
}
else
flag=8; /*Right or Left arrow*/
}
switch(flag)
{ case 1:
drawbox(2,3,2,16,32);
writestring(s[0],32,2,4);
r=2; i=0;
writestring(s1[0],112,24,1);
break;

case 2:
drawbox(3,3,3,16,32);
writestring(s[1],32,3,4);
r=3; i=1;
writestring(s1[1],112,24,1);
break;
case 3:
drawbox(4,3,4,16,32);
writestring(s[2],32,4,4);
r=4; i=2;
writestring(s1[2],112,24,1);
break;
case 4:
drawbox(5,3,5,16,32);
writestring(s[3],32,5,4);
r=5; i=3;
writestring(s1[3],112,24,1);
break;
case 5:
drawbox(6,3,6,16,32);
writestring(s[4],32,6,4);
r=6; i=4;
writestring(s1[4],112,24,1);
break;
case 6:
drawbox(8,3,8,16,32);
writestring(s[5],32,8,4);
r=8; i=5;
writestring(s1[5],112,24,1);
break;
case 7:
drawbox(10,3,10,16,32);
writestring(s[6],32,10,4);
r=10; i=6;
writestring(s1[6],112,24,1);
break;
case 8:
if(sc==77) /*Right arrow return */
{ restoremem(1,1,12,20,buff); /*Scancode of Editmenu*/
return(18);
}
else
{ if(sc==75)
{ restoremem(1,1,12,20,buff); /*Left arrow return*/
/*Scancode of Helpmenu*/
return(35);
}
else
{ restoremem(1,1,12,20,buff);/*Return & Escape*/
return(sc);
}
}
}
getcharacter(&sc,&ac);

drawbox(r,3,r,16,112);
writestring(s[i],112,r,4);
drawbox(24,1,24,55,112);
if(ac==13)
{ switch(r)
{ case 2: restoremem(1,1,12,20,buff);
refreshscreen();
setcuserpos(2,1);
fp=1;
cl=1;
t=0;
lp=21;
p=0;
return(1);
case 3: readcurser(&r1,&c1);
restoremem(1,1,12,20,buff);
openfile();
setcuserpos(r1,c1);
return(1);

case 4: readcurser(&r1,&c1);
restoremem(1,1,12,20,buff);
savefile();
setcuserpos(r1,c1);
return(1);

case 5: readcurser(&r1,&c1);
restoremem(1,1,12,20,buff);
saveasfile();
setcuserpos(r1,c1);
return(1);
case 6:
case 8:
case 10:
return(0);
} }


}
}/*End filemenu*/
//-------------------------------------------------------------------------

/*This function opens the Editmenu*/
int Editmenu(char *buff)
{ int i,sc,ac,r,c,flag;
char *s[]={"Cut Ctrl+X","Copy Ctrl+C","Paste Ctrl+V","Clear Del"};
char *s1[]={"Delete selected text and copies it to buffer",
"Copies selected text to buffer",
"Insert buffer contents at current location",
"Delete selected text without copying it to buffer"
};
union REGS inregs,outregs;

setcursersize(32,10); /*Hide the cursor*/
buff=savemem(1,8,7,28,buff);
drawbox(1,8,6,26,120);
drawbox(2,27,7,28,7);
drawbox(7,10,7,26,7);
drawborder(1,9,6,25,112);

r=2; c=11;
for(i=0;i<4;i++)
{ writestring(s[i],112,r,c);
r++;
}

sc=72; flag=2;
while(1)
{ if(sc==72) /*Up arrow*/
{ if(flag==1)
flag=4;
else
flag--;
}
else
{ if(sc==80) /*Down arrow*/
{ if(flag==4)
flag=1;
else
flag++;
}
else
flag=5; /*Right or Left arrow*/
}
switch(flag)
{ case 1:
drawbox(2,10,2,24,32);
writestring(s[0],32,2,11);
r=2; i=0;
writestring(s1[0],112,24,1);
break;

case 2:
drawbox(3,10,3,24,32);
writestring(s[1],32,3,11);
r=3; i=1;
writestring(s1[1],112,24,1);
break;
case 3:
drawbox(4,10,4,24,32);
writestring(s[2],32,4,11);
r=4; i=2;
writestring(s1[2],112,24,1);
break;
case 4:
drawbox(5,10,5,24,32);
writestring(s[3],32,5,11);
r=5; i=3;
writestring(s1[3],112,24,1);
break;
case 5:
if(sc==77) /*Right arrow return */
{ restoremem(1,8,7,28,buff); /*Scancode of Searchmenu*/
return(31);
}
else
{ if(sc==75)
{ restoremem(1,8,7,28,buff);/*Left arrow return */
return(33); /*Scancode of Filemenu*/
}
else
{ restoremem(1,8,7,28,buff);
return(sc); /*Return & Escape*/
}
}
}
getcharacter(&sc,&ac);

drawbox(r,10,r,24,112);
writestring(s[i],112,r,11);
drawbox(24,1,24,55,112);
}

}/*End Editmenu*/
//------------------------------------------------------------------------

/*This function opens the Searchmenu*/
int Searchmenu(char *buff)
{ int i,sc,ac,r,c,flag;
char *s[]={ "Find",
"Repeat Last Find F3",
"Replace"
};
char *s1[]={"Find specified text",
"Find next occurence of text specified in previous search",
"Finds and changes specified text"
};
union REGS inregs,outregs;

setcursersize(32,10); /*Hide the cursor*/
buff=savemem(1,14,6,44,buff);
drawbox(1,14,5,42,120);
drawbox(2,43,6,44,7);
drawbox(6,16,6,42,7);
drawborder(1,15,5,41,112);

r=2; c=17;
for(i=0;i<3;i++)
{ writestring(s[i],112,r,c);
r++;
}

sc=72; flag=2;
while(1)
{ if(sc==72) /*Up arrow*/
{ if(flag==1)
flag=3;
else
flag--;
}
else
{ if(sc==80) /*Down arrow*/
{ if(flag==3)
flag=1;
else
flag++;
}
else
flag=4; /*Right or Left arrow*/
}
switch(flag)
{ case 1:
drawbox(2,16,2,40,32);
writestring(s[0],32,2,17);
r=2; i=0;
writestring(s1[0],112,24,1);
break;

case 2:
drawbox(3,16,3,40,32);
writestring(s[1],32,3,17);
r=3; i=1;
writestring(s1[1],112,24,1);
break;
case 3:
drawbox(4,16,4,40,32);
writestring(s[2],32,4,17);
r=4; i=2;
writestring(s1[2],112,24,1);
break;
case 4:
if(sc==77)
{ restoremem(1,14,6,44,buff); /*Right arrow return*/
return(47); /*Scancode of Viewmenu*/
}
else
{ if(sc==75)
{ restoremem(1,14,6,44,buff);/*Left arrow return*/
return(18); /*Scancode of Editmenu*/
}
else
{ restoremem(1,14,6,44,buff);
return(sc); /*Return & Escape*/
}
}
}
getcharacter(&sc,&ac);

drawbox(r,16,r,40,112);
writestring(s[i],112,r,17);
drawbox(24,1,24,56,112);
}

}/*End Searchmenu*/
//------------------------------------------------------------------------

/*This function opens the Viewmenu*/
int Viewmenu(char *buff)
{ int i,sc,ac,r,c,flag;
char *s[]={ "Split Window Ctrl+F6",
"Size Window Ctrl+F8",
"Close Window Ctrl+F4",
"1 UNTITLED Alt+1"
};
char *s1[]={"Open a second edit window",
"Resize the edit window",
"Close the second edit window",
"Selects a file to view"
};
union REGS inregs,outregs;

setcursersize(32,10); /*Hide the cursor*/
buff=savemem(1,22,8,47,buff);
drawbox(1,22,7,45,120);
drawbox(2,46,8,47,7);
drawbox(8,24,8,45,7);
drawborder(1,23,7,44,112);

r=2; c=24;
for(i=0;i<4;i++)
{ writestring(s[i],112,r,c);
r++;
if(i==2)
{ drawline(r,23,44,112);
r++;
}
}

sc=72; flag=2;
while(1)
{ if(sc==72) /*Up arrow*/
{ if(flag==1)
flag=4;
else
flag--;
}
else
{ if(sc==80) /*Down arrow*/
{ if(flag==4)
flag=1;
else
flag++;
}
else
flag=5; /*Right or Left arrow*/
}
switch(flag)
{ case 1:
drawbox(2,24,2,43,32);
writestring(s[0],32,2,24);
r=2; i=0;
writestring(s1[0],112,24,1);
break;

case 2:
drawbox(3,24,3,43,32);
writestring(s[1],32,3,24);
r=3; i=1;
writestring(s1[1],112,24,1);
break;
case 3:
drawbox(4,24,4,43,32);
writestring(s[2],32,4,24);
r=4; i=2;
writestring(s1[2],112,24,1);
break;
case 4:
drawbox(6,24,6,43,32);
writestring(s[3],32,6,24);
r=6; i=3;
writestring(s1[3],112,24,1);
break;
case 5:
if(sc==77)
{ restoremem(1,22,8,47,buff); /*Right arrow return*/
return(24); /*Scancode of Optionmenu*/
}
else
{ if(sc==75)
{ restoremem(1,22,8,47,buff); /*Left arrow return*/
return(31); /*Scancode of Searchmenu*/
}
else
{ restoremem(1,22,8,47,buff);
return(sc); /*Return & Escape*/
}
}
}
getcharacter(&sc,&ac);

drawbox(r,24,r,43,112);
writestring(s[i],112,r,24);
drawbox(24,1,24,55,112);
}


}/*End Viewmenu*/
//-----------------------------------------------------------------------

/*This function opens the Optionmenu*/
int Optionmenu(char *buff)
{ int i,sc,ac,r,c,flag;
char *s[]={"Settings","About"};
char *s1[]={"Change editor setting",
"Change editor screen color"
};
union REGS inregs,outregs;

setcursersize(32,10); /*Hide the cursor*/
buff=savemem(1,28,5,46,buff);
drawbox(1,28,4,44,120);
drawbox(2,45,5,46,7);
drawbox(5,30,5,44,7);
drawborder(1,29,4,43,112);

r=2; c=31;
for(i=0;i<2;i++)
{ writestring(s[i],112,r,31);
r++;
}

sc=72; flag=2;
while(1)
{ if(sc==72) /*Up arrow*/
{ if(flag==1)
flag=2;
else
flag--;
}
else
{ if(sc==80) /*Down arrow*/
{ if(flag==2)
flag=1;
else
flag++;
}
else
flag=3; /*Right or Left arrow*/
}
switch(flag)
{ case 1:
drawbox(2,30,2,42,32);
writestring(s[0],32,2,31);
r=2; i=0;
writestring(s1[0],112,24,1);
break;

case 2:
drawbox(3,30,3,42,32);
writestring(s[1],32,3,31);
r=3; i=1;
writestring(s1[1],112,24,1);
break;
case 3:
if(sc==77)
{ restoremem(1,28,5,46,buff); /*Right arrow return*/
return(35); /*Scancode of Helpmenu*/
}
else
{ if(sc==75)
{ restoremem(1,28,5,46,buff);/*Left arrow return*/
return(47); /*Scancode of Viewmenu*/
}
else
{ restoremem(1,28,5,46,buff);
return(sc); /*Return & Escape*/
}
}
}
getcharacter(&sc,&ac);

drawbox(r,30,r,42,112);
writestring(s[i],112,r,31);
drawbox(24,1,24,55,112);
}

}/*End Optionmenu*/
//------------------------------------------------------------------------

/*This function opens the Helpmenu*/
int Helpmenu(char *buff)
{ int i,sc,ac,r,c,flag;
char *s[]={"Commands","About"};
char *s1[]={"Help on edit commands",
"About help"
};
union REGS inregs,outregs;

setcursersize(32,10); /*Hide the cursor*/
buff=savemem(1,36,5,52,buff);
drawbox(1,36,4,50,120);
drawbox(1,51,5,52,7);
drawbox(5,36,5,50,7);
drawborder(1,37,4,49,112);

r=2; c=39;
for(i=0;i<2;i++)
{ writestring(s[i],112,r,c);
r++;
}

sc=72; flag=2;
while(1)
{ if(sc==72) /*Up arrow*/
{ if(flag==1)
flag=2;
else
flag--;
}
else
{ if(sc==80) /*Down arrow*/
{ if(flag==2)
flag=1;
else
flag++;
}
else
flag=3; /*Right or left arrow*/
}
switch(flag)
{ case 1:
drawbox(2,38,2,48,32);
writestring(s[0],32,2,39);
r=2; i=0;
writestring(s1[0],112,24,1);
break;

case 2:
drawbox(3,38,3,48,32);
writestring(s[1],32,3,39);
r=3; i=1;
writestring(s1[1],112,24,1);
break;
case 3:
if(sc==77)
{ restoremem(1,36,5,52,buff);/*Right arrow return*/
return(33); /*Scancode of Filemenu*/
}
else
{ if(sc==75)
{ restoremem(1,36,5,52,buff);/*Left arrow return*/
return(24); /*Scancode of Optionmenu*/
}
else
{ restoremem(1,36,5,52,buff);
return(sc); /*Return & Escape*/
}
}
}

getcharacter(&sc,&ac);

drawbox(r,38,r,48,112);
writestring(s[i],112,r,39);
drawbox(24,1,24,55,112);
}


}/*End Helpmenu*/
//-----------------------------------------------------------------------

/*This function is used to hides the curser*/
setcursersize(int x,int y)
{ union REGS inregs,outregs;
inregs.h.ah=0x01;
inregs.h.ch=x;
inregs.h.cl=y;
int86(0x10,&inregs,&outregs);
}/*End setcurser*/
//------------------------------------------------------------------------

/*This function sets the cursor at given position*/
setcuserpos(int a,int b)
{ union REGS inregs,outregs;
inregs.h.ah=0x02;
inregs.h.bh=0;
inregs.h.dh=a;
inregs.h.dl=b;
int86(0x10,&inregs,&outregs);
}/*End setcurserpos*/
//-------------------------------------------------------------------------

/*This function is used to read current position of curser*/
readcurser(int *p1,int *p2)
{
union REGS inregs,outregs;
inregs.h.ah=0x03;
inregs.h.bh=0;
int86(0x10,&inregs,&outregs);
*p1=outregs.h.dh;
*p2=outregs.h.dl;
}/*End readcurser*/
//------------------------------------------------------------------------

getcharacter(int *p1,int *p2)
{ union REGS inregs,outregs;
inregs.h.ah=0;
int86(0x16,&inregs,&outregs);
*p1=outregs.h.ah;
*p2=outregs.h.al;
}
//------------------------------------------------------------------------

display()
{ int i,j,c=1,k=2,m;

refreshscreen();
for(i=fp-1;i<=t;i++)
{ if(i==lp)
break;
for(j=track[i]+eat; j { if(j>=track[i+1] && i!=t)
break;
if(a[j]==9)
{ for(m=0;m<8;m++)
writechar(32,23,k,c);
}
else
{
writechar(a[j],23,k,c);
}
c++;
}
c=1;
k++;
}
}
//-------------------------------------------------------------------------
delete1(int temp)
{ int i;
memmove(a+temp,a+(temp+1),1000-(temp+1));
p--;
for(i=cl;i<=t;i++)
track[i]=track[i]-1;
display();
}
//------------------------------------------------------------------------
insert(int temp,int ac)
{ int i;
memmove(a+(temp+1),a+temp,1000-temp);
p++;
*(a+temp)=ac;
for(i=cl;i<=t;i++)
track[i]=track[i]+1;
}
//-------------------------------------------------------------------------
activescreen(int sc,int ac,int *r,int *c)
{ int r1,c1,k,temp,i;
readcurser(&r1,&c1);

switch(sc)
{ case 71: if(eat!=0)
{ eat=0;
display();
}
readcurser(&r1,&c1);
c1=1;
setcuserpos(r1,c1);
break;

case 79: if(t!=(cl-1))
{ readcurser(&r1,&c1);
c1=track[cl]-track[cl-1]+1;
if(c1<78)
{ if(eat!=0)
{ eat=0;
display();
}
setcuserpos(r1,c1);
}
else
{ eat=c1-78;
display();
readcurser(&r1,&c1);
c1=78;
setcuserpos(r1,c1);
}
}
else
{ readcurser(&r1,&c1);
c1=p-track[cl-1]+1;
if(c1<78)
setcuserpos(r1,c1);
else
{ eat=c1-78;
display();
readcurser(&r1,&c1);
c1=78;
setcuserpos(r1,c1);
}
}
break;

case 73: if(fp!=1)
{ if(lp>41)
{ fp=fp-20;
lp=lp-20;
cl=cl-20;
display();
}
else
{ fp=1;
lp=21;
display();
readcurser(&r1,&c1);
cl=r1-1;
}
}
break;

case 81: if((fp+20) { fp=fp+20;
lp=lp+20;
cl=cl+20;
display();
if((lp-1)>t)
{ cl=t+1;
readcurser(&r1,&c1);
r1=cl-fp+2;
setcuserpos(r1,c1);
}
}
break;
case 83: if((cl-1)==t || (track[cl-1]+(c1+eat))<=track[cl])
{ readcurser(&r1,&c1);
temp=track[cl-1]+(c1+eat)-1;
delete1(temp);
}
else
{ temp=track[cl-1]+(c1+eat)-track[cl]-1;
k=temp+track[cl];
for(i=track[cl];i insert(i,32);

memmove(track+cl,track+(cl+1),99-(cl+1));
t--;
display();
}
break;
case 14: readcurser(&r1,&c1);
temp=track[cl-1]+c1-2;
delete1(temp);
c1=c1-1;
setcuserpos(r1,c1);
break;
case 15: a[p]=ac;
p++;
writechar(ac,23,r1,c1);
c=c1+8;
break;
case 72: if(cl!=1)
{ if(cl>fp)
{ cl--;
readcurser(&r1,&c1);
r1--;
setcuserpos(r1,c1);
}
else
{ fp--;
lp--;
cl--;
display();
// setcuserpos(r1,c1);
}
}
break;

case 80: if(cl!=t+1)
{ if(cl { cl++;
readcurser(&r1,&c1);
r1++;
setcuserpos(r1,c1);
}
else
{ fp++;
lp++;
cl++;
display();
}
}
break;

case 77: if(c1<78)
{ c1++;
setcuserpos(r1,c1);
}
else
{ eat++;
display();
}
break;

case 75: if(c1!=1)
{ if((eat+c1)>78)
{ eat--;
display();
}
else
{ c1--;
setcuserpos(r1,c1);
}
}
break;

default: if(track[cl-1]+c1-1+eat==p)
{ if(ac==13)
{ t++;
track[t]=track[t-1]+(eat+c1)-1;
k=t;
t--;
if(cl==lp)
{ fp++;
lp++;
display();
readcurser(&r1,&c1);
c1=1;
setcuserpos(r1,c1);
}

cl++;
if(eat!=0)
{ eat=0;
display();
}
t=k;
if(lp<=21)
{ readcurser(&r1,&c1);
r1=r1+1;
c1=1;
setcuserpos(r1,c1);
}

}
else
{ if((eat+c1)>= 78)
{ a[p]=ac;
eat++;
setcuserpos(r1,78);
display();
p++;
}
else
{ writechar(ac,23,r1,c1);
a[p]=ac;
p++;
c1++;
setcuserpos(r1,c1);
}
}
}
else
{ readcurser(&r1,&c1);
if((track[cl-1]+c1+eat)>track[cl])
{ readcurser(&r1,&c1);
temp=track[cl-1]+(c1+eat)-1;
for(i=track[cl];i insert(i,32);
}
if(ac!=13)
{ readcurser(&r1,&c1);
if(eat==0)
{ temp=track[cl-1]+c1-1;
insert(temp,ac);
display();
c1=c1+1;
setcuserpos(r1,c1);
}
else
{ temp=track[cl-1]+(c1+eat)-1;
insert(temp,ac);
eat++;
display();
c1=78;
setcuserpos(r1,c1);
}
}
else
{ memmove(track+(cl+1),track+cl,99-cl);
*(track+cl)=track[cl-1]+(c1+eat)-1;
t++;
r1=r1+1;
c1=1;
display();
setcuserpos(r1,c1);
cl++;
}
}
}
*r=r1;
*c=c1;
}
main()
{ int i,r,c,sc,c1,ac,k,m,exit=0;
union REGS inregs,outregs;
char far *v;
char *buff,*s[]= { "File",
"Edit",
"Search",
"View",
"Option",
"Help",
"Line:",
"Col:"
};

clrscr();
drawscreen();
track[t]=0;


c=3;
writestring(s[0],112,0,c); /*String,Attribute,Row No,Column No*/
c=9;
writestring(s[1],112,0,c);
c=15;
writestring(s[2],112,0,c);
c=23;
writestring(s[3],112,0,c);
c=29;
writestring(s[4],112,0,c);
c=37;
writestring(s[5],112,0,c);

writechar(179,112,24,58);
writestring(s[6],112,24,61);
writestring(s[7],112,24,70);
c=1; r=2;
setcuserpos(2,1);
while(!exit)
{ m=77;
drawbox(24,75,24,77,120);
c1=eat+c;
while(c1>0)
{ k=c1%10;
writechar(k+48,112,24,m);
c1=c1/10;
m--;
}
m=69;
r=cl;
drawbox(24,66,24,69,120);
while(r>0)
{ k=r%10;
writechar(k+48,112,24,m);
r=r/10;
m--;
}


getcharacter(&sc,&ac);

while(sc!=1 && ac==0 && sc!=77 && sc!=75 && sc!=72 && sc!=15 && sc!=80 && sc!=71 && sc!=79 && sc!=73 && sc!=81 && sc!=83 && sc!=14)
{ switch(sc)
{ case 33: writestring(s[0],32,0,3);
sc = Filemenu(buff);
if(sc==0)
exit=1;
writestring(s[0],112,0,3);
break;

case 18: writestring(s[1],32,0,9);
sc = Editmenu(buff);
writestring(s[1],112,0,9);
break;

case 31: writestring(s[2],32,0,15);
sc = Searchmenu(buff);
writestring(s[2],112,0,15);
break;

case 47: writestring(s[3],32,0,23);
sc = Viewmenu(buff);
writestring(s[3],112,0,23);
break;

case 24: writestring(s[4],32,0,29);
sc = Optionmenu(buff);
writestring(s[4],112,0,29);
break;

case 35: writestring(s[5],32,0,37);
sc = Helpmenu(buff);
writestring(s[5],112,0,37);
break;
default:
sc=1;
}
setcursersize(5,6);
}
if(sc!=1)
activescreen(sc,ac,&r,&c);
}
}