Tuesday 31 May 2016

New ebook on simple C listings for indexed sequential file databases

I have just published a new ebook on simple C listings for indexed sequential file databases. The book and the C source code / C listings contained in it are open source. The link to the website to download it for free is

Open source ebook on databases written in C.


A website that has free Windows programs to download including working examples of C programs has the following link

A website of free programs including C databases.

Monday 23 May 2016

A barchart plotter for integers written in C. Data saved to file and barchart written to txt file for import into word processor

This program is compiled in Linux with the gcc command
gcc programname.c -lm
The -lm means that the maths funtions are included during compilation.
From the terminal using the directory containg the file / compiler output the program can b run using ./a.out

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#define max 100
int number, ymax;
int ploty[max];
void createbar(void), updatebar(void), savedata(void), loadbar(void), plot(void), savebartxt(void);
int main(void)
{
int choice;
for(;;) {
printf("\n");
printf("1. Create a barchart.\n");
printf("2. Save barchart to a .txt file for printing.\n");
printf("3. Save data to to a data file to plot again later\n");
printf("4. Append data to a barchart.\n");
printf("5. Load a bar chart from data file.\n");
printf("6. Quit\n");
do {
printf("\nEnter your choice: ");
scanf("%i", &choice);
}while(choice < 0 && choice > 5);
switch(choice) {
case 1: createbar();
break;
case 2: savebartxt();
break;
case 3: savedata();
break;
case 4: updatebar();
break;
case 5: loadbar();
break;
case 6: exit(0);
}
}
return 0;
}
void createbar(void)
{
  int  y_data;
  int i, j, k;
for(k=1; k<=max; k++)
ploty[k] = '\0';
printf("Enter the number of y points to plot.\n");
printf("Maximum number of points is 100 or type 111 to exit.\n");
scanf("%i", &number);
if(number == 111) exit(0);
printf("Enter the maximum / largest yvalue.\n");
scanf("%i", &ymax);
for(j=1; j<=number; j++)
{
 printf("Enter y datum %i\n", j);
 scanf("%i", &y_data);
 ploty[j]=y_data;
}
plot();

}

void plot(void)
{
int xdatum, ydatum;
int i;
int yvalue;
float yscale, ybar, maxsize = 60.0;
printf( "y_data---------->\n");
  for(i=1; i<=number; i++)
    {
  yscale = maxsize/ymax;
ybar = yscale* ploty[i];
yvalue = round(ybar);  
for(xdatum =1; xdatum<=3; xdatum++){
for(ydatum=1; ydatum<=yvalue; ydatum++)
      printf("*");
 printf("\n");
}
printf("\n");
}
}


void updatebar(void)
{
  int xdatum, ydatum;
  int  y_data;
  int  i, j, l;
   FILE *prnt;
for(l=1; l<=max; l++)
ploty[l] = '\0';
 printf("Enter the number of y points to plot.\n");
     scanf("%i", &number);
for(j=1; j<=number; j++)
{
 printf("Enter y datum %i\n", j);
 scanf("%i", &y_data);
 ploty[j]=y_data;
}
printf( "y_data---------->\n");
  for(i=1; i<=number; i++)
    {
 
for(xdatum =1; xdatum<=3; xdatum++){
   for(ydatum=1; ydatum<=ploty[i]; ydatum++)
      printf("*");
 printf("\n");
}
printf("\n");
}

FILE *fp;
char filename[30];
printf("Enter a file name .txt to add bars to an already existing chart.\n");
scanf("%s",filename);
printf("\n");
fp = fopen(filename, "a+");
if((fp=fopen(filename, "a+"))==NULL) {
printf("Cannot open file.\n");
return;
}

time_t lt;
struct tm *ptr;
lt = time(NULL);
ptr = localtime(&lt);
fprintf(fp,"%s\n",asctime(ptr));

for(i=1; i<=number; i++)
    {
for(xdatum =1; xdatum<=3; xdatum++){
   for(ydatum=1; ydatum<=ploty[i]; ydatum++)
      fprintf(fp,"*");
 fprintf(fp,"\n");
}
fprintf(fp,"\n");
}
fclose(fp);

FILE *fp2;
char filename2[30];
int k;
printf("Enter a file name .dat to add data to plot at a later date.\n");
scanf("%s",filename2);
printf("\n");
fp2 = fopen(filename2, "a+");
if((fp2=fopen(filename2, "a+"))==NULL) {
printf("Cannot open file.\n");
return;
}
for(k = 1; k<= number; k++)
{
fprintf(fp2,"%i", ploty[k]);
fprintf(fp2,"\n");
}
fclose(fp2);
return;

}

void savedata(void)
{
FILE *fp;
char filename[30];
int i;
printf("Enter a file name .dat to contain data to plot at a later.\n");
printf("This can be printed using a text editor.\n");
scanf("%s",filename);
printf("\n");
if((fp=fopen(filename, "w"))==NULL) {
printf("Cannot open file.\n");
return;
}
fprintf(fp,"%i\n", ymax);
for(i = 1; i<= number; i++)
{
fprintf(fp,"%i", ploty[i]);
fprintf(fp,"\n");
}
fclose(fp);
return;
}

void loadbar(void)
{
int i;
char filename[30];
FILE *fp;
number = 0;
for(i=1; i<=max; i++)
ploty[i] ='\0';
printf("Enter a file name .dat containing data to plot.\n");
scanf("%s",filename);
printf("\n");
if((fp=fopen(filename, "r"))==NULL) {
printf("Cannot open file.\n");
return;
}
fscanf(fp,"%i", &ymax);
while(!feof(fp))
{
number = number + 1;
fscanf(fp, "%i", &ploty[number]);
}
fclose(fp);
plot();
}

void savebartxt(void)
{
FILE *fp;
char filename[30];
int i;
int xdatum, ydatum;
int yvalue;
float yscale, ybar, maxsize = 60.0;
printf("Enter a file .txt name to contain the bar chart.\n");
printf("This can be printed using a text editor. \n");
scanf("%s",filename);
printf("\n");
if((fp=fopen(filename, "w"))==NULL) {
printf("Cannot open file.\n");
return;
}
time_t lt;
struct tm *ptr;
lt = time(NULL);
ptr = localtime(&lt);
fprintf(fp,"%s\n",asctime(ptr));

fprintf(fp, "y_data---------->\n");
  for(i=1; i<=number; i++)
    {
 yscale = maxsize/ymax;
ybar = yscale* ploty[i];
yvalue = round(ybar);  

for(xdatum =1; xdatum<=3; xdatum++){
   for(ydatum=1; ydatum<=yvalue; ydatum++)
      fprintf(fp,"*");
 fprintf(fp,"\n");
}
fprintf(fp,"\n");
}
fclose(fp);
return;
}

f = cos(x) + cos(y) implemented in C

This program is compiled in Linux using the gcc command
gcc programname.c -lm
The -lm means that the maths funtions are included during compilation.

#include<stdio.h>
#include<math.h>

main()
{
const space = ' ', screenheight = 60, ycentre = 30, screenwidth = 70;
const xcentre = 35;
float xscale =7.0 , yscale = 7.0;
char screen[screenwidth][screenheight];
float xs, ys, interval, fvalue;
int period, row, col, ft, x, y, startx, starty, stopx, stopy;
char point;

/*insert spaces*/
for(col=0; col<=screenwidth; col++)
for(row=0; row<=screenheight; row++)
screen[row][col] = space;

startx = -xcentre;
stopx = xcentre;
starty =  -ycentre;
stopy = ycentre;
for(y = starty; y <= stopy; y++)
for(x = startx; x <= stopx; x++)
{
xs = x/xscale;
ys = y/yscale;
printf("xs =%f\n",xs);
printf("xy=%f\n",ys);
fvalue = cos(xs) + cos(ys);
ft=round((fvalue + 2) * 2.5);
printf("%i\n", ft);
switch(ft)
{
case 0: point = 'A';
break;
case 1: point = ' ';
break;
case 2: point = 'B';
break;
case 3: point = ' ';
break;
case 4: point = 'C';
break;
case 5: point = ' ';
break;
case 6: point = 'D';
break;
case 7: point = ' ';
break;
case 8: point = 'E';
break;
case 9: point = ' ';
break;
case 10: point = 'F';
}
screen[x+xcentre][y+ycentre] = point;
}
/*draw graph*/
for(row =0; row <= screenheight; row++)
{
for(col=0; col<=screenwidth; col++)
printf("%c",screen[row][col]);
printf("\n");
}

}

Sine wave plot implemented in C

This program is compiled using the gcc command in Linux
gcc programname.c -lm
The -lm means the mathe functions are included during compilation.

#include<stdio.h>
#include<math.h>
#define PI 3.1415926


main()
{
const increment = 15, scale = 30, centre = 40;
int angle;
int i, j;
angle = 0;
do
{
j = centre-scale*sin(angle*PI/180);
for(i=1; i<=j; i++)
printf(" ");
printf("sine\n");
angle = angle + increment;
}while(angle <= 720);
return 0;
}