M
Mark A. Nicolosi
I've been trying to learn C for quite a while. But I've had trouble
with the
lack of good quality online text (some of it's alright). But I finally
bought
a book on C, Practical C. I like it alot. I'm pretty familiar with the
C basics,
because I've been trying to learn it for a while. But anyone, I wrote
a program
to average any number of arguments given at the command line. I
thought it might
be useful to get feedback about my coding style or anything I've done
wrong. It
compile just fine with gcc (with -Wall -ansi -pedantic). Anyways, here
it is:
/* average.c - Average several numbers together. */
/* Copyright (C) 2003 Mark A. Nicolosi */
#include <stdio.h>
#include <malloc.h>
float average (int *numbers, int how_many);
int main (int argc, char **argv)
{
int how_many; /* Number of numbers to average. */
int i; /* Index for for loop. */
int *numbers; /* Array of numbers to be averaged. */
float answer; /* Variable to hold the answer. */
how_many = argc - 1; /* Every argument after argv[0] should be
number to average */
if (how_many == 0) /* If TRUE then not called with any arguments.
*/
{
printf ("Usage: %s <num1> <num2> ...\n", argv[0]);
return 1;
}
/* Allocate enough memory to hold the array of numbers to be
averaged. */
numbers = malloc (sizeof (int) * how_many);
/* Transfer the string arrays in argv[i + 1] to an array of
integars. */
for (i = 0; i < how_many; i++)
{
sscanf (argv[i + 1], "%d", &numbers);
}
/* Now that we've got an array of integars to average, average 'em.
*/
answer = average (numbers, how_many);
printf ("%g\n", answer);
/* Free the array of numbers. */
free (numbers);
return 0;
}
float average (int *numbers, int how_many)
{
float answer = 0.0; /* Variable to hold the answer. */
int i; /* Index for for loop. */
/* Loop to add all the numbers in the number[] array. */
for (i = 0; i < how_many; i++)
{
answer = answer + numbers;
}
/* Now divide by the number of numbers. */
answer = answer / how_many;
return answer;
}
I copy and pasted that in to links, so I'm not sure if it'll look
alight
Thanks.
with the
lack of good quality online text (some of it's alright). But I finally
bought
a book on C, Practical C. I like it alot. I'm pretty familiar with the
C basics,
because I've been trying to learn it for a while. But anyone, I wrote
a program
to average any number of arguments given at the command line. I
thought it might
be useful to get feedback about my coding style or anything I've done
wrong. It
compile just fine with gcc (with -Wall -ansi -pedantic). Anyways, here
it is:
/* average.c - Average several numbers together. */
/* Copyright (C) 2003 Mark A. Nicolosi */
#include <stdio.h>
#include <malloc.h>
float average (int *numbers, int how_many);
int main (int argc, char **argv)
{
int how_many; /* Number of numbers to average. */
int i; /* Index for for loop. */
int *numbers; /* Array of numbers to be averaged. */
float answer; /* Variable to hold the answer. */
how_many = argc - 1; /* Every argument after argv[0] should be
number to average */
if (how_many == 0) /* If TRUE then not called with any arguments.
*/
{
printf ("Usage: %s <num1> <num2> ...\n", argv[0]);
return 1;
}
/* Allocate enough memory to hold the array of numbers to be
averaged. */
numbers = malloc (sizeof (int) * how_many);
/* Transfer the string arrays in argv[i + 1] to an array of
integars. */
for (i = 0; i < how_many; i++)
{
sscanf (argv[i + 1], "%d", &numbers);
}
/* Now that we've got an array of integars to average, average 'em.
*/
answer = average (numbers, how_many);
printf ("%g\n", answer);
/* Free the array of numbers. */
free (numbers);
return 0;
}
float average (int *numbers, int how_many)
{
float answer = 0.0; /* Variable to hold the answer. */
int i; /* Index for for loop. */
/* Loop to add all the numbers in the number[] array. */
for (i = 0; i < how_many; i++)
{
answer = answer + numbers;
}
/* Now divide by the number of numbers. */
answer = answer / how_many;
return answer;
}
I copy and pasted that in to links, so I'm not sure if it'll look
alight
Thanks.