[Noplug] Last Meeting Notes

Rod Page rpage at olympus.net
Fri Dec 16 09:50:15 PST 2005


Hi Jeffrey

The discussion centered on problems with wifi and Linux.
Duncan Dupree demonstrated a combination access point
and hot spot detector. I did a short presentation on a first
lesson in C based on a program to read data in from a
file. This is from an idea I have to teach a course in C
from studying a succession of 10 programs. This sort of
developed from my own review of C and attempting to
improve my own skills. I'm attaching the files I used and
if we could put them on the website people could play
with them on their own computer if they were further interested.
We don't have to do this if it's too much trouble.
I won't continue these lessons with this group but it
was what I've been working on lately and the group
agreed to be subjects for a little teaching experiment.

Thanks, Rod






Jeffrey Froman wrote:

>Hi Everyone,
>
>I was unfortunately unable to attend the last NOPLUG meeting. If anyone feels 
>like writing a sentence or two about the evening's events, please send it my 
>way so it can be added to the Meeting Log on the web site.
>
>Thank you,
>Jeffrey
>
>  
>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Ten.jpg
Type: image/jpeg
Size: 62520 bytes
Desc: not available
Url : http://lists.olympus.net/pipermail/noplug/attachments/20051216/a718c0f5/Ten.jpg
-------------- next part --------------
1
2
3
4
5
6
7
8
9
10
20
30
40
50
60
70
80
90
100
-------------- next part --------------
A non-text attachment was scrubbed...
Name: reader
Type: application/octet-stream
Size: 12703 bytes
Desc: not available
Url : http://lists.olympus.net/pipermail/noplug/attachments/20051216/a718c0f5/reader.obj
-------------- next part --------------
/* this is a comment, within comment delimitors */
/* the following lines include library function declarations */
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

/* we expect no more than MAX data in the file */
#define MAX 1000

/* declare our variables */
int n, i;
float data[MAX];
FILE *fptr;

main (int argc, char *argv[])
{
  if ((fptr = fopen (argv[1], "r")) == NULL)
    {
      printf ("No such file \n");
      exit (0);
    }

  n = 0;
  do
    {
      fscanf (fptr, "%f", &data[n]);
      n = n + 1;
    }
  while (!feof (fptr));
  /* we overcounted by incrementing n before testing for eof */
  /* but we're only testing i<n in the print statement */
  for (i = 0; i < n; i++)
    printf ("%f\n", data[i]);

  fclose (fptr);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: reader2
Type: application/octet-stream
Size: 13160 bytes
Desc: not available
Url : http://lists.olympus.net/pipermail/noplug/attachments/20051216/a718c0f5/reader2.obj
-------------- next part --------------
/* this is a comment, within comment delimitors */
/* the following lines include library function declarations */
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

/* we expect no more than MAX data in the file */
#define MAX 1000

/* declare our variables */
int n,i;
float data[MAX], ave, std;
FILE *fptr;

main(int argc, char *argv[])
{
    if((fptr=fopen(argv[1],"r"))==NULL){
    printf("No such file \n");
    exit(0);
    }

    n=0;
    do{
        fscanf(fptr,"%f", &data[n]);
    n=n+1;
         }while(!feof(fptr));
    ave=0.0;
    std=0.0;
  for(i=0;i<n;i++)
    {
         ave=ave+data[i];
    }

  ave=ave/(float)n;

  for(i=0;i<n;i++)
    {
     /* sum of the squares of the difference between the value and the average */    
         std=std+(data[i]-ave)*(data[i]-ave);
    }
  /* square root of the sum of the squares divided by the number of values */
  std=sqrt(std/(float)(n-1));

  printf("The average is %f, and standard deviation is %f\n", ave, std);

            fclose(fptr);
}
-------------- next part --------------
 


Reading files

 The following program uses the fscanf function to read data from a file, which in C is specified by a pointer to the memory location in which the file is stored. It uses the feof function to determine when the end of the file has been encountered. It expects the file to be a single column of data (floating point numbers) of unspecified length.
 

/* this is a comment, within comment delimitors */
/* the following lines include library function declarations */
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

/* we expect no more than MAX data in the file */
#define MAX 1000

/* declare our variables */
int n, i;
float data[MAX];
FILE *fptr;

main (int argc, char *argv[])
{
  if ((fptr = fopen (argv[1], "r")) == NULL)
    {
      printf ("No such file \n");
      exit (0);
    }

  n = 0;
  do
    {
      fscanf (fptr, "%f", &data[n]);
      n = n + 1;
    }
  while (!feof (fptr));
  /* we overcounted by incrementing n before testing for eof */
  /* but we are going to increment to i<n */
  for (i = 0; i < n; i++)
    printf ("%f\n", data[i]);

  fclose (fptr);
}


 

Notice that we read the data into the memory address of the array element data[n], not its value, but we print out the value put into that address on the very next line. Notice that we open the file read-only, and close it when we are done. Our program accepts argc command line options, stored as strings in an array argv[  ], the zeroth being the name of the program, and the first being the name of the file that the program will read. Our program reads data from the file until it reaches the end, storing how many data it read in variable n. It uses two types of loops; a do-while and a for loop.
 To compile this, save it as reader.c, and compile with gcc -o reader reader.c. Run it with ./reader datafile.
 


Now we make this do something useful. We add a little code to make the program find the average and variance of the data read in. We need to create two new variables, and add another loop.
 

/* this is a comment, within comment delimitors */
/* the following lines include library function declarations */
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

/* we expect no more than MAX data in the file */
#define MAX 1000

/* declare our variables */
int n,i;
float data[MAX], ave, std;
FILE *fptr;

main(int argc, char *argv[])
{
    if((fptr=fopen(argv[1],"r"))==NULL){
    printf("No such file \n");
    exit(0);
    }

    n=0;
    do{
        fscanf(fptr,"%f", &data[n]);
    n=n+1;
         }while(!feof(fptr));
    ave=0.0;
    std=0.0;
  for(i=0;i<n;i++)
    {
         ave=ave+data[i];
    }

  ave=ave/(float)n;

  for(i=0;i<n;i++)
    {
     /* sum of the squares of the difference between the value and the average */    
         std=std+(data[i]-ave)*(data[i]-ave);
    }
  /* square root of the sum of the squares divided by the number of values */
  std=sqrt(std/(float)(n-1));

  printf("The average is %f, and standard deviation is %f\n", ave, std);

            fclose(fptr);
}


This program recasts the integer data n as a float, and uses the sqrt function prototyped in math.h. That means we need to link the code to the math library libm, by compiling with gcc -o reader reader.c -lm.


More information about the Noplug mailing list