Tuesday 29 November 2011

Reading list for C.

1
Absolute beginners guide to C 2nd edition
Greg Perry
SAMS publishing


2
C / C++ annotated archives
A. Friedman, L. Klander, M. Michaelis, H. Schildt
Osborne


3
C by example special edition
Greg Perry
QUE


4
C for dummies volume one
Dan Gookin
IDG books


5
C for dummies volume two
Dan Gookin
IDG books


6
C programming in easy steps 3rd edition
Mike McGrath



7
Learning to program in C
N. Kantaris
Bernard Babani publishing LTD


8
Numerical recipes in C
W.H. Press, S.A. Teukolsky, W.T. Vetterling, B.P. Flannery
Cambridge


9
Programming in C third edition
Stephen G. Kochan
Developer's library


10
Programming in Objective-C 2.0 second edition
Stephen G. Kochan
Developer's library


11
Simple C
Jim McGregor, Richard McGregor, Alan Watt
Addison-Wesley


12
The C programming language second edition
Brain W. Kernighan and Dennis M. Ritchie
Prentice Hall Software series


13
The complete reference C fourth edition
Herbert Schildt
Osborne

Monday 7 November 2011

A countdown timer written in C

/***************************************************************************
 *   Copyright (C) 2009 by David Ronald Smith,,,   *
 *   david@debian   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#define DELAY 1000000000
int hcount;
int mcount;
int scount;
void delay(void);

int main(void)
{
printf("input the hours ");
scanf("%d", &hcount);
printf("input the minutes ");
scanf("%d", &mcount);
printf("input the seconds ");
scanf("%d", &scount);

for(;;)
{
scount--;

if(scount==-1) {
scount =59;
mcount--;
}

if(mcount== -1){
mcount=59;
hcount--;
}

if(hcount==0 && mcount==0 && scount==0){
break;
}
delay();
printf("\n%i\n", scount);
printf("\n%i\n", mcount);
printf("\n%i\n", hcount);
}
return 0;
}

void delay(void)
{
long int t;
for(t=1; t<DELAY; ++t);
}

Saturday 5 November 2011

A lottery program written in C

 #include<stdlib.h>
 #include<time.h>
 #include<stdio.h>

 void main()
 {
 int numbers[49];
 int ball, run, i, j, k, flag;
 run = 0;
 ball = 0;
 for(k=1; k<=49; k++)
  numbers[k] = '\0';
 printf("Here are your lottery numbers.\n");
 do
 {
 srand((unsigned)time(NULL));
 ball = rand()%49 + 1;
 flag =0;
 if(ball == numbers[ball])
 {
 flag = 1;
 }
 if(flag==0)
 {
 numbers[ball] = ball;
 run = run + 1;
 printf("%i\n",ball);
 }
 } while(run <= 6);
 }

Tuesday 9 August 2011

Scanate implemented in a C program for Linux and Windows

This computer program written in C is an implementation of the idea Scanate. Scanate generates random grid references on a map (eastings and northings) and its name is short for scanning coordinates. The coordinates are then used to see if remote viewing of thes location is possible. Remote viewing is a form of extra sensory perception.
For more information look up Ingo Swann.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void seedrnd(void);

void main()
{
int easting, northing;
seedrnd();
printf("Enter the number of Eastings on your map.\n");
scanf("%i", &easting);
printf("Eastings \n\n");
printf("%d\n", (rand() % easting)+1);
printf("Enter the number of Northings on your map.\n");
scanf("%i", &northing);
printf("Northings \n\n");
printf("%d\n",(rand() %northing)+1);
}

void seedrnd(void)
{
srand((unsigned)time(NULL));
}

A fortune telling with dice program written in C for LInux and Windows

#include<time.h>
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i;
srand((unsigned)time(NULL));
i=rand()%6;
printf("\nThis is an oracle using a green, a white and a red dice.\n");
printf("\nLove situation.");
if(i==0)
{
printf("\nGreen six.");
printf("\nFruit tree.");
printf("\nPossessions, property, presentation.");
}
else
if(i==1)
{
printf("\nGreen five.");
printf("\nGoblet.");
printf("\nDrink, dining dibauchery.");
}
else
if(i==2)
{
printf("\nGreen four.");
printf("\nKey");
printf("\nOpportunity, doors opening or closing.");
}
else if(i==3)
{
printf("\nGreen three.");
printf("\nLadder.");
printf("\nAmbition, enthusiasm, new projects.");
}
else if(i==4)
{
printf("\nGreen two.");
printf("\nLightning.");
printf("\nThoughtlessness, wrong end of the stick.");
}
else
{
printf("\nGreen one.");
printf("\nSnail.");
printf("\nHealth, think on gaurd.");
}
printf("\n");
i=rand()%6;
printf("\nFinancial situation.");
if(i==0)
{
printf("\nWhite six.");
printf("\nCrossroads.");
printf("\nHidden surprises, strangers, outsiders.");
}
else if(i==1)
{
printf("\nWhite five.");
printf("\nCat.");
printf("\nHome marriage, family circle.");
}
else if(i==2)
{
printf("\nWhite four.");
printf("\nKnife.");
printf("\nTreachery, deceit, intrigues.");
}
else if(i==3)
{
printf("\nWhite three.");
printf("\nHeart.");
printf("\nLove, affection, friendship, fondness, passion, desire.");
}
else if(i==4)
{
printf("\nWhite two.");
printf("\nStork.");
printf("\nNew beginnings, fertility, ideas, inventiveness.");
}
else
{
printf("\nWhite one.");
printf("\nSun.");
printf("\nAmbition, drive, success.");
}
printf("\n");
i=rand()%6;
printf("\nGeneral situation.");
if(i==0)
{
printf("\nRed six.");
printf("\nBat.");
printf("\nA passing shadow which can hide good and evil.");
printf("\nWarning, omen, prediction, prophecy\n.");
}
else if(i==1)
{
printf("\nRed five.");
printf("\nBeetle.");
printf("\nDetermination, purposefulness, specific ambition, targets.\n");
}
else if(i==2)
{
printf("\nRed four.");
printf("\nHorseshoe.");
printf("\nGood fortune, success, laughter.\n");
}
else if(i==3)
{
printf("\nRed three.");
printf("\nBoat.");
printf("\nOutward journeys, travel, expeditions.\n");
}
else if(i==4)
{
printf("\nRed two.");
printf("\nSkull.");
printf("\nChange by transformation, sudden unexpected change.\n");
}
else
{
printf("\nRed one.");
printf("\nWeb.");
printf("\nConfusion, illusion, traps, pitfalls.\n");
}
return 0;
}

A runes program written in C for Linux and Windows.

#include<time.h>
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char ch;
int i;
srand((unsigned)time(NULL));
i=rand()%42;
if(i==0)
{
printf("Beorc. \n");
 printf("Birth, fertility, or marriage.\n");
}
else
if(i==1)
{
printf("Beorc, reversed. \n");
 printf("Miscarriage, abortion, or divorce.\n");
}
else
if(i==2)
{
 printf("Eolh. \n");
 printf("A new hobby, job or friendship. \n");
}
else
if(i==3)
{
printf("Eolh reversed. \n");
 printf("An offer which should be refused or a person to be avoided. \n");
}
else
if(i==4)
{
 printf("Os. \n");
 printf("Wisdom imparted to the enquirer by an older person or \n");
 printf("one more knowledgeable in the ways of the world or spirit. \n");
}
else
if(i==5)
{
printf("Os reversed.\n ");
 printf("Bad counsel from one who means harm.\n");
}
else
if(i==6)
{
 printf("Rad. \n");
 printf("Indicates a journey that will bring happiness.\n");
}
else
if(i==7)
{
printf("Rad reversed. \n");
printf("Travel difficulties. An unexpected trip to see sick relatives or friends.\n");
}
else
if(i==8)
{
printf("Ken. \n");
 printf("An illuminating experience which shows the way forward.\n");
}
else
if(i==9)
{
 printf("Ken reversed. \n");
 printf("Confusion, bad judgement and obscured vision.\n");
}
else
if(i==10)
{
 printf("Nyd. \n");
 printf("Caution is required if the enquirer is to succeed in his or her aims.\n");
 printf("The forces of destiny are working.\n");
}
else
if(i==11)
{
 printf("Nyd reversed. \n");
 printf("Sadness caused by those closest to the enquirer.\n");
}
else
if(i==12)
{
printf("Wynn. \n");
 printf("Hasty judgement or impatience lead to disaster.\n");
}
else
if(i==13)
{
printf("Wynn reversed. \n");
printf("Loss of affection. Self sacrifice.\n");
}
else
if(i==14)
{
 printf("Haegl. \n");
printf("Plans are delayed because the time is not right for their execution.\n");
}
else
if(i==15)
{
 printf("Haegl reversed. \n");
 printf("Delays and setbacks beyond the enquirers control.\n");
}
else
if(i==16)
{
 printf("Gyfu. \n");
 printf("Marriage or the cementing of a long standing realtionship.\n");
 printf("A gift which indicates shared love.\n");
}
else
if(i==17)
{
printf("Gyfu reversed. \n");
 printf("Sadness caused by those closest to the enquirer.\n");
}
else
if(i==18)
{
 printf("Ger. \n");
 printf("A warning not to speak ill of others or judge them before all the facts.\n");
 printf("are known.\n");
}
else
if(i==19)
{
 printf("Ger reversed. \n");
 printf("Harsh words spoken hotly may end a good friendship.\n");
}
else
if(i==20)
{
printf("Eoh. \n");
 printf("The solving of a matter which has been dormant for some time.\n");
 printf("News of a friend from the past.\n");
}
else
if(i==21)
{
 printf("Eoh reversed. \n");
 printf("A return to the old conflicts and situations which are a detriment to\n");
 printf("the present.\n");
}
else
if(i==22)
{
 printf("Pear. \n");
 printf("Unexpected material gains from a legacy or secret gift.\n");
}
else
if(i==23)
{
printf("Pear reversed. \n");
 printf("A secret is revealed which may harm the good name of the enquirer.\n");
}
else
if(i==24)
{
 printf("Feoh. \n ");
 printf("A new career opportunity is on the horizon.\n");
}
else
if(i==25)
{
printf("Feoh reversed. \n");
 printf("Material loss which cannot be recovered.\n");
}
else
if(i==26)
{
 printf("Tyr. \n");
printf("A passionate love affair is indicated. It will be a strong and lasting one.\n");
}
else
if(i==27)
{
 printf("Tyr reversed. \n");
 printf("The enquirer should be aware of casting his or her affections around for\n");
 printf("heartbreak will result.\n");
}
else
if (i==28)
{
 printf("Mannaz. \n");
 printf("Contacts with the outside world which will enlarge the perspective of the enquirer.\n");
}
else
if(i==29)
{
 printf("Mannaz reversed. \n");
 printf("A period of self-isolation or detachment from life so that new ideas can be nurtured.\n");
}
else
if(i==30)
{
 printf("Lagu. \n");
 printf("A revelation received through intuition, sudden inspiration or a dream.\n");
}
else
if(i==31)
{
 printf("Lagu reversed. \n");
printf("A woman will disrupt the life of the enquirer and bring many complications.\n");
}
else
if(i==32)
{
printf("Ing. \n");
 printf("News of a stranger from abroad.\n");
}
else
if(i==33)
{
 printf("Ing reversed. \n");
 printf("A wasted telephone call or a letter which will never be answered.\n");
}
else
if(i==34)
{
printf("Odal. \n");
printf("A will or a legacy bringing benefits to the enquirer or their family.\n");
}
else
if(i==35)
{
printf("Odal reversed. \n");
printf("An older person who, if left to their own selfish devices, could \n");
printf("become a long standing burden.\n");
}
else
if(i==36)
{
 printf("Sigel. \n");
 printf("Health, wise direction.\n");
}
else
if(i==37)
{
 printf("Porn. \n");
printf("An unexpected decision, good news from afar, unexpected luck.\n");
}
else
if(i==38)
{
 printf("Porn reversed. \n");
 printf("Beware making any hasty decisions, bad news.\n");
}
else
if(i==39)
{
printf("Dueg. \n");
printf("Contacts with the outside world which will enlarge your perspective.\n");
}
else
{
printf("Dueg revewsed. \n");
printf("A time of detachment so that new ideas can be nurtured.\n");
}
}

Saturday 6 August 2011

A database written in C for Linux and Windows

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100

struct addr {
char name[40];
char street[40];
char district[40];
char town[40];
char county[40];
char country[40];
char postcode[40];
char telno[40];
char email[40];
}addr_list[MAX];

struct addr temp;
int counter;
void init_list(void), enter(void);
void delete(void), list(void);
void load(void), save(void), sort(void);
void loadbackup(void), outputthree(void);
void outputtwo(void), search(void);
void readLine(char buffer[]);
void swap(int u, int v);
int main(void)
{
int choice;
char s[80];
counter = 0;
init_list();
for(;;) {
printf("\n");
printf("1.  Create a record\n");
printf("2.  Delete a record\n");
printf("3.  List the file\n");
printf("4.  Save the file\n");
printf("5.  Load the file\n");
printf("6.  Sort the file\n");
printf("7.  Search\n");
printf("8.  Save to a back up file\n");
printf("9.  Load from a backup file\n");
printf("10. Output to a text file for printing or import into wp\n");
printf("11. Quit\n");
do {
printf("\nEnter your choice: ");
readLine(s);
/*gets(s);*/
choice=atoi(s);
/*scanf("%i", &choice);*/
}while(choice < 0 && choice > 11);
switch(choice) {
case 1: enter();
break;
case 2: delete();
break;
case 3: list();
break;
case 4: save();
break;
case 5: load();
break;
case 6: sort();
break;
case 7: search();
break;
case 8: outputtwo();
break;
case 9: loadbackup();
break;
case 10: outputthree();
break;
case 11: exit(0);
}
}
return 0;
}

void init_list(void)
{
register int t;

for(t=0; t<MAX; ++t) addr_list[t].name[0] ='\0';
}

void enter(void)
{
char s[40];
int i;
for(i=1; i<MAX; i++)
if(!*addr_list[i].name)
break;
if(i==MAX)
{
printf("Subscriber array full\n");
return;
}
printf("Enter information on new customer\n");

printf("Enter name: ");
readLine(addr_list[i].name);

printf("Enter street: ");
readLine(addr_list[i].street);

printf("Enter district / parish: ");
readLine(addr_list[i].district);

printf("Enter town / city: ");
readLine(addr_list[i].town);

printf("Enter county: ");
readLine(addr_list[i].county);

printf("Enter country: ");
readLine(addr_list[i].country);

printf("Enter postcode: ");
readLine(addr_list[i].postcode);

printf("Enter telephone number: ");
readLine(addr_list[i].telno);

printf("Enter email address: ");
readLine(addr_list[i].email);
counter=counter+1;
}



void delete(void)
{
int t;
int u;
printf("enter a record number #: ");
scanf("%i", &t);
for(u=t; u<=counter; u++){
strncpy(addr_list[u].name, addr_list[u+1].name, 40);
strncpy(addr_list[u].street, addr_list[u+1].street, 40);
strncpy(addr_list[u].district, addr_list[u+1].district, 40);
strncpy(addr_list[u].town, addr_list[u+1].town, 40);
strncpy(addr_list[u].county, addr_list[u+1].county, 40);
strncpy(addr_list[u].country, addr_list[u+1].country, 40);
strncpy(addr_list[u].postcode, addr_list[u+1].postcode, 40);
strncpy(addr_list[u].telno, addr_list[u+1].telno, 40);
strncpy(addr_list[u].email, addr_list[u+1].email, 40);
}
addr_list[counter].name[0] = '\0';
addr_list[counter].street[0] = '\0';
addr_list[counter].district[0] = '\0';
addr_list[counter].town[0] = '\0';
addr_list[counter].county[0] = '\0';
addr_list[counter].country[0] = '\0';
addr_list[counter].postcode[0] = '\0';
addr_list[counter].telno[0] = '\0';
addr_list[counter].email[0] = '\0';
counter = counter -1;
}

void list(void)
{
register int t;
int count;
count=0;
for(t=1; t<=counter; t++) {
count = count +1;
printf("Record number: %i\n",count);
printf("%s\n", addr_list[t].name);
printf("%s\n", addr_list[t].street);
printf("%s\n", addr_list[t].district);
printf("%s\n", addr_list[t].town);
printf("%s\n", addr_list[t].county);
printf("%s\n", addr_list[t].country);
printf("%s\n", addr_list[t].postcode);
printf("%s\n", addr_list[t].telno);
printf("%s\n", addr_list[t].email);
printf("\n");
}
}

void sort(void)
{
register int u;
register int v;
for(u=1; u<counter; u++)
for (v=u+1; v<=counter; v++){
if( strcmp(addr_list[u].name, addr_list[v].name) >0){
swap(u, v);
}
}
}

void swap(int u, int v)
{
strncpy(temp.name, addr_list[u].name, 40);
strncpy(temp.street, addr_list[u].street, 40);
strncpy(temp.district, addr_list[u].district, 40);
strncpy(temp.town, addr_list[u].town, 40);
strncpy(temp.county, addr_list[u].county, 40);
strncpy(temp.country, addr_list[u].country, 40);
strncpy(temp.postcode,addr_list[u].postcode, 40);
strncpy(temp.telno, addr_list[u].telno, 40);
strncpy(temp.email, addr_list[u].email, 40);
strncpy(addr_list[u].name, addr_list[v].name, 40);
strncpy(addr_list[u].street, addr_list[v].street, 40);
strncpy(addr_list[u].district, addr_list[v].district, 40);
strncpy(addr_list[u].town, addr_list[v].town, 40);
strncpy(addr_list[u].county, addr_list[v].county, 40);
strncpy(addr_list[u].country, addr_list[v].country, 40);
strncpy(addr_list[u].postcode, addr_list[v].postcode, 40);
strncpy(addr_list[u].telno, addr_list[v].telno, 40);
strncpy(addr_list[u].email, addr_list[v].email, 40);
strncpy(addr_list[v].name, temp.name, 40);
strncpy(addr_list[v].street, temp.street, 40);
strncpy(addr_list[v].district, temp.district, 40);
strncpy(addr_list[v].town, temp.town, 40);
strncpy(addr_list[v].county, temp.county, 40);
strncpy(addr_list[v].country, temp.country, 40);
strncpy(addr_list[v].postcode, temp.postcode, 40);
strncpy(addr_list[v].telno, temp.telno, 40);
strncpy(addr_list[v].email, temp.email, 40);

}
void save(void)
{
FILE *fp;
register int i;

if((fp=fopen("maillist", "wb"))==NULL) {
printf("Cannot open file.\n");
return;
}

for(i=1; i<MAX; i++)
if(*addr_list[i].name)
if(fwrite(&addr_list[i],
sizeof(struct addr), 1, fp)!=1)
printf("File write error.\n");

fclose(fp);
}

void loadbackup(void)
{
FILE *fp;
register int i;
char filename[30];
printf("Enter a file name.\n");
scanf("%s",filename);
printf("\n");

if((fp=fopen(filename, "rb"))==NULL) {
printf("Cannot open file,\n");
return;
}

init_list();
for(i=1;  i<MAX; i++){
if(fread(&addr_list[i],
sizeof(struct addr), 1, fp)!=1) {
if(feof(fp)) break;
printf("File read error.\n");
}
counter=counter+1;
}
fclose(fp);
}



void load(void)
{
FILE *fp;
register int i;

if((fp=fopen("maillist", "rb"))==NULL) {
printf("Cannot open file,\n");
return;
}

init_list();
for(i=1;  i<MAX; i++){
if(fread(&addr_list[i],
sizeof(struct addr), 1, fp)!=1) {
if(feof(fp)) break;
printf("File read error.\n");
}
counter=counter +1;
}
fclose(fp);
}


void outputthree(void)
{
int t;
int count;
FILE *stream;
char filename[30];
count = 0;
printf("Enter a file name.\n");
scanf("%s",filename);
printf("\n");
stream = fopen(filename, "w+");
for(t=1; t<MAX; t++) {
if(addr_list[t].name[0]) {
count = count +1;
fprintf(stream,"%i\n",count);
fprintf(stream,"%s\n", addr_list[t].name);
fprintf(stream,"%s\n", addr_list[t].street);
fprintf(stream,"%s\n", addr_list[t].district);
fprintf(stream,"%s\n", addr_list[t].town);
fprintf(stream,"%s\n", addr_list[t].county);
fprintf(stream,"%s\n", addr_list[t].country);
fprintf(stream,"%s\n", addr_list[t].postcode);
fprintf(stream,"%s\n", addr_list[t].telno);
fprintf(stream,"%s\n", addr_list[t].email);
fprintf(stream,"\n");
}
}
fclose(stream);
return;
}



void outputtwo(void)
{
FILE *fp;
register int i;
char filename[30];
printf("Enter a file name.\n");
scanf("%s",filename);
printf("\n");
if((fp=fopen(filename, "wb"))==NULL) {
printf("Cannot open file.\n");
return;
}

for(i=1; i<MAX; i++)
if(*addr_list[i].name)
if(fwrite(&addr_list[i],
sizeof(struct addr), 1, fp)!=1)
printf("File write error.\n");

fclose(fp);
}

void search(void)
{
int i;
int count;
char s[40];
count=0;
printf("\nEnter a name.\n");
readLine(s);
for(i=1;i<MAX;i++)
{
if(addr_list[i].name[0]) {
count = count +1;
}

if (strcmp(s, addr_list[i].name)==0)
{
printf("\nRecord number: %i\n", count);
printf("\nName: %s\n",addr_list[i].name);
printf("\nStreet: %s\n",addr_list[i].street);
printf("\nDistrict: %s\n",addr_list[i].district);
printf("\nTown: %s\n",addr_list[i].town);
printf("\nCounty: %s\n",addr_list[i].county);
printf("\nCountry: %s\n",addr_list[i].country);
printf("\nPostcode:%s\n",addr_list[i].postcode);
printf("\nTelephone number: %s\n",addr_list[i].telno);
printf("\nEmail address: %s\n",addr_list[i].email);
printf("\n");
}
}
}

void readLine(char buffer[])
{
char character;
int i=0;
do
{
character=getchar();
buffer[i]=character;
++i;
}
while(character !='\n');
buffer[i-1]='\0';
}