Ooops, I sent this directly to Mr. Chuong rather than to the Dev-C++ mailing
list. It would be nicer if the reply address with mailing list items was
automatically back to the mailing list. Here's what I said:
I have corrected a few errors that I can see, but I've not tested the
outcome so I'll leave that to you. Note that passing as an array and passing
as a pointer equate to the same thing.
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
//--------------------------------------------------------------------------
--
----
// array passed as pointer
float sum_array( float *A, int len )
{
float s = 0;
for( int i = 0; i < len; ++i )
s += A[i];
return s;
}
// array passed as array of unspecified length
float average_array( float A[], int len )
{
return sum_array( A, len )/len;
}
//--------------------------------------------------------------------------
--
----
void main( int argc, char **argv )
{
if( argc != 2 ) /* there will always be at least 1 in argc as the name
of the executable counts as an argument */
{
cerr << "Usage: " << argv[0] << " number" << endl;
cerr <<"int argc: "<<argc;
getchar();
exit(0);
}
int n = atoi( argv[1] ); /* you don't check that a valid result was
given, but I assume that you will be giving correct commands */
//float H[n];
/* this is illegal in all but the latest C standard (C99), you cannot
allocate an array with a variable size. You should use dynamic allocation,
as follows */
float *H = new float[n]; /* I'm not absolutely sure of syntax off the
top of my head, so check it works.*/
for( int i = 1; i <= n; ++i )
H[i-1] = 1.0/static_cast<float>(i); /* this is a better way of making
the result float, i.e. use a float literal and cast the integer into a
float, although it would probably have been an implicit cast anyway */
cout << "harmonic number " << n << ": " << sum_array( H, n ) << endl;
cout << "average 1/i up to " << n << ": " << average_array( H, n ) <<
endl;
system("PAUSE");
delete[] H; // don't forget to delete array when finished with it
exit(0);
}
I hope those little pointers help you. I'll leave it to you to debug what I
have written. I don't think I have made any mistakes (maybe some typing
errors) but I didn't have time to check.
Jason.
----- Original Message -----
From: <Cuo...@ao...>
To: <dev...@li...>
Sent: Thursday, November 15, 2001 6:41 AM
Subject: [Dev-C++] Using Command Line to Execute program
> Please show me step-by-step how to use DevC++ to run a program through
> Command Line method fpr the following program. Thank you!!!!!!!!!!!!!!
>
>
> #include <iostream.h>
> #include <stdlib.h>
> #include <stdio.h>
>
>
//--------------------------------------------------------------------------
--
>
> ----
> // array passed as pointer
>
> float sum_array( float *A, int len )
> {
> float s = 0;
> for( int i = 0; i < len; ++i )
> s += A[i];
> return s;
> }
>
> // array passed as array of unspecified length
>
> float average_array( float A[], int len )
> {
> return sum_array( A, len )/len;
> }
>
>
//--------------------------------------------------------------------------
--
>
> ----
> void main( int argc, char **argv )
> {
> if( argc != 1 )
> {
> cerr << "Usage: " << argv[0] << " number" << endl;
> cerr <<"int argc: "<<argc;
> getchar();
> exit(0);
> }
>
> int n = atoi( argv[1] );
>
> float H[n];
>
> for( int i = 1; i <= n; ++i )
> H[i-1] = 1/float(i);
>
> cout << "harmonic number " << n << ": " << sum_array( H, n ) <<
endl;
> cout << "average 1/i up to " << n << ": " << average_array( H, n ) <<
> endl;
>
> exit(0);
> }
>
> //--------------------------------
> ------------------------------------------------
>
>
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|