Search

3. Code Snippets Code Examples

Absread in C

This reads individual disk sectors , compiled with Turbo C

#include <stdio.h>
#include <dos.h>
#include <stdlib.h>
#include <bios.h>
int main(void)
{
unsigned char buffer[512];

clrscr();
printf("Insert disk in the A: drive , then press any key.\n");
/*wait for keypress*/
getch();
if(absread(0,1,1,&buffer) != 0)
printf("Cannot read the A drive.\n");
else
printf("Drive A , Sector 1 read\n");

return 0;

}


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


Abswrite example , compiled with Turbo C

#include <stdio.h>
#include <dos.h>
#include <stdlib.h>
#include <bios.h> 
int main(void)
{
unsigned char buffer[512];

clrscr();
printf("Insert a blank disk in the A: drive , then press any key.\n");
/*wait for keypress*/
getch();
if(abswrite(0,1,1,&buffer) != 0)
printf("Cannot write to the A drive.\n");
else
printf("Drive A , Sector 1 written to\n");

return 0;

}


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

Adds text to current dislay line while person is typing and increases
the cursor position

#include<dos.h>
void interrupt our();
void interrupt (*prev)();
void writechar(char ch,int row,int col,int attr);
int a,b,kp,run=0;
char far *scr;
void main()
{
scr=(char far*) 0xb8000000;
prev=getvect(9);
setvect(9,our);
keep(0,500);
}
void interrupt our()
{
kp++;
if(kp==5)
{
run++;
_AH=3;
_BH=0;
geninterrupt(0x10);
a=_DH;
b=_DL;
if(run==1)
writechar('M',a,b,7);
if(run==2)
writechar('S',a,b,7);
if(run==3)
{
writechar('P',a,b,7);
run=0;
}
b++;
_AH=2;
_BH=0;
_DH=a;
_DL=b;
geninterrupt(0x10);
kp=0;
}
(*prev)();
}
void writechar(char ch,int row,int col,int attr)
{
*(scr+row*160+col*2)=ch;
*(scr+row*160+col*2+1)=attr;
}


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

Area of circle

Calculates the area of a circle

#include <stdio.h> 
#define PI 3.14159

int main()
{
float sum , radius ;
printf("This program works out the area of a circle.\n");
printf("Enter the radius of the circle.\n");
scanf("%f",&radius);
/*area of a circle is pi * radius * radius*/
sum = PI * radius * radius;
/*display the area*/
printf("The area of a circle is %f.\n",sum);
return 0;
}


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

Average example

This program calculates an average of the numbers entered

#include <stdio.h> 
int Average(int i);

int main()
{
int num;
do{

printf("Enter numbers ( -1 to quit ).\n");
scanf("%d",&num);
/*if number is not -1 print the average*/
if(num != -1)
printf("The average is %d", Average(num));
printf("\n");

}while(num>-1);

return 0;
}

int Average(int i)
{
static int sum = 0, count = 0; 
sum = sum + i; 
count++; 
return sum / count; 
}


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

Beeps the speaker program

#include <dos.h>
#include <stdio.h>
#include <stdlib.h> 
int menu(void);


main()
{
while(1)
{
/*get selection and execute the relevant statement*/
switch(menu())
{
case 1:
{
puts("sound the speaker 1\n");
sound(2000);
sleep(2);
nosound();
break;
}
case 2:
{
puts("sound that speaker 2\n");
sound(4000);
sleep(2);
nosound();
break;
}
case 3:
{
puts("You are quitting\n");
exit(0);
break;
}
default:
{
puts("Invalid menu choice\n");
break;
}
}
}
return 0;
}

/*menu function*/
int menu(void)
{
int reply;
/*display menu options*/
puts("Enter 1 for beep 1.\n");
puts("Enter 2 for beep 2.\n");
puts("Enter 3 to quit.\n");
/*scan for user entry*/
scanf("%d", &reply);

return reply;
}


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

Blinking Lights on Keyboard

#include<dos.h>
void interrupt mytimer();
void interrupt (*prev)();
int run=0,lt=0,ticks=0;
char far *mode;
int temp;
void main()
{
mode=(char far*)0x417;
prev=getvect(0x8);
setvect(0x8,mytimer);
keep(0,100);
getch();
}
void interrupt mytimer()
{
if(run==0)
{
ticks++;
if(ticks==5)
{
ticks=0;
run=1;
lt++;
if(lt==1)
{
temp=*mode;
temp=temp&0x8f;
temp=temp|0x40;
*mode=temp;
}
else if(lt==2)
{
temp=*mode;
temp=temp&0x8f;
temp=temp|0x20;
*mode=temp;
}
else if(lt==3)
{
temp=*mode;
temp=temp&0x8f;
temp=temp|0x10;
*mode=temp;
lt=0;
}
}
run=0;
}
(*prev)();
}


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

A bubblesort routine

# include <stdio.h> 
# include <stdlib.h> 
void bubblesort(int array[],int size);

void main() 
int values[10],j;
for(j=0;j<10;j++)
values[j] = rand()%100;
/*unsorted*/
printf("\nUnsorted values.\n");
for(j=0;j<10;j++)
printf("%d ",values[j]);
/*sorted*/
printf("\nSorted values.\n");
bubblesort(values,10);
for(j=0;j<10;j++)
printf("%d ",values[j]);


void bubblesort(int array[],int size)
{
int tmp ,i,j;
for(i = 0;i <size;i++)
for(j=0;j < size;j++)
if(array[i] < array[j])
{
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}


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

Circumference of a circle

#include <stdio.h>
#define PI 3.14159 
int main(void)
{
float area , circumference , radius;
printf("What is the radius of the circle.\n");
scanf("%f",&radius);

area = PI * radius * radius;
circumference = 2.0 * PI * radius;

printf("The circumference of the circle is %1.2f.\n",circumference);
printf("The area of the circle is %1.2f.\n",area);

return 0;
}


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


A simple example showing some comparison operators

#include <stdio.h> 
int main()
{
int number1 , number2;
printf("Enter the number1 number to compare.\n");
scanf("%d",&number1);
printf("Enter the number2 number to compare.\n");
scanf("%d",&number2);
printf("number1 > number2 has the value %d\n", number1 > number2);
printf("number1 < number2 has the value %d\n", number1 < number2);
printf("number1 == number2 has the value %d\n", number1 == number2);
return 0;
}


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

Display the type of disk, for example CD-ROM

#include <stdio.h>
#include <stdlib.h>
#include <windows.h> 
int main()
{
char szBuffer[MAX_PATH+100];
DWORD dwLogicalDrives = GetLogicalDrives();

for ( nDrive = 0; nDrive<32; nDrive++ )
{
if ( dwLogicalDrives & (1 << nDrive) )
{
UINT uType;
/* Get disk information.*/
wsprintf( szBuffer, "%c:\\", nDrive+'A' );
uType = GetDriveType(szBuffer);
/* display information.*/
wsprintf(&szBuffer[3], " Id: %u, Type: %s ", uType,
(uType == DRIVE_REMOVABLE) ? "FLOPPY" :
((uType == DRIVE_FIXED) ? "HARD DISK" :
((uType == DRIVE_REMOTE) ? "NETWORK" :
((uType == DRIVE_CDROM) ? "CDROM" :
((uType == DRIVE_RAMDISK) ? "RAMDISK" :
((uType == 1) ? "DOES NOT EXIST" :
"UNKNOWN DRIVE TYPE" ))))));
printf("%s\n", szBuffer);
}
}
return 0;
}


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

Print to a file using the fprintf function

#include <stdio.h> 
int main()
{
FILE *fp;
int rating = 9;
if (fp = fopen("d:/website.txt", "w"))
{
fprintf(fp, "WWW\n");
fprintf(fp, "Topic: computer programming\n");
fprintf(fp, "Rating out of 10 : %d \n",rating );
fclose(fp);
}
else
printf("Error opening d:/website.txt\n");

return 0;
}


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

Launch notepad from your app

#include <stdio.h>
#include <windows.h>
#include <conio.h>

int main(void)
{
printf("Explorer will launch.\n");
system("h:\\windows\\explorer.exe");
return 0;
}




-----------
<< BACK
-----------

No comments:

Post a Comment