Connecting Tech Pros Worldwide Forums | Help | Site Map

Segmentation Fault....

Nomad.C@gmail.com
Guest
 
Posts: n/a
#1: Apr 10 '07
Hi
I'm coding this simple program, but I get a segmentation fault, I'm
pretty sure that the arrays are big enough and there isn't really any
buffer overflows so why is this happening?

Program as followed:

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

//------------------------------------------------------
// Programming II - Coursework Assignment 1-4
// This program calculates the following :
// - Sum of numbers
// - Sum of numbers squared
//- Mean
// - Mean of numbers squared
// - Standard Deviation
//- Error in mean of a set of numbers
//------------------------------------------------------
// Note: This version differs in the previous version.
// 1.)Arrays are used to store and be read
// 2.)Two functions are introduced vsum, vsum2
// 3.)Array refuses to accept more values
//------------------------------------------------------

//Functions
double vsum(double x[20], int n){
int i;
float Result = 0;

//Calculation for - Sum of numbers
for (i=1;i<=n;i++){
Result += x[i];
}
return Result;
}

double vsum2(double x[20], int n){
int i;
float Result = 0;
float Number;

//Calculation for - Sum of the numbers squared
for (i=1;i<n+1;i++){
Number = x[i];
Result += pow(Number,2);
}
return Result;
}


int main ()
{
int NumOfNum; // Number "n" of numbers to be entered
int i; // For-loop counter
double Array[20]; // Array to store numbers
float Sum_1 = 0; // Sum of numbers
float Sum_2 = 0; // Sum of the numbers squared
float Mean_1 = 0; // Mean
float Mean_2 = 0; // Mean of the numbers squared
float SD = 0; // Standard Deviation
float Error = 0; // Error in mean
float Number = 0;
int Count = 1;

// Introduction
printf("\nThis program calculates the following:\n");
printf("- Sum of numbers\n- Sum of numbers squared\n- Mean\n- Mean of
numbers squared\n");
printf("- Standard Deviation\n- Error in mean of a set of numbers\n
\n");

// Number "n" of numbers
printf("To obtain all of the above, you first need to enter the
number \"n\" \n");
printf("of numbers to be entered. Followed by the number itself.\n");
scanf(" %f", &NumOfNum);

//Adds numbers into array & calculation for sum of numbers
for (i=0;i<=NumOfNum;i++){
printf("\nEnter the value for number %d \n", Count);
scanf(" %f", &Number);
Array[i] = Number;
//Uses the function vsum
Sum_1 = vsum(Array, NumOfNum);
//Uses the function vsum2
Sum_2 = vsum2(Array, NumOfNum);
Count++;
}

//Calculation for - Mean
Mean_1 = Sum_1 / NumOfNum;
//Calculation for - Mean of the squares
Mean_2 = Sum_2 / NumOfNum;
//Calculation for - Standard Deviation
SD = sqrt(Mean_2 - pow(Mean_1,2));
//Calculation for - Error in mean
Error = SD / sqrt(NumOfNum);

//Displays All Results
printf("\n\nTable of Results:\n");
printf("---------------------------------------------------\n");
printf("The Sum of numbers is: \t\t\t%.2f\n", Sum_1);
printf("The Sum of the numbers squared is: \t%.2f\n", Sum_2);
printf("The Mean is: \t\t\t\t%.2f\n", Mean_1);
printf("The Mean of the numbers squared is: \t%.2f\n", Mean_2);
printf("The Stanard Deviation is: \t\t%.2f\n", SD);
printf("The Error in the mean is: \t\t%.2f\n\n", Error);
return 0;
}

any advice would be grateful thanks
Chris


Malcolm McLean
Guest
 
Posts: n/a
#2: Apr 10 '07

re: Segmentation Fault....



<Nomad.C@gmail.comwrote in message
news:1176216355.474692.168560@y80g2000hsf.googlegr oups.com...
Quote:
Hi
I'm coding this simple program, but I get a segmentation fault, I'm
pretty sure that the arrays are big enough and there isn't really any
buffer overflows so why is this happening?
>
Program as followed:
>
#include <stdio.h>
#include <math.h>
>
//------------------------------------------------------
// Programming II - Coursework Assignment 1-4
// This program calculates the following :
// - Sum of numbers
// - Sum of numbers squared
//- Mean
// - Mean of numbers squared
// - Standard Deviation
//- Error in mean of a set of numbers
//------------------------------------------------------
// Note: This version differs in the previous version.
// 1.)Arrays are used to store and be read
// 2.)Two functions are introduced vsum, vsum2
// 3.)Array refuses to accept more values
//------------------------------------------------------
>
//Functions
double vsum(double x[20], int n){
int i;
float Result = 0;
>
//Calculation for - Sum of numbers
for (i=1;i<=n;i++){
Result += x[i];
}
return Result;
}
>
double vsum2(double x[20], int n){
int i;
float Result = 0;
float Number;
>
//Calculation for - Sum of the numbers squared
for (i=1;i<n+1;i++){
Number = x[i];
Result += pow(Number,2);
}
return Result;
}
>
>
int main ()
{
int NumOfNum; // Number "n" of numbers to be entered
int i; // For-loop counter
double Array[20]; // Array to store numbers
float Sum_1 = 0; // Sum of numbers
float Sum_2 = 0; // Sum of the numbers squared
float Mean_1 = 0; // Mean
float Mean_2 = 0; // Mean of the numbers squared
float SD = 0; // Standard Deviation
float Error = 0; // Error in mean
float Number = 0;
int Count = 1;
>
// Introduction
printf("\nThis program calculates the following:\n");
printf("- Sum of numbers\n- Sum of numbers squared\n- Mean\n- Mean of
numbers squared\n");
printf("- Standard Deviation\n- Error in mean of a set of numbers\n
\n");
>
// Number "n" of numbers
printf("To obtain all of the above, you first need to enter the
number \"n\" \n");
printf("of numbers to be entered. Followed by the number itself.\n");
scanf(" %f", &NumOfNum);
>
This could be the problem since you are asking scanf() to write a float to
an integer. It could crash here, more liley float and int is 32 bits, and it
writes a number which is interpreted as a huge value to NumOfNum
Quote:
//Adds numbers into array & calculation for sum of numbers
for (i=0;i<=NumOfNum;i++){
printf("\nEnter the value for number %d \n", Count);
scanf(" %f", &Number);
Array[i] = Number;
//Uses the function vsum
Sum_1 = vsum(Array, NumOfNum);
//Uses the function vsum2
Sum_2 = vsum2(Array, NumOfNum);
Count++;
}
>
//Calculation for - Mean
Mean_1 = Sum_1 / NumOfNum;
//Calculation for - Mean of the squares
Mean_2 = Sum_2 / NumOfNum;
//Calculation for - Standard Deviation
SD = sqrt(Mean_2 - pow(Mean_1,2));
//Calculation for - Error in mean
Error = SD / sqrt(NumOfNum);
>
//Displays All Results
printf("\n\nTable of Results:\n");
printf("---------------------------------------------------\n");
printf("The Sum of numbers is: \t\t\t%.2f\n", Sum_1);
printf("The Sum of the numbers squared is: \t%.2f\n", Sum_2);
printf("The Mean is: \t\t\t\t%.2f\n", Mean_1);
printf("The Mean of the numbers squared is: \t%.2f\n", Mean_2);
printf("The Stanard Deviation is: \t\t%.2f\n", SD);
printf("The Error in the mean is: \t\t%.2f\n\n", Error);
return 0;
}
>
any advice would be grateful thanks
Chris
>
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

bytebro
Guest
 
Posts: n/a
#3: Apr 10 '07

re: Segmentation Fault....


On 10 Apr, 15:45, "Noma...@gmail.com" <Noma...@gmail.comwrote:
Quote:
>
I'm coding this simple program, but I get a segmentation fault, I'm
pretty sure that the arrays are big enough and there isn't really any
buffer overflows so why is this happening?
Well compiling with "gcc -Wall" gives:

x.c: In function `main':
x.c:78: warning: float format, different type arg (arg 2)

which points to the line which says:

scanf(" %f", &NumOfNum);

which is reading a double into an int.

I'd start from there if I were you...

Daniel Rudy
Guest
 
Posts: n/a
#4: Apr 10 '07

re: Segmentation Fault....


At about the time of 4/10/2007 7:45 AM, Nomad.C@gmail.com stated the
following:
Quote:
Hi
I'm coding this simple program, but I get a segmentation fault, I'm
pretty sure that the arrays are big enough and there isn't really any
buffer overflows so why is this happening?
>
Program as followed:
>
#include <stdio.h>
#include <math.h>
>
//------------------------------------------------------
// Programming II - Coursework Assignment 1-4
// This program calculates the following :
// - Sum of numbers
// - Sum of numbers squared
//- Mean
// - Mean of numbers squared
// - Standard Deviation
//- Error in mean of a set of numbers
//------------------------------------------------------
// Note: This version differs in the previous version.
// 1.)Arrays are used to store and be read
// 2.)Two functions are introduced vsum, vsum2
// 3.)Array refuses to accept more values
//------------------------------------------------------
>
Those comments are C++ style. Are you coding in C or C++? This group
is for C. If you want C++, then head to the room down the hall.

As for your code....See questions 4.11 and various questions in 6 in the
FAQ. http://c-faq.com/
Quote:
>
any advice would be grateful thanks
Chris
>

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Nomad.C@gmail.com
Guest
 
Posts: n/a
#5: Apr 10 '07

re: Segmentation Fault....


On 10 Apr, 16:09, Daniel Rudy <spamt...@spamthis.netwrote:
Quote:
At about the time of 4/10/2007 7:45 AM, Noma...@gmail.com stated the
following:
>
>
>
Quote:
Hi
I'm coding this simple program, but I get a segmentation fault, I'm
pretty sure that the arrays are big enough and there isn't really any
buffer overflows so why is this happening?
>
Quote:
Program as followed:
>
Quote:
#include <stdio.h>
#include <math.h>
>
Quote:
//------------------------------------------------------
// Programming II - Coursework Assignment 1-4
// This program calculates the following :
// - Sum of numbers
// - Sum of numbers squared
//- Mean
// - Mean of numbers squared
// - Standard Deviation
//- Error in mean of a set of numbers
//------------------------------------------------------
// Note: This version differs in the previous version.
// 1.)Arrays are used to store and be read
// 2.)Two functions are introduced vsum, vsum2
// 3.)Array refuses to accept more values
//------------------------------------------------------
>
Those comments are C++ style. Are you coding in C or C++? This group
is for C. If you want C++, then head to the room down the hall.
>
As for your code....See questions 4.11 and various questions in 6 in the
FAQ. http://c-faq.com/
>
>
>
Quote:
any advice would be grateful thanks
Chris
>
--
Daniel Rudy
>
Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m
>
Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Thanks for the tip about scanf..can't believe it was a simple wrong
type......
I don't think C/C++ comments make much difference....

jacob navia
Guest
 
Posts: n/a
#6: Apr 10 '07

re: Segmentation Fault....


Daniel Rudy a écrit :
Quote:
>
Those comments are C++ style. Are you coding in C or C++? This group
is for C. If you want C++, then head to the room down the hall.
>
This is nonsense.

// comments are standard C!!!

jacob
jacob navia
Guest
 
Posts: n/a
#7: Apr 10 '07

re: Segmentation Fault....


Nomad.C@gmail.com a écrit :
Quote:
I don't think C/C++ comments make much difference....
>
// comments are standard C. Mr Rudy is completely wrong.
Charlton Wilbur
Guest
 
Posts: n/a
#8: Apr 10 '07

re: Segmentation Fault....


>>>>"N" == Nomad C@gmail com <Nomad.C@gmail.comwrites:

NThanks for the tip about scanf..can't believe it was a simple
Nwrong type...... I don't think C/C++ comments make much
Ndifference....

They do if you want help from this group, for two reasons.

First, // comments were only included in the C99 standard. While they
are technically standard, using them is more often than not a sign of
sloppy thinking about C and a lack of awareness of C standards, which
is likely to reflect poorly on the programmer and irritate the most
helpful people here (who care deeply about what is standard and
non-standard in C).

Second, they are a pain on Usenet, especially with long lines; if my
terminal window is narrower than you expect, that comment is likely to
rewrap when I copy and paste your code to compile it myself to see
what happens. This is one more problem that I need to fix before I
can offer help, and makes me less likely to bother helping with code
that has // comments.

Charlton



--
Charlton Wilbur
cwilbur@chromatico.net
jacob navia
Guest
 
Posts: n/a
#9: Apr 10 '07

re: Segmentation Fault....


Charlton Wilbur a écrit :
Quote:
Quote:
Quote:
>>>>>>"N" == Nomad C@gmail com <Nomad.C@gmail.comwrites:
>
>
NThanks for the tip about scanf..can't believe it was a simple
Nwrong type...... I don't think C/C++ comments make much
Ndifference....
>
They do if you want help from this group, for two reasons.
>
First, // comments were only included in the C99 standard. While they
are technically standard, using them is more often than not a sign of
sloppy thinking about C and a lack of awareness of C standards, which
is likely to reflect poorly on the programmer and irritate the most
helpful people here (who care deeply about what is standard and
non-standard in C).
Look Mr, if you "care deeply about what is standard and non-standard"
please stop talking nonsense.

The C standard 6.4.9: Comments

Except within a character constant, a string literal, or a comment, the
characters // introduce a comment that includes all multibyte characters
up to, but not including, the next new-line character. The contents of
such a comment are examined only to identify multibyte characters and to
find the terminating new-line character.

The C99 standard is the current standard. Others have only an historical
interest.
Richard Heathfield
Guest
 
Posts: n/a
#10: Apr 10 '07

re: Segmentation Fault....


jacob navia said:
Quote:
Charlton Wilbur a écrit :
Quote:
Quote:
>>>>>>>"N" == Nomad C@gmail com <Nomad.C@gmail.comwrites:
>>
>>
> NThanks for the tip about scanf..can't believe it was a simple
> Nwrong type...... I don't think C/C++ comments make much
> Ndifference....
>>
>They do if you want help from this group, for two reasons.
>>
>First, // comments were only included in the C99 standard. While
>they are technically standard, using them is more often than not a
>sign of sloppy thinking about C and a lack of awareness of C
>standards, which is likely to reflect poorly on the programmer and
>irritate the most helpful people here (who care deeply about what is
>standard and non-standard in C).
>
Look Mr, if you "care deeply about what is standard and non-standard"
please stop talking nonsense.
He's not talking nonsense. He makes good points, only one of which you
have left unsnipped.
Quote:
>
The C standard 6.4.9: Comments
>
Except within a character constant, a string literal, or a comment,
the characters // introduce a comment [...]
Yes, you're right - C99 introduced // comments. Nobody is disputing
this.
Quote:
The C99 standard is the current standard.
Nobody is disputing that either. It's just that almost nobody uses the
C99 Standard in daily life. It's still a C90 world.
Quote:
Others have only an historical interest.
C90 is still the standard that almost everyone actually uses in the real
world. C99 is mostly of academic interest. Few conforming C99
implementations exist.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
jacob navia
Guest
 
Posts: n/a
#11: Apr 10 '07

re: Segmentation Fault....


Richard Heathfield a écrit :
Quote:
jacob navia said:
>
>
Quote:
>>Charlton Wilbur a écrit :
>>
Quote:
>>>>>>>>"N" == Nomad C@gmail com <Nomad.C@gmail.comwrites:
>>>
>>>
>> NThanks for the tip about scanf..can't believe it was a simple
>> Nwrong type...... I don't think C/C++ comments make much
>> Ndifference....
>>>
>>>They do if you want help from this group, for two reasons.
>>>
>>>First, // comments were only included in the C99 standard. While
>>>they are technically standard, using them is more often than not a
>>>sign of sloppy thinking about C and a lack of awareness of C
>>>standards, which is likely to reflect poorly on the programmer and
>>>irritate the most helpful people here (who care deeply about what is
>>>standard and non-standard in C).
>>
>>Look Mr, if you "care deeply about what is standard and non-standard"
>>please stop talking nonsense.
>
>
He's not talking nonsense. He makes good points, only one of which you
have left unsnipped.
>
>
Quote:
>>The C standard 6.4.9: Comments
>>
>>Except within a character constant, a string literal, or a comment,
>>the characters // introduce a comment [...]
>
>
Yes, you're right - C99 introduced // comments. Nobody is disputing
this.
>
>
Quote:
>>The C99 standard is the current standard.
>
>
Nobody is disputing that either. It's just that almost nobody uses the
C99 Standard in daily life. It's still a C90 world.
>
Excuse me, I have yet to find a C compiler that does NOT accept
// comments.

Please show me *some* example for those...
Charlton Wilbur
Guest
 
Posts: n/a
#12: Apr 10 '07

re: Segmentation Fault....


>>>>"jn" == jacob navia <jacob@jacob.remcomp.frwrites:

jnCharlton Wilbur a écrit :
Quote:
Quote:
>First, // comments were only included in the C99 standard.
>While they are technically standard, using them is more often
>than not a sign of sloppy thinking about C and a lack of
>awareness of C standards, which is likely to reflect poorly on
>the programmer and irritate the most helpful people here (who
>care deeply about what is standard and non-standard in C).
jnLook Mr, if you "care deeply about what is standard and
jnnon-standard" please stop talking nonsense.

Quoting chapter and verse that establishes that what I said in the
first line is correct does not invalidate what I said in the
following five lines.

jnThe C99 standard is the current standard. Others have only an
jnhistorical interest.

Find me a fully conforming C99 compiler and standard library in
widespread use, and I'll believe you.

Charlton


--
Charlton Wilbur
cwilbur@chromatico.net
jacob navia
Guest
 
Posts: n/a
#13: Apr 10 '07

re: Segmentation Fault....


Charlton Wilbur a écrit :
Quote:
Quote:
Quote:
>>>>>>"jn" == jacob navia <jacob@jacob.remcomp.frwrites:
>
>
jnCharlton Wilbur a écrit :
>
Quote:
Quote:
>First, // comments were only included in the C99 standard.
>While they are technically standard, using them is more often
>than not a sign of sloppy thinking about C and a lack of
>awareness of C standards, which is likely to reflect poorly on
>the programmer and irritate the most helpful people here (who
>care deeply about what is standard and non-standard in C).
>
jnLook Mr, if you "care deeply about what is standard and
jnnon-standard" please stop talking nonsense.
>
Quoting chapter and verse that establishes that what I said in the
first line is correct does not invalidate what I said in the
following five lines.
>
jnThe C99 standard is the current standard. Others have only an
jnhistorical interest.
>
Find me a fully conforming C99 compiler and standard library in
widespread use, and I'll believe you.
>
Charlton
>
>
// comments are accepted by most C compilers: microsoft, gcc, IBM,
and almost all main compilers accept them. This feature is really almost
universal by now.

What is strange is that all this people that always speak about
"standard C" do not really mean standard C but some other standard
like C95, C89, or maybe even K&R C from 1975???

It is already 8 years since 1999, and maybe it is time to look around
a bit?

Or you prefer living in the past, as many people here?

jacob
Richard Heathfield
Guest
 
Posts: n/a
#14: Apr 10 '07

re: Segmentation Fault....


jacob navia said:

<snip>
Quote:
Excuse me, I have yet to find a C compiler that does NOT accept
// comments.
I get diagnostic messages for // comments from all of the following
compilers (among others) when they are invoked in their conforming
mode:

Turbo C 2.01
Borland C 4.5, 5.02
Microsoft Visual C 1.0, 1.5, 2.0, 5.0, 6.0
gcc

This is, of course, a non-exhaustive list. I don't recall ever
encountering a conforming C compiler that didn't diagnose // comments
when invoked in conforming mode. In fact, if it didn't diagnose them,
it wouldn't be a conforming C compiler (unless it conformed to C99, of
course, but there are precious few compilers that do that).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Richard Heathfield
Guest
 
Posts: n/a
#15: Apr 10 '07

re: Segmentation Fault....


jacob navia said:
Quote:
Charlton Wilbur a écrit :
Quote:
Quote:
>>>>>>>"jn" == jacob navia <jacob@jacob.remcomp.frwrites:
>>
<snip>
Quote:
Quote:
> jnThe C99 standard is the current standard. Others have only an
> jnhistorical interest.
>>
>Find me a fully conforming C99 compiler and standard library in
>widespread use, and I'll believe you.
>>
// comments are accepted by most C compilers: microsoft, gcc, IBM,
and almost all main compilers accept them. This feature is really
almost universal by now.
Not in their conforming mode.
Quote:
What is strange is that all this people that always speak about
"standard C" do not really mean standard C but some other standard
like C95, C89, or maybe even K&R C from 1975???
>
It is already 8 years since 1999, and maybe it is time to look around
a bit?
Find me a fully conforming C99 compiler and standard library in
widespread use, and I'll believe you.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
jacob navia
Guest
 
Posts: n/a
#16: Apr 10 '07

re: Segmentation Fault....


Richard Heathfield a écrit :
Quote:
jacob navia said:
>
<snip>
>
Quote:
>>Excuse me, I have yet to find a C compiler that does NOT accept
>>// comments.
>
>
I get diagnostic messages for // comments from all of the following
compilers (among others) when they are invoked in their conforming
mode:
>
Turbo C 2.01
BRAVO Heathfield:
According to http://dn.codegear.com/article/20841
Antique Software: Turbo C version 2.01
Ship date: 11-May-1989
Quote:
Borland C 4.5, 5.02
The official name is
Borland C++ 5.02. You have surely tweaked the compiler to
put it in some configuration that doesn't accept //

I have compiled with that compiler and it will accept //
in C mode.
Quote:
Microsoft Visual C 1.0, 1.5, 2.0, 5.0, 6.0
It accepts those comments since at least
1995 or even earlier. In any case, since 1995
they are accepted in C mode. This is just a lie.

Quote:
gcc
Gcc accepts // comments in C mode unles you force it not to.
This is a lie too.
Quote:
>
This is, of course, a non-exhaustive list. I don't recall ever
encountering a conforming C compiler that didn't diagnose // comments
when invoked in conforming mode.
Conforming to WHAT?
Obviously conforming to C89. Many compilers have a flag that
makes them conforming to C89, even lcc-win32 has one. But this
is just another lie Hethfield.

Quote:
In fact, if it didn't diagnose them,
it wouldn't be a conforming C compiler (unless it conformed to C99, of
course, but there are precious few compilers that do that).

IBM does (In the power pc version of their compiler for instance)
gcc does
and many other compilers have adopted C99.

But if you want to live in the past, or in some world that
stopped spinning in 1989 you are welcome.

But do not pretend that would be standard C.
Clever Monkey
Guest
 
Posts: n/a
#17: Apr 10 '07

re: Segmentation Fault....


jacob navia wrote:
Quote:
Richard Heathfield a écrit :
Quote:
>jacob navia said:
>>
><snip>
>>
Quote:
>>Excuse me, I have yet to find a C compiler that does NOT accept
>>// comments.
>>
>>
>I get diagnostic messages for // comments from all of the following
>compilers (among others) when they are invoked in their conforming mode:
>>
[...]
Quote:
Quote:
>Microsoft Visual C 1.0, 1.5, 2.0, 5.0, 6.0
>
It accepts those comments since at least
1995 or even earlier. In any case, since 1995
they are accepted in C mode. This is just a lie.
>
No lie. I have to use MSVC 5 and MSVC 6 to compile pure C code
regularly, and it will warn when fed such comments, at least when
invoked via our makefiles. I don't care enough to figure out why. I
just change my code to adhere to the standard I know is mostly properly
implemented.

I can add to the list some varieties of currently supported Sun C
compilers and IBM C compilers on AIX, since the code where folks ignore
those warnings on Windows invariably fails to compile /at all/ when the
nightlies happen on Unix.

Moral of the story: working C coders that have to compile on a great
many platforms use C90 as a common base, and form their code to adhere
to this standard. If something as simple as a lazy set of comments or
variable declaration can break a build it is no excuse to the rest of
your team that you decided to use those constructs just because you feel
you ought to be able to.

Perhaps one day C99 will be more broadly supported. That day is not
today, and your C90 code will compile fine on a conforming C99 compiler
when that day comes.

Seems like a no-brainer to me.
user923005
Guest
 
Posts: n/a
#18: Apr 10 '07

re: Segmentation Fault....


This version has several corrections. However, you should get rid of
the two goto's with a do or while control structure. Also, the way
that it calculates standard deviation is not good. You should examine
"The Art of Computer Programming", Volume 2, page 232 for the
'Welford' method of computing standard deviation. I would highly
recommend adding a function which calculates the standard deviation
using that method. Did you notice that I took the sum calculations
out of the loop? This is very important. Also, initializing the
count to 1 before you have counted anything is a serious mistake.

#include <assert.h>
#include <stdio.h>
#include <math.h>
//------------------------------------------------------
// Programming II - Coursework Assignment 1-4
// This program calculates the following :
// - Sum of numbers
// - Sum of numbers squared
//- Mean
// - Mean of numbers squared
// - Standard Deviation
//- Error in mean of a set of numbers
//------------------------------------------------------
// Note: This version differs in the previous version.
// 1.)Arrays are used to store and be read
// 2.)Two functions are introduced sum_x, sum_x_squared
// 3.)Array refuses to accept more values
//------------------------------------------------------
//Functions
static double sum_x(const double x[20], int n)
{
int i;
double Result = 0;

// Calculation for - Sum of numbers
for (i = 0; i < n; i++) {
Result += x[i];
}
return Result;
}

static double sum_x_squared(const double x[20], int n)
{
int i;
double Result = 0;

// Calculation for - Sum of the numbers squared
for (i = 0; i < n; i++) {
Result += x[i] * x[i];
}
return Result;
}

int main()
{
int NumOfNum; // Number "n" of numbers to be entered
int i; // For-loop counter
double Array[20]; // Array to store numbers
double Sum_X = 0; // Sum of numbers
double Sum_X2 = 0; // Sum of the numbers squared
double Mean_1 = 0; // Mean
double Mean_2 = 0; // Mean of the numbers squared
double SD = 0; // Standard Deviation
double Error = 0; // Error in mean
int Count = 0; // Never tell a lie in your code.
int converted; // How many things got converted

// Introduction
printf("\nThis program calculates the following:\n");
printf("- Sum of numbers\n- Sum of numbers squared\n- Mean\n- Mean
of numbers squared\n");
printf("- Standard Deviation\n- Error in mean of a set of numbers\n
\n");
// Number "n" of numbers
redo:
printf("To obtain all of the above, you first need to enter the
number \"n\" \n");
printf("of numbers to be entered. Followed by the number itself.
\n");
converted = scanf("%d", &NumOfNum);
if (converted != 1) goto redo;
if (NumOfNum 20)
{
puts("This test program only allows 20 numbers.");
puts("Redimention Array[20] to a larger size.");
goto redo;
}
if (NumOfNum <= 0)
{
puts("This test program must input at least one number.");
goto redo;
}
// Adds numbers into array & calculation for sum of numbers
for (i = 0; i < NumOfNum; i++) {
redo2:
printf("\nEnter the value for number %d \n", i+1);
converted = scanf(" %lf", &Array[i]);
if (converted != 1) goto redo2;
Count++;
}
assert(Count == NumOfNum);
// Uses the function sum_x
Sum_X = sum_x(Array, NumOfNum);
// Uses the function sum_x_squared
Sum_X2 = sum_x_squared(Array, NumOfNum);
// Calculation for - Mean
Mean_1 = Sum_X / NumOfNum;
// Calculation for - Mean of the squares
Mean_2 = Sum_X2 / NumOfNum;
// Calculation for - Standard Deviation
SD = sqrt(Mean_2 - Mean_1 * Mean_1);
// Calculation for - Error in mean
Error = SD / sqrt((double)NumOfNum);
// Displays All Results
printf("\n\nTable of Results:\n");
printf("---------------------------------------------------\n");
printf("The Sum of numbers is: \t\t\t%.2f\n", Sum_X);
printf("The Sum of the numbers squared is: \t%.2f\n", Sum_X2);
printf("The Mean is: \t\t\t\t%.2f\n", Mean_1);
printf("The Mean of the numbers squared is: \t%.2f\n", Mean_2);
printf("The Stanard Deviation is: \t\t%.2f\n", SD);
printf("The Error in the mean is: \t\t%.2f\n\n", Error);
return 0;
}

Clever Monkey
Guest
 
Posts: n/a
#19: Apr 10 '07

re: Segmentation Fault....


jacob navia wrote:
Quote:
Charlton Wilbur a écrit :
Quote:
Quote:
>>>>>>"jn" == jacob navia <jacob@jacob.remcomp.frwrites:
>>
>>
> jnCharlton Wilbur a écrit :
>>
Quote:
> >First, // comments were only included in the C99 standard.
> >While they are technically standard, using them is more often
> >than not a sign of sloppy thinking about C and a lack of
> >awareness of C standards, which is likely to reflect poorly on
> >the programmer and irritate the most helpful people here (who
> >care deeply about what is standard and non-standard in C).
>>
> jnLook Mr, if you "care deeply about what is standard and
> jnnon-standard" please stop talking nonsense.
>>
>Quoting chapter and verse that establishes that what I said in the
>first line is correct does not invalidate what I said in the
>following five lines.
>>
> jnThe C99 standard is the current standard. Others have only an
> jnhistorical interest.
>>
>Find me a fully conforming C99 compiler and standard library in
>widespread use, and I'll believe you.
Quote:
// comments are accepted by most C compilers: microsoft, gcc, IBM,
and almost all main compilers accept them. This feature is really almost
universal by now.
>
Select conforming feature(s) != conforming implementation. This seems
to be the crux of what people are trying to teach you.

As suggested else-thread, I have nightlies that will break if you insist
on using C99 features (including the comment format you insist is so
widely accepted we can ignore any objections otherwise) accepted in some
marginally conforming implementation. You insist that "all main
compilers accept them" but this is not my experience. Those conforming
features do not always match up with each-other, so you have a situation
where you can have a number of C90+some features implementations
consuming the same code.

This is a recipe for disaster.
Quote:
What is strange is that all this people that always speak about
"standard C" do not really mean standard C but some other standard
like C95, C89, or maybe even K&R C from 1975???
>
C90 /is/ a standard. It is the standard most people find in the field.
It really makes no difference that you don't like this.
Quote:
It is already 8 years since 1999, and maybe it is time to look around
a bit?
>
The age of a standard means little to the millions of lines of code we
have to maintain over the years. Stable code is a Good Thing, and
adoption of C99 /by the system and compiler implementers/ has been slow
and incomplete. Why introduce risk over something you cannot control?
Quote:
Or you prefer living in the past, as many people here?
>
The day a significant compiler can claim full support of the required
parts of the C99 standard then you will see newer code being written to
support that.

It has nothing to do with the age of a standard. A decade is nothing in
terms of software maintenance, especially when expressed in such a
robust and well understood language like C. Change incurs risk, and
unless there are significant gains, that risk is simply not worth it.
We have a situation where few shops /need/ C99, and therefore there are
few implementers willing to undertake the expensive operation of
conforming to that standard. The fact that implementations have slowly
started to conform over the years doesn't change the fact that few
people need anything C99 brings.

Since C99 implementations will not break conforming C90 code, why should
the majority of us care how a comment is formed?

One thing you seem to be missing is that many shops have coding
standards, and a lot of code will already have decades of changes,
including well-formed comments. Just because C99 allows // does not
mean anyone needs to use it. At my shop comments have a specific
format, and even when we move to C99 (on all platforms, so don't hold
your breath) we will not change that because it would mean reformatting
all sorts of existing comments.
Richard Heathfield
Guest
 
Posts: n/a
#20: Apr 10 '07

re: Segmentation Fault....


jacob navia said:
Quote:
Richard Heathfield a écrit :
Quote:
>jacob navia said:
Quote:
>>>Excuse me, I have yet to find a C compiler that does NOT accept
>>>// comments.
>>
>I get diagnostic messages for // comments from all of the following
>compilers (among others) when they are invoked in their conforming
>mode:
>>
>Turbo C 2.01
BRAVO Heathfield:
According to http://dn.codegear.com/article/20841
Antique Software: Turbo C version 2.01
Ship date: 11-May-1989
>
Quote:
>Borland C 4.5, 5.02
The official name is
Borland C++ 5.02.
Whatever. I was merely making it clear that I was talking about the C
compiler functionality of that software.
Quote:
You have surely tweaked the compiler to
put it in some configuration that doesn't accept //
Yes, it's called "conforming mode".
Quote:
I have compiled with that compiler and it will accept //
in C mode.
It is required to generate a diagnostic message, and it meets that
requirement when invoked correctly.

Quote:
Quote:
>Microsoft Visual C 1.0, 1.5, 2.0, 5.0, 6.0
>
It accepts those comments since at least
1995 or even earlier. In any case, since 1995
they are accepted in C mode. This is just a lie.
No, I would not go so far as to say that you're lying - I think you just
don't understand about the concept of conformance. All of those
versions of Visual C will produce the required diagnostic message when
presented with a syntax error such as // if you invoke them in
conforming mode.
Quote:
Quote:
>gcc
>
Gcc accepts // comments in C mode unles you force it not to.
When invoked in conforming mode, gcc produces a diagnostic message if
presented with a syntax error such as //.
Quote:
This is a lie too.
No, you're probably not lying. You probably believe your claims. But
they are still false claims.
Quote:
Quote:
>This is, of course, a non-exhaustive list. I don't recall ever
>encountering a conforming C compiler that didn't diagnose // comments
>when invoked in conforming mode.
>
Conforming to WHAT?
The de facto C Standard.
Quote:
Obviously conforming to C89.
Right.
Quote:
Many compilers have a flag that
makes them conforming to C89, even lcc-win32 has one. But this
is just another lie Hethfield.
You mean it's a lie that lcc-win32 has a C89-conforming mode? Well, I
must bow to your lcc-win32 experience there.
Quote:
Quote:
>In fact, if it didn't diagnose them,
>it wouldn't be a conforming C compiler (unless it conformed to C99,
>of course, but there are precious few compilers that do that).
>
IBM does (In the power pc version of their compiler for instance)
IBM does indeed advertise a C99-conforming compiler, but it's hardly
what I would call widely-used. And I note that they don't appear to
mention whether they have a conforming library to go with it. Maybe
their use of the word "compiler" is intended to encompass the library,
and maybe it isn't.
Quote:
gcc does
No, it doesn't. See http://gcc.gnu.org/c99status.html
Quote:
and many other compilers have adopted C99.
Actually, very few conform to it.
Quote:
But if you want to live in the past, or in some world that
stopped spinning in 1989 you are welcome.
Now you're just being silly. When C99 arrives, fine, let's embrace it.
But it isn't here yet, for practical purposes.
Quote:
But do not pretend that would be standard C.
C90 remains the de facto standard. I do not expect you to understand
this.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Flash Gordon
Guest
 
Posts: n/a
#21: Apr 10 '07

re: Segmentation Fault....


jacob navia wrote, On 10/04/07 19:09:
Quote:
Richard Heathfield a écrit :
Quote:
>jacob navia said:
>>
><snip>
>>
Quote:
>>Excuse me, I have yet to find a C compiler that does NOT accept
>>// comments.
>>
>>
>I get diagnostic messages for // comments from all of the following
>compilers (among others) when they are invoked in their conforming mode:
>>
>Turbo C 2.01
BRAVO Heathfield:
According to http://dn.codegear.com/article/20841
Antique Software: Turbo C version 2.01
Ship date: 11-May-1989
Still in use.
Quote:
Quote:
>Borland C 4.5, 5.02
The official name is
Borland C++ 5.02. You have surely tweaked the compiler to
put it in some configuration that doesn't accept //
>
I have compiled with that compiler and it will accept //
in C mode.
Not in conforming C mode.
Quote:
Quote:
>Microsoft Visual C 1.0, 1.5, 2.0, 5.0, 6.0
>
It accepts those comments since at least
1995 or even earlier. In any case, since 1995
they are accepted in C mode. This is just a lie.
Not in conforming C mode.
Quote:
Quote:
>gcc
>
Gcc accepts // comments in C mode unles you force it not to.
This is a lie too.
Not when told to conform to the only standard it claims to fully conform to.
Quote:
Quote:
>This is, of course, a non-exhaustive list. I don't recall ever
>encountering a conforming C compiler that didn't diagnose // comments
>when invoked in conforming mode.
>
Conforming to WHAT?
You are being disingenuous at best, since you know that all of those
compilers only fully conform to one standard.
Quote:
Obviously conforming to C89. Many compilers have a flag that
makes them conforming to C89, even lcc-win32 has one. But this
is just another lie Hethfield.
No, it is the absolute truth.
Quote:
Quote:
>In fact, if it didn't diagnose them, it wouldn't be a conforming C
>compiler (unless it conformed to C99, of course, but there are
>precious few compilers that do that).
>
>
IBM does (In the power pc version of their compiler for instance)
gcc does
and many other compilers have adopted C99.
gcc has a mode that fully conforms to C89 and a mode that according to
its developers does not fully conform to C99. However, you know this.
Quote:
But if you want to live in the past, or in some world that
stopped spinning in 1989 you are welcome.
>
But do not pretend that would be standard C.
You have mentioned one compiler that I think has a full C99 mode (maybe
two if you have finished implementing yours, including the library). All
the others, including all the ones that a lot of us use, either do not
fully support C99 or do not support it at all.

Personally I would very much like to move to C99 since there are a few
things I would like to use, however I have to allow for compatibility
with MS compilers. Many others have good reasons for not being able to
use C99.
--
Flash Gordon
Malcolm McLean
Guest
 
Posts: n/a
#22: Apr 10 '07

re: Segmentation Fault....



"jacob navia" <jacob@jacob.remcomp.frwrote in message
news:461badd0$0$5100$ba4acef3@news.orange.fr...
Quote:
Daniel Rudy a écrit :
Quote:
>>
>Those comments are C++ style. Are you coding in C or C++? This group
>is for C. If you want C++, then head to the room down the hall.
>>
>
This is nonsense.
>
// comments are standard C!!!
>
>
We don't have a standard any longer. I included some slash slash comments in
my code on the basis that if any extension was now standard, that one was,
only to find them break on an MPI compiler. It also uses Fortran 77.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Daniel Rudy
Guest
 
Posts: n/a
#23: Apr 10 '07

re: Segmentation Fault....


At about the time of 4/10/2007 8:22 AM, Nomad.C@gmail.com stated the
following:
Quote:
On 10 Apr, 16:09, Daniel Rudy <spamt...@spamthis.netwrote:
Quote:
>Those comments are C++ style. Are you coding in C or C++? This group
>is for C. If you want C++, then head to the room down the hall.
>>
>As for your code....See questions 4.11 and various questions in 6 in the
>FAQ. http://c-faq.com/
>>
>>
>>
Quote:
>>any advice would be grateful thanks
>>Chris
>--
>Daniel Rudy
>>
>Email address has been base64 encoded to reduce spam
>Decode email address using b64decode or uudecode -m
>>
>Why geeks like computers: look chat date touch grep make unzip
>strip view finger mount fcsk more fcsk yes spray umount sleep
>
Thanks for the tip about scanf..can't believe it was a simple wrong
type......
I don't think C/C++ comments make much difference....
>
I wasn't the one who came up with the scanf issue, but the C++ style
comments prevented your code from compiling on my system.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Daniel Rudy
Guest
 
Posts: n/a
#24: Apr 10 '07

re: Segmentation Fault....


At about the time of 4/10/2007 8:32 AM, jacob navia stated the following:
Quote:
Nomad.C@gmail.com a écrit :
Quote:
>I don't think C/C++ comments make much difference....
>>
>
// comments are standard C. Mr Rudy is completely wrong.
Really? Then why will the code not compile on gcc using -ansi -std=c89?


--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Daniel Rudy
Guest
 
Posts: n/a
#25: Apr 10 '07

re: Segmentation Fault....


At about the time of 4/10/2007 10:04 AM, jacob navia stated the following:
Quote:
Richard Heathfield a écrit :
Quote:
>jacob navia said:
>>
>>
Quote:
>>Charlton Wilbur a écrit :
>>>
>>>>>>>>"N" == Nomad C@gmail com <Nomad.C@gmail.comwrites:
>>>>
>>> NThanks for the tip about scanf..can't believe it was a simple
>>> Nwrong type...... I don't think C/C++ comments make much
>>> Ndifference....
>>>>
>>>They do if you want help from this group, for two reasons.
>>>>
>>>First, // comments were only included in the C99 standard. While
>>>they are technically standard, using them is more often than not a
>>>sign of sloppy thinking about C and a lack of awareness of C
>>>standards, which is likely to reflect poorly on the programmer and
>>>irritate the most helpful people here (who care deeply about what is
>>>standard and non-standard in C).
>>Look Mr, if you "care deeply about what is standard and non-standard"
>>please stop talking nonsense.
>>
>He's not talking nonsense. He makes good points, only one of which you
>have left unsnipped.
>>
>>
Quote:
>>The C standard 6.4.9: Comments
>>>
>>Except within a character constant, a string literal, or a comment,
>>the characters // introduce a comment [...]
>>
>Yes, you're right - C99 introduced // comments. Nobody is disputing
>this.
>>
>>
Quote:
>>The C99 standard is the current standard.
>>
>Nobody is disputing that either. It's just that almost nobody uses the
>C99 Standard in daily life. It's still a C90 world.
>>
Excuse me, I have yet to find a C compiler that does NOT accept
// comments.
>
Please show me *some* example for those...
gcc on Unix with these options:

gcc -W -Wall -pedantic -ansi -std=c89


--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Ian Collins
Guest
 
Posts: n/a
#26: Apr 10 '07

re: Segmentation Fault....


Daniel Rudy wrote:
Quote:
At about the time of 4/10/2007 8:32 AM, jacob navia stated the following:
>
Quote:
>>Nomad.C@gmail.com a écrit :
>>
Quote:
>>>I don't think C/C++ comments make much difference....
>>>
>>
>>// comments are standard C. Mr Rudy is completely wrong.
>
>
Really? Then why will the code not compile on gcc using -ansi -std=c89?
>
>
Because they are legal in the current standard, not the old one.

--
Ian Collins.
Ian Collins
Guest
 
Posts: n/a
#27: Apr 10 '07

re: Segmentation Fault....


Daniel Rudy wrote:
Quote:
At about the time of 4/10/2007 10:04 AM, jacob navia stated the following:
Quote:
>>
>>Excuse me, I have yet to find a C compiler that does NOT accept
>>// comments.
>>
>>Please show me *some* example for those...
>
gcc on Unix with these options:
>
gcc -W -Wall -pedantic -ansi -std=c89
>
So it doesn't accept them if you explicitly tell it not to. Wow, I'm
impressed.

--
Ian Collins.
Daniel Rudy
Guest
 
Posts: n/a
#28: Apr 10 '07

re: Segmentation Fault....


At about the time of 4/10/2007 8:31 AM, jacob navia stated the following:
Quote:
Daniel Rudy a écrit :
Quote:
>Those comments are C++ style. Are you coding in C or C++? This group
>is for C. If you want C++, then head to the room down the hall.
>>
>
This is nonsense.
>
// comments are standard C!!!
>
jacob
Then explain this:

strata:/home/dr2867/c/test 1031 $$$ ->more -N test1.c
1 #include <stdio.h>
2 #include <math.h>
3
4 //------------------------------------------------------
5 // Programming II - Coursework Assignment 1-4
6 // This program calculates the following :
7 // - Sum of numbers
8 // - Sum of numbers squared
9 //- Mean
10 // - Mean of numbers squared
11 // - Standard Deviation
12 //- Error in mean of a set of numbers
13 //------------------------------------------------------
14 // Note: This version differs in the previous version.
15 // 1.)Arrays are used to store and be read
16 // 2.)Two functions are introduced vsum, vsum2
17 // 3.)Array refuses to accept more values
18 //------------------------------------------------------
19
20 //Functions


Using c89 standard:

strata:/home/dr2867/c/test 1032 $$$ ->gcc -W -Wall -pedantic -ansi
-std=c89 -o test1 test1.c
test1.c:4: error: syntax error before '/' token
strata:/home/dr2867/c/test 1033 $$$ ->


Using C90 standard:

strata:/home/dr2867/c/test 1034 $$$ ->gcc -W -Wall -pedantic
-std=iso9899:199409 -o test1 test1.c
test1.c:4: error: syntax error before '/' token
strata:/home/dr2867/c/test 1035 $$$ ->


Looks pretty conclusive to me. Just because you are using a standard
that may or may not be supported by any one compiler, doesn't mean that
the rest of us have to. I perfer c89 for all my stuff, and then I use
C90 when I need to use long long or unsigned long long.


--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Daniel Rudy
Guest
 
Posts: n/a
#29: Apr 10 '07

re: Segmentation Fault....


At about the time of 4/10/2007 2:19 PM, Ian Collins stated the following:
Quote:
Daniel Rudy wrote:
Quote:
>At about the time of 4/10/2007 10:04 AM, jacob navia stated the following:
Quote:
>>Excuse me, I have yet to find a C compiler that does NOT accept
>>// comments.
>>>
>>Please show me *some* example for those...
>gcc on Unix with these options:
>>
>gcc -W -Wall -pedantic -ansi -std=c89
>>
So it doesn't accept them if you explicitly tell it not to. Wow, I'm
impressed.
>
Not you too.

I always use -std=c89 -ansi options when I compile my code. Sometimes I
use -std=iso9899:199409 for C90 standard when I need it. I don't need
or have the desire to code to C99, especially when it's only partly
implemented.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Ian Collins
Guest
 
Posts: n/a
#30: Apr 10 '07

re: Segmentation Fault....


Daniel Rudy wrote:
Quote:
At about the time of 4/10/2007 8:31 AM, jacob navia stated the following:
>
Quote:
>>Daniel Rudy a écrit :
>>
Quote:
>>>Those comments are C++ style. Are you coding in C or C++? This group
>>>is for C. If you want C++, then head to the room down the hall.
>>>
>>
>>This is nonsense.
>>
>>// comments are standard C!!!
>>
>>jacob
>
Then explain this:
>
strata:/home/dr2867/c/test 1031 $$$ ->more -N test1.c
1 #include <stdio.h>
2 #include <math.h>
cc y.c
"y.c", line 1: syntax error before or at: 1
"y.c", line 1: invalid source character: '#'
"y.c", line 1: warning: old-style declaration or incorrect type for: include
"y.c", line 1: warning: old-style declaration or incorrect type for: stdio
"y.c", line 1: warning: old-style declaration or incorrect type for: h

--
Ian Collins.
Army1987
Guest
 
Posts: n/a
#31: Apr 10 '07

re: Segmentation Fault....



"Daniel Rudy" <spamthis@spamthis.netha scritto nel messaggio
news:8gTSh.2172$Q23.1981@newssvr17.news.prodigy.ne t...
Quote:
Looks pretty conclusive to me. Just because you are using a standard
that may or may not be supported by any one compiler, doesn't mean that
the rest of us have to. I perfer c89 for all my stuff, and then I use
C90 when I need to use long long or unsigned long long.
You meant C99. C90 is practically the same as C89.


jacob navia
Guest
 
Posts: n/a
#32: Apr 10 '07

re: Segmentation Fault....


Ian Collins a écrit :
Quote:
Daniel Rudy wrote:
>
Quote:
>>At about the time of 4/10/2007 10:04 AM, jacob navia stated the following:
>>
Quote:
>>>Excuse me, I have yet to find a C compiler that does NOT accept
>>>// comments.
>>>
>>>Please show me *some* example for those...
>>
>>gcc on Unix with these options:
>>
>>gcc -W -Wall -pedantic -ansi -std=c89
>>
>
So it doesn't accept them if you explicitly tell it not to. Wow, I'm
impressed.
>
Me too. It is impressing how apparently intelligent people
will use completely moronic arguments:

:-)

But this is hopeless.



Daniel Rudy
Guest
 
Posts: n/a
#33: Apr 10 '07

re: Segmentation Fault....


At about the time of 4/10/2007 2:30 PM, Army1987 stated the following:
Quote:
"Daniel Rudy" <spamthis@spamthis.netha scritto nel messaggio
news:8gTSh.2172$Q23.1981@newssvr17.news.prodigy.ne t...
>
Quote:
>Looks pretty conclusive to me. Just because you are using a standard
>that may or may not be supported by any one compiler, doesn't mean that
>the rest of us have to. I perfer c89 for all my stuff, and then I use
>C90 when I need to use long long or unsigned long long.
>
You meant C99. C90 is practically the same as C89.
>
>
No, I meant c89. I explicitly use -ansi -std=c89 for most of my C code.



--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
jacob navia
Guest
 
Posts: n/a
#34: Apr 10 '07

re: Segmentation Fault....


Daniel Rudy a écrit :
Quote:
At about the time of 4/10/2007 8:31 AM, jacob navia stated the following:
>
Quote:
>>Daniel Rudy a écrit :
>>
Quote:
>>>Those comments are C++ style. Are you coding in C or C++? This group
>>>is for C. If you want C++, then head to the room down the hall.
>>>
>>
>>This is nonsense.
>>
>>// comments are standard C!!!
>>
>>jacob
>
>
Then explain this:
>
strata:/home/dr2867/c/test 1031 $$$ ->more -N test1.c
1 #include <stdio.h>
2 #include <math.h>
3
4 //------------------------------------------------------
5 // Programming II - Coursework Assignment 1-4
6 // This program calculates the following :
7 // - Sum of numbers
8 // - Sum of numbers squared
9 //- Mean
10 // - Mean of numbers squared
11 // - Standard Deviation
12 //- Error in mean of a set of numbers
13 //------------------------------------------------------
14 // Note: This version differs in the previous version.
15 // 1.)Arrays are used to store and be read
16 // 2.)Two functions are introduced vsum, vsum2
17 // 3.)Array refuses to accept more values
18 //------------------------------------------------------
19
20 //Functions
>
>
Using c89 standard:
>
strata:/home/dr2867/c/test 1032 $$$ ->gcc -W -Wall -pedantic -ansi
-std=c89 -o test1 test1.c
test1.c:4: error: syntax error before '/' token
strata:/home/dr2867/c/test 1033 $$$ ->
>
>
Using C90 standard:
>
strata:/home/dr2867/c/test 1034 $$$ ->gcc -W -Wall -pedantic
-std=iso9899:199409 -o test1 test1.c
test1.c:4: error: syntax error before '/' token
strata:/home/dr2867/c/test 1035 $$$ ->
>
>
Looks pretty conclusive to me.
Yes. If you tell gcc not to use the current standard but some other old
one it will not accept standard syntax.

WOW, how clever you are.
Quote:
Just because you are using a standard
that may or may not be supported by any one compiler, doesn't mean that
the rest of us have to.
I am speaking about standard C. Nobody forces you to use anything
and if you do not like standrad C I do not care. But I do not want
to allow that you pass YOUR preferences as standard C. They are not.


Quote:
I perfer c89 for all my stuff, and then I use
C90 when I need to use long long or unsigned long long.
>
>

Great!!!

But that is not standard C. Clear?
Mark McIntyre
Guest
 
Posts: n/a
#35: Apr 10 '07

re: Segmentation Fault....


On 10 Apr 2007 07:45:55 -0700, in comp.lang.c , "Nomad.C@gmail.com"
<Nomad.C@gmail.comwrote:
Quote:
>Hi
>I'm coding this simple program, but I get a segmentation fault, I'm
>pretty sure that the arrays are big enough and there isn't really any
>buffer overflows
Apart from the off-by-one errors....
Quote:
>double vsum(double x[20], int n){
> int i;
> float Result = 0;
>
> //Calculation for - Sum of numbers
> for (i=1;i<=n;i++){
> Result += x[i];
C arrays start from zero. If n = 20 then this will write to the 21st
element of x, which doesn't exist. Additionally you're not including
the first element of the array in your sum...

Of course its /possible/ to write VB-style 1-based arrays in C, but
seriously, this is a Bad Idea as no other C programmer will do it.
Quote:
> int NumOfNum; // Number "n" of numbers to be entered
....
Quote:
> scanf(" %f", &NumOfNum);
format specifier incorrect.
Quote:
>
> //Adds numbers into array & calculation for sum of numbers
> for (i=0;i<=NumOfNum;i++){
you want to loop to Less than NumofNum - think about it.
Quote:
> Mean_1 = Sum_1 / NumOfNum;
The format specifier error means that NumofNum is probably zero,
and...

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mark McIntyre
Guest
 
Posts: n/a
#36: Apr 10 '07

re: Segmentation Fault....


On Tue, 10 Apr 2007 17:32:08 +0200, in comp.lang.c , jacob navia
<jacob@jacob.remcomp.frwrote:
Quote:
>Nomad.C@gmail.com a écrit :
Quote:
>I don't think C/C++ comments make much difference....
>>
>
>// comments are standard C. Mr Rudy is completely wrong.
He's not /completely/ wrong. The vast majority of extant C compilers
will reject C++ style comments when invoked in conforming mode, since
tehy conform to C89. A few, such as recentish releases of gcc, will
accept them since tehy conform to C99.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mark McIntyre
Guest
 
Posts: n/a
#37: Apr 10 '07

re: Segmentation Fault....


On Tue, 10 Apr 2007 18:27:31 +0200, in comp.lang.c , jacob navia
<jacob@jacob.remcomp.frwrote:
Quote:
>The C99 standard is the current standard. Others have only an historical
>interest.
Given that its likely that the /overwhelming/ majority of C compilers
in commercial use don't conform to C99, that remark seems absurd.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Ian Collins
Guest
 
Posts: n/a
#38: Apr 10 '07

re: Segmentation Fault....


Daniel Rudy wrote:
Quote:
At about the time of 4/10/2007 2:19 PM, Ian Collins stated the following:
>
Quote:
>>Daniel Rudy wrote:
>>
Quote:
>>>At about the time of 4/10/2007 10:04 AM, jacob navia stated the following:
>>>
>>>>Excuse me, I have yet to find a C compiler that does NOT accept
>>>>// comments.
>>>>
>>>>Please show me *some* example for those...
>>>
>>>gcc on Unix with these options:
>>>
>>>gcc -W -Wall -pedantic -ansi -std=c89
>>>
>>
>>So it doesn't accept them if you explicitly tell it not to. Wow, I'm
>>impressed.
>>
>
Not you too.
>
Yes, me too.
Quote:
I always use -std=c89 -ansi options when I compile my code. Sometimes I
use -std=iso9899:199409 for C90 standard when I need it. I don't need
or have the desire to code to C99, especially when it's only partly
implemented.
>
I don't realy care about comment style, it's up to the individual
developer which they use and to which standard they want their code to
conform. So I don't rant and rail about such trivia and I don't make
futile attempts to impose my choice of standard on others.

--
Ian Collins.
Mark McIntyre
Guest
 
Posts: n/a
#39: Apr 10 '07

re: Segmentation Fault....


On Tue, 10 Apr 2007 19:04:07 +0200, in comp.lang.c , jacob navia
<jacob@jacob.remcomp.frwrote:
Quote:
>Richard Heathfield a écrit :
Quote:
>>
>Nobody is disputing that either. It's just that almost nobody uses the
>C99 Standard in daily life. It's still a C90 world.
>>
>Excuse me, I have yet to find a C compiler that does NOT accept
>// comments.
Jacob, please don't be so obtuse. It just makes you look foolish.
Quote:
>Please show me *some* example for those...
Anything from Microsoft, for example, when invoked in conforming mode.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mark McIntyre
Guest
 
Posts: n/a
#40: Apr 10 '07

re: Segmentation Fault....


On Tue, 10 Apr 2007 20:09:20 +0200, in comp.lang.c , jacob navia
<jacob@jacob.remcomp.frwrote:
Quote:
>Richard Heathfield a écrit :
Quote:
>jacob navia said:
>>
Quote:
>>>Excuse me, I have yet to find a C compiler that does NOT accept
>>>// comments.
>>
>
Quote:
>Microsoft Visual C 1.0, 1.5, 2.0, 5.0, 6.0
>
>It accepts those comments since at least
>1995 or even earlier. In any case, since 1995
>they are accepted in C mode. This is just a lie.
Not one of the above compilers will accept // comments *if invoked as
a conforming C compiler*. There's no option to engage C99 compliance
either, since they all predate that standard.
Quote:
Quote:
>gcc
>
>Gcc accepts // comments in C mode unles you force it not to.
Incorrect. Even gcc 4.x will emit an error if you tell it to conform
to ANSI or C89 standards.
Quote:
>This is a lie too.
One day, someone is going to call you on this, and you'll end up in
court paying damages for libel or slander or whatever it is.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mark McIntyre
Guest
 
Posts: n/a
#41: Apr 10 '07

re: Segmentation Fault....


On Wed, 11 Apr 2007 09:19:26 +1200, in comp.lang.c , Ian Collins
<ian-news@hotmail.comwrote:
Quote:
>Daniel Rudy wrote:
>
Quote:
>gcc -W -Wall -pedantic -ansi -std=c89
>>
>So it doesn't accept them if you explicitly tell it not to.
Ok, pedants corner: it accepts them if you allow it to engage language
extensions.

What, you thought that gcc invoked without any arguments was a
C99-conformant compiler ? Wow.
Quote:
>Wow, I'm impressed.
Allow me to sell you some snake oil, if you're *that* easily
impressed.



--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Richard Heathfield
Guest
 
Posts: n/a
#42: Apr 10 '07

re: Segmentation Fault....


jacob navia said:
Quote:
I am speaking about standard C.
Fine, but you're speaking about a standard that almost nobody uses in
the real world. The de facto standard remains C90. I believe I am right
in saying that not even your own compiler conforms to C99.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Richard Tobin
Guest
 
Posts: n/a
#43: Apr 10 '07

re: Segmentation Fault....


In article <ui1o13ts2o0rbvb1727ec3vnakudi3pn6v@4ax.com>,
Mark McIntyre <markmcintyre@spamcop.netwrote:
Quote:
Quote:
>>This is a lie too.
Quote:
>One day, someone is going to call you on this, and you'll end up in
>court paying damages for libel or slander or whatever it is.
No, it's just rhetoric. No-one's reputation is going to be damaged by
Jacob calling something a lie in a flame about about C standards.
Except Jacob's of course, and even in the US you probably can't sue
yourself.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
jacob navia
Guest
 
Posts: n/a
#44: Apr 10 '07

re: Segmentation Fault....


Mark McIntyre a écrit :
Quote:
On Tue, 10 Apr 2007 20:09:20 +0200, in comp.lang.c , jacob navia
<jacob@jacob.remcomp.frwrote:
>
>
Quote:
>>Richard Heathfield a écrit :
>>
Quote:
>>>jacob navia said:
>>>
>>>
>>>>Excuse me, I have yet to find a C compiler that does NOT accept
>>>>// comments.
>>>
>>>Microsoft Visual C 1.0, 1.5, 2.0, 5.0, 6.0
>>
>>It accepts those comments since at least
>>1995 or even earlier. In any case, since 1995
>>they are accepted in C mode. This is just a lie.
>
>
Not one of the above compilers will accept // comments *if invoked as
a conforming C compiler*. There's no option to engage C99 compliance
either, since they all predate that standard.
>
Fine. The examples (as I stated above) do not prove anything since they
are obsolete versions of of compilers that now do accept // comments.

This means that Heathfield examples are not any examples of a *current*
version of a compiler.
Quote:
>
Quote:
Quote:
>>>gcc
>>
>>Gcc accepts // comments in C mode unles you force it not to.
>
>
Incorrect. Even gcc 4.x will emit an error if you tell it to conform
to ANSI or C89 standards.
>
>
Quote:
>>This is a lie too.
>
>
One day, someone is going to call you on this, and you'll end up in
court paying damages for libel or slander or whatever it is.
>
libel?
slander?

I did not insult anyone. I just call a lie a lie, since it is
misleading, and misleading people *is* lying. ALL those examples
are wrong, since if invoked in a mode where C89 compliance is NOT
required they wil all accept // comments, except those like Turbo C 2.0
that predate the invention of // comments!
Richard Heathfield
Guest
 
Posts: n/a
#45: Apr 11 '07

re: Segmentation Fault....


jacob navia said:

<snip>
Quote:
The examples (as I stated above) do not prove anything since
they are obsolete versions of of compilers that now do accept //
comments.
Not in conforming mode, they don't.

<snip>
Quote:
I did not insult anyone. I just call a lie a lie, since it is
misleading, and misleading people *is* lying. ALL those examples
are wrong, since if invoked in a mode where C89 compliance is NOT
required they wil all accept // comments,
If an implementation is invoked in a non-conforming mode, that
invocation is off-topic in clc. When invoked in conforming mode, all
the compilers I mentioned issue the necessary diagnostic message for a
// syntax error.
Quote:
except those like Turbo C
2.0 that predate the invention of // comments!
Typical Navia nonsense. // comments go at least as far back as BCPL,
which pre-dates C, let alone Turbo C.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Ian Collins
Guest
 
Posts: n/a
#46: Apr 11 '07

re: Segmentation Fault....


jacob navia wrote:
Quote:
Mark McIntyre a écrit :
>
Quote:
>On Tue, 10 Apr 2007 20:09:20 +0200, in comp.lang.c , jacob navia
><jacob@jacob.remcomp.frwrote:
>>
Quote:
>>Richard Heathfield a écrit :
>>>
>>>jacob navia said:
>>>>
>>>>Excuse me, I have yet to find a C compiler that does NOT accept
>>>>// comments.
>>>>
>>>Microsoft Visual C 1.0, 1.5, 2.0, 5.0, 6.0
>>>
>>It accepts those comments since at least
>>1995 or even earlier. In any case, since 1995
>>they are accepted in C mode. This is just a lie.
>>
>Not one of the above compilers will accept // comments *if invoked as
>a conforming C compiler*. There's no option to engage C99 compliance
>either, since they all predate that standard.
>
Fine. The examples (as I stated above) do not prove anything since they
are obsolete versions of of compilers that now do accept // comments.
>
This means that Heathfield examples are not any examples of a *current*
version of a compiler.
Quote:
>>
This argument, as ever, will go nowhere.

There are those who prefer the more widely supported earlier standards
either for maximum portability or due to Luddite tendencies and there
are those who prefer the current standard or widely used extensions.

Neither way is the true path to enlightenment, so to each his or her own.

--
Ian Collins.
Flash Gordon
Guest
 
Posts: n/a
#47: Apr 11 '07

re: Segmentation Fault....


Daniel Rudy wrote, On 10/04/07 22:34:
Quote:
At about the time of 4/10/2007 2:30 PM, Army1987 stated the following:
Quote:
>"Daniel Rudy" <spamthis@spamthis.netha scritto nel messaggio
>news:8gTSh.2172$Q23.1981@newssvr17.news.prodigy.n et...
>>
Quote:
>>Looks pretty conclusive to me. Just because you are using a standard
>>that may or may not be supported by any one compiler, doesn't mean that
>>the rest of us have to. I perfer c89 for all my stuff, and then I use
>>C90 when I need to use long long or unsigned long long.
>You meant C99. C90 is practically the same as C89.
>
No, I meant c89. I explicitly use -ansi -std=c89 for most of my C code.
long long is not part of C89 or C90. In fact, the difference between C89
and C90 is the paragraph numbering, so a compiler that conforms to one
by definition conforms to the other.

<OT>
Using both -ansi and -std=c89 is pointless since the two switches mean
the same thing in gcc.
</OT>
--
Flash Gordon
jacob navia
Guest
 
Posts: n/a
#48: Apr 11 '07

re: Segmentation Fault....


Richard Heathfield a écrit :
Quote:
>
If an implementation is invoked in a non-conforming mode, that
invocation is off-topic in clc. When invoked in conforming mode, all
the compilers I mentioned issue the necessary diagnostic message for a
// syntax error.
>
CONFORMING TO WHAT?????

Not to the C standard of course, but to the standard that
guru heathfield likes and prescribes for the masses.

And if I discuss standard C this is off topic in clc because
standard C is off topic here. What counts is the opinion of the
local guru...

"non-conforming mode" means for heathfield non conforming to
an obsolete standard even if it is conforming to the C standard.

And obviously if I point out that Mr Guru is wrong, all the
hell breaks loose and each and every one of the sect will
start screaming

HERESY HERESY...

:-)

It is so ridiculous this stuff.

If a compiler accepts a feature defined in the standard it
is running in "NON CONFORMING MODE".

Yeah. Obvious.

Gurus are always right.
jacob navia
Guest
 
Posts: n/a
#49: Apr 11 '07

re: Segmentation Fault....


Ian Collins a écrit :
Quote:
jacob navia wrote:
>
Quote:
>>Mark McIntyre a écrit :
>>
>>
Quote:
>>>On Tue, 10 Apr 2007 20:09:20 +0200, in comp.lang.c , jacob navia
>>><jacob@jacob.remcomp.frwrote:
>>>
>>>
>>>>Richard Heathfield a écrit :
>>>>
>>>>
>>>>>jacob navia said:
>>>>>
>>>>>
>>>>>>Excuse me, I have yet to find a C compiler that does NOT accept
>>>>>>// comments.
>>>>>
>>>>>Microsoft Visual C 1.0, 1.5, 2.0, 5.0, 6.0
>>>>
>>>>It accepts those comments since at least
>>>>1995 or even earlier. In any case, since 1995
>>>>they are accepted in C mode. This is just a lie.
>>>
>>>Not one of the above compilers will accept // comments *if invoked as
>>>a conforming C compiler*. There's no option to engage C99 compliance
>>>either, since they all predate that standard.
>>
>>Fine. The examples (as I stated above) do not prove anything since they
>>are obsolete versions of of compilers that now do accept // comments.
>>
>>This means that Heathfield examples are not any examples of a *current*
>>version of a compiler.
>>
This argument, as ever, will go nowhere.
>
There are those who prefer the more widely supported earlier standards
either for maximum portability or due to Luddite tendencies and there
are those who prefer the current standard or widely used extensions.
>
Neither way is the true path to enlightenment, so to each his or her own.
>
I would accept that, but heathfield insists that anything different from
C89 is "off topic in clc". He is convinced that he can dictate what
can be discussed here.

In the other answer in this same thread he says:

<quoting>
If an implementation is invoked in a non-conforming mode, that
invocation is off-topic in clc. When invoked in conforming mode, all
the compilers I mentioned issue the necessary diagnostic message for a
// syntax error.
< end quote>

With "non conforming" he means in a mode that accepts standard
comments...
With "conforming mode" he means "conforming to MY favorite version of
the standard even if it is obsolete"

And anything else is off topic.

I can accept that heathfield has problems with C99 or that he doesn't
like blondies and only does it with brunettes. Who cares about his
likes and dislikes?

He can expose its reasons, I mean I have never accused anybody here with
"off topic" nonsense.

But what I do not accept is this "this is off topic in clc.
// comments are wrong."

NONSENSE I repeat!
Gregor H.
Guest
 
Posts: n/a
#50: Apr 11 '07

re: Segmentation Fault....


On Wed, 11 Apr 2007 01:16:49 +0200, jacob navia <jacob@jacob.remcomp.fr>
wrote:
Quote:
>
Gurus are always right.
>
Sure. That's why THEY are Gurus, and WE are only // well forget about that.

Say, Jacob, how's about the C99 conformance of lcc-win32? (I'm just curious.
I'm a frequent user of lcc-win32.)


G.

--

E-mail: info<at>simple-line<Punkt>de
Closed Thread