General Protection Exception

D

dmoran21

I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.

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

int main()
{
/* Integer declarations */
const int exitcode = -1;
float arraylength = 0;
float angfreq = 0;
float* time = (float*)NULL;
float* output = (float*)NULL;
float ysinesum = 0;
float ycossum = 0;
float sumy = 0;
float acoef = 0;
float bcoef = 0;
float ccoef = 0;
int count = 0;

/* Input section */
printf("How many pairs of data are available?");
scanf("%f", &arraylength);
if(arraylength<1) {
printf("Invalid array length\n");
exit(exitcode);
}
printf("Enter the angular frequency: ");
scanf("%f", &angfreq);

for(count = 0;count<=(arraylength-1);count++) {
printf("Enter the time for entry %d: ",count);
scanf("%f", &time[count]);
printf("Enter the output cooresponding to t = %d: ", count);
scanf("%f", &output[count]);
}

/* Calculation Section */
for(count = 0;count<=(arraylength-1);count++) {
sumy = sumy + output[count];
ysinesum = ysinesum + output[count]*sin(angfreq*time[count]);
ycossum = ycossum + output[count]*cos(angfreq*time[count]);
}

acoef = sumy / (arraylength - 1);
bcoef = (2/(arraylength - 1)) * ycossum;
ccoef = (2/(arraylength - 1)) * ysinesum;

/* Output Section */
printf("The equation of the sinusoid is: \n");
printf("%3.3f + %3.3f cos(%3.3f t) + %3.3f sin(%3.3f t)\n");
return 0;
}

Thanks,
Dave
 
N

Nelu

I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.

I realize that being a mathematician you won't care about some of the
advice I'll give you, but you should if you want to use C properly in
the future:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()

use: int main(void)
{
/* Integer declarations */
const int exitcode = -1;
float arraylength = 0;
float angfreq = 0;
float* time = (float*)NULL;
float* output = (float*)NULL;

no need for the casts.
float ysinesum = 0;
float ycossum = 0;
float sumy = 0;
float acoef = 0;
float bcoef = 0;
float ccoef = 0;
int count = 0;

/* Input section */
printf("How many pairs of data are available?");
scanf("%f", &arraylength);
if(arraylength<1) {
printf("Invalid array length\n");
exit(exitcode);
}
printf("Enter the angular frequency: ");
scanf("%f", &angfreq);

It is a good idea to check the value returned by scanf to make sure it
read properly.
for(count = 0;count<=(arraylength-1);count++) {
printf("Enter the time for entry %d: ",count);
scanf("%f", &time[count]);

You did not allocate memory for time which is of type pointer to float
and is initialized to NULL. You are trying to write in a place that
doesn't belong to you. This is, probably, the reason for your error,
although I don't remember what the error means in Turbo C. Protection
Exception makes me believe that's something related to your attempt to
write where you shouldn't.
To allocate space for arraylength elements you should use this:
time=malloc(arraylength*sizeof(double))
or
time=malloc(arraylength*sizeof*arraylength)
then you should check and make sure the memory was allocated:
if(time==NULL) {
printf("Oooops");
/* ... add code to handle the allocation problem */
/* probably to cleanup and exit */
}

If everything goes well, do not forget to free the memory allocated by
malloc.

The same goes lower with output.
printf("Enter the output cooresponding to t = %d: ", count);
scanf("%f", &output[count]);

Same mistake as above. You didn't allocate memory for output.
}

/* Calculation Section */
for(count = 0;count<=(arraylength-1);count++) {
sumy = sumy + output[count];
ysinesum = ysinesum + output[count]*sin(angfreq*time[count]);
ycossum = ycossum + output[count]*cos(angfreq*time[count]);
}

You're using output although it doesn't have memory allocated for it.
Your program probably doesn't get here, anyway.
acoef = sumy / (arraylength - 1);
bcoef = (2/(arraylength - 1)) * ycossum;
ccoef = (2/(arraylength - 1)) * ysinesum;

You should test for correct input for arraylength, otherwise you may
have surprises when you divide by 0.
 
B

Barry Schwarz

I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.

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

int main()
{
/* Integer declarations */
const int exitcode = -1;

It would be more portable to remove this and use EXIT_FAILURE as the
argument to exit().
float arraylength = 0;

It is a bit unusual to use a float to hold the number of elements of
an array.
float angfreq = 0;
float* time = (float*)NULL;

First, remove the casts. They serve no purpose.

Each of these pointers is initialized to point to "nowhere". In
particular, they do not point to memory you own or have legal access
to. You cannot extract data from where they point nor can you store
data there. Since the pointers are initialized, you can test them for
equality with other pointers or pointer constants, but you may not
perform arithmetic on them.
float* output = (float*)NULL;
float ysinesum = 0;
float ycossum = 0;
float sumy = 0;
float acoef = 0;
float bcoef = 0;
float ccoef = 0;
int count = 0;

/* Input section */
printf("How many pairs of data are available?");
scanf("%f", &arraylength);
if(arraylength<1) {
printf("Invalid array length\n");
exit(exitcode);
}
printf("Enter the angular frequency: ");
scanf("%f", &angfreq);

for(count = 0;count<=(arraylength-1);count++) {
printf("Enter the time for entry %d: ",count);
scanf("%f", &time[count]);

Here you violate all those restrictions. The expression &time[count]
is identical to the expression time+count. This is not a valid
address. And then you expect scanf to store something at that
address.

Before you can use time to reference an array of values, you must
initialize it so that it points to memory you can use. The
traditional method is
time = malloc(arraylength * sizeof *time);

This will, if it succeeds, allocate a contiguous block of memory
capable of holding arraylength objects, each of which is the size of
the object type time points to, in this case float.

You should always check to see if malloc succeeded by testing the
return value against NULL.
printf("Enter the output cooresponding to t = %d: ", count);
scanf("%f", &output[count]);
Ditto.

}

/* Calculation Section */
for(count = 0;count<=(arraylength-1);count++) {
sumy = sumy + output[count];
ysinesum = ysinesum + output[count]*sin(angfreq*time[count]);
ycossum = ycossum + output[count]*cos(angfreq*time[count]);
}

acoef = sumy / (arraylength - 1);
bcoef = (2/(arraylength - 1)) * ycossum;
ccoef = (2/(arraylength - 1)) * ysinesum;

/* Output Section */
printf("The equation of the sinusoid is: \n");
printf("%3.3f + %3.3f cos(%3.3f t) + %3.3f sin(%3.3f t)\n");

Each of the %3.3f terms in the format string means there must be a
corresponding argument following the string which evaluates to a
double. You don't have any arguments after the string. It is OK to
use a float value because such values are promoted to double when
passed to printf.
return 0;
}

Thanks,
Dave


Remove del for email
 
J

J. Sommers

I realize that being a mathematician you won't care about some of the
advice I'll give you,

Didn't your mummy teach you to leave your prejudices at home? Or
perhaps you are so small minded that just can't?
 
D

dmoran21

Barry said:
I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.

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

int main()
{
/* Integer declarations */
const int exitcode = -1;

It would be more portable to remove this and use EXIT_FAILURE as the
argument to exit().
float arraylength = 0;

It is a bit unusual to use a float to hold the number of elements of
an array.
float angfreq = 0;
float* time = (float*)NULL;

First, remove the casts. They serve no purpose.

Each of these pointers is initialized to point to "nowhere". In
particular, they do not point to memory you own or have legal access
to. You cannot extract data from where they point nor can you store
data there. Since the pointers are initialized, you can test them for
equality with other pointers or pointer constants, but you may not
perform arithmetic on them.
float* output = (float*)NULL;
float ysinesum = 0;
float ycossum = 0;
float sumy = 0;
float acoef = 0;
float bcoef = 0;
float ccoef = 0;
int count = 0;

/* Input section */
printf("How many pairs of data are available?");
scanf("%f", &arraylength);
if(arraylength<1) {
printf("Invalid array length\n");
exit(exitcode);
}
printf("Enter the angular frequency: ");
scanf("%f", &angfreq);

for(count = 0;count<=(arraylength-1);count++) {
printf("Enter the time for entry %d: ",count);
scanf("%f", &time[count]);

Here you violate all those restrictions. The expression &time[count]
is identical to the expression time+count. This is not a valid
address. And then you expect scanf to store something at that
address.

Before you can use time to reference an array of values, you must
initialize it so that it points to memory you can use. The
traditional method is
time = malloc(arraylength * sizeof *time);

This will, if it succeeds, allocate a contiguous block of memory
capable of holding arraylength objects, each of which is the size of
the object type time points to, in this case float.

You should always check to see if malloc succeeded by testing the
return value against NULL.
printf("Enter the output cooresponding to t = %d: ", count);
scanf("%f", &output[count]);
Ditto.

}

/* Calculation Section */
for(count = 0;count<=(arraylength-1);count++) {
sumy = sumy + output[count];
ysinesum = ysinesum + output[count]*sin(angfreq*time[count]);
ycossum = ycossum + output[count]*cos(angfreq*time[count]);
}

acoef = sumy / (arraylength - 1);
bcoef = (2/(arraylength - 1)) * ycossum;
ccoef = (2/(arraylength - 1)) * ysinesum;

/* Output Section */
printf("The equation of the sinusoid is: \n");
printf("%3.3f + %3.3f cos(%3.3f t) + %3.3f sin(%3.3f t)\n");

Each of the %3.3f terms in the format string means there must be a
corresponding argument following the string which evaluates to a
double. You don't have any arguments after the string. It is OK to
use a float value because such values are promoted to double when
passed to printf.
return 0;
}

Thanks,
Dave


Remove del for email

Thanks for all of your replies; this is the first major program I've
written since I've gotten my degree in math. I will look at these again
tomorrow when I look at it again. I think I really need to get a better
background in C; I only took one programming class in college.

Dave
 
N

Nelu

J. Sommers said:
Didn't your mummy teach you to leave your prejudices at home? Or
perhaps you are so small minded that just can't?

It's not prejudice. I have friends that are mathematicians and none of
them cares about checking the return value of scanf, malloc, and any
other checks, in general. They usually rely on the fact that those
functions will not fail for their needs.
That's all.
I aplogize if I hurt anyone's feelings.
 
C

CBFalconer

I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code
is below.

.... snip code ...

Your code is apparently C, not C++.

After passing your code through indent to eliminate the excessive
indentation etc. and make it readable, I compiled it. An obvious
error is the value of errorcode, which can only be 0, EXIT_SUCCESS,
or EXIT_FAILURE. Similarly casting NULL to float is evil. If you
want 0.0 say so. Casts are always suspect, and probably errors.
I see no sign of allocation of the arrays, which I suspect should
exist because of the presence of the variable arraylength (and why
should that be a float?). Just a cursory look.

Results follow:

junk.c: In function `main':
junk.c:53: warning: too few arguments for format

The line involved is the last printf.

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

int main()
{
/* Integer declarations */
const int exitcode = -1;
float arraylength = 0;
float angfreq = 0;
float *time = (float *)NULL;
float *output = (float *)NULL;
float ysinesum = 0;
float ycossum = 0;
float sumy = 0;
float acoef = 0;
float bcoef = 0;
float ccoef = 0;
int count = 0;

/* Input section */
printf("How many pairs of data are available?");
scanf("%f", &arraylength);
if (arraylength < 1) {
printf("Invalid array length\n");
exit(exitcode);
}
printf("Enter the angular frequency: ");
scanf("%f", &angfreq);

for (count = 0; count <= (arraylength - 1); count++) {
printf("Enter the time for entry %d: ", count);
scanf("%f", &time[count]);
printf("Enter the output cooresponding to t = %d: ", count);
scanf("%f", &output[count]);
}

/* Calculation Section */
for (count = 0; count <= (arraylength - 1); count++) {
sumy = sumy + output[count];
ysinesum = ysinesum +
output[count] * sin(angfreq * time[count]);
ycossum = ycossum +
output[count] * cos(angfreq * time[count]);
}

acoef = sumy / (arraylength - 1);
bcoef = (2 / (arraylength - 1)) * ycossum;
ccoef = (2 / (arraylength - 1)) * ysinesum;

/* Output Section */
printf("The equation of the sinusoid is: \n");
printf("%3.3f + %3.3f cos(%3.3f t) + %3.3f sin(%3.3f t)\n");
return 0;
}
 
J

Jack Klein

I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.

There are so many things wrong with this program, meaning that you
have so many misunderstandings about C, that I will probably miss
some.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
/* Integer declarations */
const int exitcode = -1;

In C++ they often use const qualified objects to replace constant
expressions or literals, but this is not idiomatic in C, nor do C
compilers always optimize it as well.

Furthermore, returning -1 from main(), or passing it to exit(), is
non-standard and non-portable and the result is
implementation-defined. Much better, especially since you have
is to use the standard and portable macro said:
float arraylength = 0;

Given the way you use "arraylength" below, it should be an integer
type, not a floating point type.
float angfreq = 0;
float* time = (float*)NULL;

"time" is a legal, but ill-advised, name for an object at block scope.
The standard C library contains a function named time(), prototyped in
the header <time.h>, and that name is reserved at file scope. It is
not reserved inside a function, as you use it here, but might well
confuse a reader.
float* output = (float*)NULL;

The (float*) cast in the two initializations is completely unnecessary
and serves only to clutter your program and slightly obscure it.
Eliminate them. The placement of the asterisk suggests that you are
actually programming in C++, but the cast is not necessary there
either.
float ysinesum = 0;
float ycossum = 0;
float sumy = 0;
float acoef = 0;
float bcoef = 0;
float ccoef = 0;
int count = 0;

/* Input section */
printf("How many pairs of data are available?");
scanf("%f", &arraylength);

scanf() is a very poor choice to use for interactive user input, for a
variety of reasons. Not the least of which is the fact that the
behavior is undefined if the scanned text represents a value outside
the range of the destination type.
if(arraylength<1) {
printf("Invalid array length\n");
exit(exitcode);

Here is where you should have:
exit(EXIT_FAILURE);
printf("Enter the angular frequency: ");
scanf("%f", &angfreq);

for(count = 0;count<=(arraylength-1);count++) {
printf("Enter the time for entry %d: ",count);
scanf("%f", &time[count]);

Here's where your reap problem starts. You are asking scanf() to
write a value into time[copunt], but "time" is a null pointer, you
specifically initialized it that way. "time" does not point to any
floats, in fact it does not point to any valid memory that your
program can access.

When you try to write to memory through a null pointer, you produce
undefined behavior and under many modern operating systems, generate
some sort of fault that causes the OS to terminate your program.
printf("Enter the output cooresponding to t = %d: ", count);
scanf("%f", &output[count]);
}

/* Calculation Section */
for(count = 0;count<=(arraylength-1);count++) {
sumy = sumy + output[count];
ysinesum = ysinesum + output[count]*sin(angfreq*time[count]);
ycossum = ycossum + output[count]*cos(angfreq*time[count]);
}

acoef = sumy / (arraylength - 1);
bcoef = (2/(arraylength - 1)) * ycossum;
ccoef = (2/(arraylength - 1)) * ysinesum;

/* Output Section */
printf("The equation of the sinusoid is: \n");
printf("%3.3f + %3.3f cos(%3.3f t) + %3.3f sin(%3.3f t)\n");
return 0;
}

Somewhere after the definition of the pointers "time" and "output" and
getting a value for "count", you need to allocate memory to these
pointers to hold the values you want to store there.

Something like this:

time = malloc(count * sizeof *time);
output = malloc(count * sizeof *output);
if ((NULL == time) || (NULL == output))
{
printf("Memory allocation failure\n");
free(time);
free(output);
exit (EXIT_FAILURE);
}

And at the end of your program, before the perfectly correct "return
0;", you should add:

free(time);
free(output);
 
D

dmoran21

Nelu said:
It's not prejudice. I have friends that are mathematicians and none of
them cares about checking the return value of scanf, malloc, and any
other checks, in general. They usually rely on the fact that those
functions will not fail for their needs.
That's all.
I aplogize if I hurt anyone's feelings.

None taken; I really think that a programming class should've been
required for my math degree. I took it as part of my other degree
(meteorology).

Dave
 
D

dmoran21

Jack said:
I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.

There are so many things wrong with this program, meaning that you
have so many misunderstandings about C, that I will probably miss
some.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
/* Integer declarations */
const int exitcode = -1;

In C++ they often use const qualified objects to replace constant
expressions or literals, but this is not idiomatic in C, nor do C
compilers always optimize it as well.

Furthermore, returning -1 from main(), or passing it to exit(), is
non-standard and non-portable and the result is
implementation-defined. Much better, especially since you have
is to use the standard and portable macro said:
float arraylength = 0;

Given the way you use "arraylength" below, it should be an integer
type, not a floating point type.
float angfreq = 0;
float* time = (float*)NULL;

"time" is a legal, but ill-advised, name for an object at block scope.
The standard C library contains a function named time(), prototyped in
the header <time.h>, and that name is reserved at file scope. It is
not reserved inside a function, as you use it here, but might well
confuse a reader.
float* output = (float*)NULL;

The (float*) cast in the two initializations is completely unnecessary
and serves only to clutter your program and slightly obscure it.
Eliminate them. The placement of the asterisk suggests that you are
actually programming in C++, but the cast is not necessary there
either.
float ysinesum = 0;
float ycossum = 0;
float sumy = 0;
float acoef = 0;
float bcoef = 0;
float ccoef = 0;
int count = 0;

/* Input section */
printf("How many pairs of data are available?");
scanf("%f", &arraylength);

scanf() is a very poor choice to use for interactive user input, for a
variety of reasons. Not the least of which is the fact that the
behavior is undefined if the scanned text represents a value outside
the range of the destination type.
if(arraylength<1) {
printf("Invalid array length\n");
exit(exitcode);

Here is where you should have:
exit(EXIT_FAILURE);
printf("Enter the angular frequency: ");
scanf("%f", &angfreq);

for(count = 0;count<=(arraylength-1);count++) {
printf("Enter the time for entry %d: ",count);
scanf("%f", &time[count]);

Here's where your reap problem starts. You are asking scanf() to
write a value into time[copunt], but "time" is a null pointer, you
specifically initialized it that way. "time" does not point to any
floats, in fact it does not point to any valid memory that your
program can access.

When you try to write to memory through a null pointer, you produce
undefined behavior and under many modern operating systems, generate
some sort of fault that causes the OS to terminate your program.
printf("Enter the output cooresponding to t = %d: ", count);
scanf("%f", &output[count]);
}

/* Calculation Section */
for(count = 0;count<=(arraylength-1);count++) {
sumy = sumy + output[count];
ysinesum = ysinesum + output[count]*sin(angfreq*time[count]);
ycossum = ycossum + output[count]*cos(angfreq*time[count]);
}

acoef = sumy / (arraylength - 1);
bcoef = (2/(arraylength - 1)) * ycossum;
ccoef = (2/(arraylength - 1)) * ysinesum;

/* Output Section */
printf("The equation of the sinusoid is: \n");
printf("%3.3f + %3.3f cos(%3.3f t) + %3.3f sin(%3.3f t)\n");
return 0;
}

Somewhere after the definition of the pointers "time" and "output" and
getting a value for "count", you need to allocate memory to these
pointers to hold the values you want to store there.

Something like this:

time = malloc(count * sizeof *time);
output = malloc(count * sizeof *output);
if ((NULL == time) || (NULL == output))
{
printf("Memory allocation failure\n");
free(time);
free(output);
exit (EXIT_FAILURE);
}

And at the end of your program, before the perfectly correct "return
0;", you should add:

free(time);
free(output);

Thanks, Jack. Bear with me as I am relearning a lot of this. I got away
with not having to program for about a year and a half after I got my
degree so I'm probably due.

Dave
 
J

james of tucson

J. Sommers said:
Didn't your mummy teach you to leave your prejudices at home? Or
perhaps you are so small minded that just can't?

Such prejudices usually don't develop until after you are forced to work
with mathematicians, then they aren't so much prejudices as judgements.
 
E

Ernie Wright

Jack said:
Given the way you use "arraylength" below, it should be an integer
type, not a floating point type.

A number of people have said this, but by itself it's misleading. The
OP later uses arraylength in expressions that are intended to be float
valued:

If he declares arraylength as an int, the subexpression

2 / ( arraylength - 1 )

will be evaluated using integer arithmetic, and will have the value 0
for arraylength > 3.

Assuming an arraylength declared as an int, the subexpression must be
coerced to a floating-point type. Either

2 / (( float ) arraylength - 1 )

or

2.0 / ( arraylength - 1 )

Or he could rearrange the full expression:

bcoef = ycossum * 2 / ( arraylength - 1 );

In this case, as long as ycossum is a floating-point type, the entire
expression, written in this order, is evaluated in floating-point.

Making arraylength an int is the right thing to do, but you also have to
account for the numerical consequences.

- Ernie http://home.comcast.net/~erniew
 
E

Ernie Wright

I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it.

A GPF (general protection fault) is what happens on x86-based machines
when your program does something it's not allowed to do, and by far the
most common cause is trying to access memory that doesn't belong to you.

In your program, you declare a couple of pointers,
float* time = (float*)NULL;
float* output = (float*)NULL;

and later try to store some numbers in the memory they point to,
scanf("%f", &time[count]);
scanf("%f", &output[count]);

but since you didn't actually allocate any memory for them, they don't
point to anywhere.

A couple of observations in addition to the ones others have already made:

scanf() can be a minefield. An alternative that's often better is to use
something like fgets() in a custom function. For example,

int enter_int( void )
{
char buf[ 80 ];

fgets( buf, sizeof buf, stdin );
return atoi( buf );
}

You can make this as fancy as you want, and all the extra work remains in
this one function, no matter how many times you need to get an input from
the user. It could print the prompt and enforce range restrictions:

int enter_int( char *prompt, int lo, int hi )
{
char buf[ 80 ];
int i;

while ( 1 ) {
printf( "%s\n", prompt );
fgets( buf, sizeof buf, stdin );
i = atoi( buf );
if ( i >= lo && i <= hi )
return i;
printf( "The number must be between %d and %d.\n", lo, hi );
}
}

Or it could optionally get the input from any stream, e.g. a data file,
rather than solely from the user.

int enter_int( FILE *fp )
{
char buf[ 80 ];

fgets( buf, sizeof buf, fp );
return atoi( buf );
}
for(count = 0;count<=(arraylength-1);count++) {

The more common and concise idiom for this is

for ( count = 0; count < arraylength; count++ ) {
sumy = sumy + output[count];

C allows you to write this more concisely as

sumy += output[ count ];

Whichever way you write it, beware of the numerical behavior of this
kind of accumulation. In the worst case, roundoff error can grow at
each iteration. Consider the following program.

#include <stdio.h>

int main( void )
{
float f = 1000.0;
int i;

for ( i = 0; i < 1000; i++ )
f += 0.1;
printf( "%.6f\n", f );
return 0;
}

You might expect this to print "1100.000000", but in reality it prints
"1099.975586" (typically).
printf("The equation of the sinusoid is: \n");
printf("%3.3f + %3.3f cos(%3.3f t) + %3.3f sin(%3.3f t)\n");

If the intent is simply to print the number with 3 decimal places, you
don't need the width component of the format specifier. In other words,
rather than "%3.3f", you only need "%.3f".

- Ernie http://home.comcast.net/~erniew
 
I

Ivar Rosquist

It's not prejudice.

It is prejudice.
I have friends that are mathematicians and none of them cares about
checking the return value of scanf, malloc, and any other checks, in
general.

How many friends do you have that are mathematicians? Ten? Twenty? How
does that compare with the thousands of mathematicians world over? Even
those ten or twenty systematically, and in every single occasion were to
ignore your advice that would not imply that all mathematicians would (but
it would probably imply that there is something systematically weird with
your advice).

I also have a number a software engineer friends who entertain astounding
superstitions, but that is no evidence that all software engineers do so.
You just can't generalize from such small samples and anecdotal evidence.
They usually rely on the fact that those functions will not
fail for their needs. That's all.

Which might be a sensible thing to do for them. C purity and perfection
might be your goal, but other people will just want their little C code to
run well enough to solve their problem, even if, say, they use

int main()

rather than

int main(int argc, char **argv)

I aplogize if I hurt anyone's feelings.

Those who have the guts to apologize in public tend to be a cut above the
rest. Kudos to you. Just remember to keep your prejudices in check. We all
have prejudices - otherwise we could not make sense of a world where
information is almost always incomplete.
 
N

Nelu

Ivar Rosquist said:
It is prejudice.

Ok. Although, combining my (limited) experience with the lack of checks in
the OP's code I don't think it should have come out that way. But that's
just my opinion, of course.
How many friends do you have that are mathematicians? Ten? Twenty? How
does that compare with the thousands of mathematicians world over? Even
those ten or twenty systematically, and in every single occasion were to
ignore your advice that would not imply that all mathematicians would (but
it would probably imply that there is something systematically weird with
your advice).

I also have a number a software engineer friends who entertain astounding
superstitions, but that is no evidence that all software engineers do so.
You just can't generalize from such small samples and anecdotal evidence.


Which might be a sensible thing to do for them. C purity and perfection
might be your goal, but other people will just want their little C code to
run well enough to solve their problem, even if, say, they use

int main()

rather than

int main(int argc, char **argv)

This is why I said what I said. I should have probably said 'you may not
care' instead of 'you won't care' and I specified 'some of the advice'
because it 'might be a sensible thing to do for them' to do things the
way they see fit. I did not indent to say anything that may be
considered prejudiced or hurtful in any way. I am really sorry if it
came out that way.
 
A

Al Balmer

It is prejudice.

In general, programmers are more interested in programming than in
mathematics. In general, mathematicians are more interested in
mathematics than programming.

If that's prejudice, so be it.
 
R

Richard Heathfield

Al Balmer said:
In general, programmers are more interested in programming than in
mathematics. In general, mathematicians are more interested in
mathematics than programming.

They're different states of the same element. Mathematics is frozen
programming. (Or, if you prefer, programming is defrosted mathematics.)
 
C

Clever Monkey

Nelu said:
It's not prejudice. I have friends that are mathematicians and none of
them cares about checking the return value of scanf, malloc, and any
other checks, in general. They usually rely on the fact that those
functions will not fail for their needs.
That's all.
I aplogize if I hurt anyone's feelings.
*shrug* I know a graph theorist and he always listen to advice
regarding clean coding.

People from all walks of life ignore returns from malloc(), at al. Even
coders, which is evident from the constant stream of examples to this
very newsgroup.
 
C

Clever Monkey

Richard said:
Al Balmer said:


They're different states of the same element. Mathematics is frozen
programming. (Or, if you prefer, programming is defrosted mathematics.)
So, if you leave your math alone for too long, does it become freezer burnt?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top