# Segmentation Fault....

Discussion in 'C Programming' started by Nomad.C@gmail.com, Apr 10, 2007.

1. ### Guest

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;
}
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;
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 = 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

, Apr 10, 2007

2. ### Malcolm McLeanGuest

<> wrote in message
news:...
> 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;
> }
> 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;
> 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

> //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 = 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

Malcolm McLean, Apr 10, 2007

3. ### bytebroGuest

On 10 Apr, 15:45, "" <> wrote:
>
> 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...

bytebro, Apr 10, 2007
4. ### Daniel RudyGuest

At about the time of 4/10/2007 7:45 AM, stated the
following:
> 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/

>
> 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

Daniel Rudy, Apr 10, 2007
5. ### Guest

On 10 Apr, 16:09, Daniel Rudy <> wrote:
> At about the time of 4/10/2007 7:45 AM, stated the
> following:
>
>
>
> > 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/
>
>
>
> > 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....

, Apr 10, 2007
6. ### jacob naviaGuest

Daniel Rudy a écrit :
>
> 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.

jacob

jacob navia, Apr 10, 2007
7. ### jacob naviaGuest

a écrit :
> I don't think C/C++ comments make much difference....
>

// comments are standard C. Mr Rudy is completely wrong.

jacob navia, Apr 10, 2007
8. ### Charlton WilburGuest

>>>>> "N" == Nomad C@gmail com <> writes:

N> Thanks for the tip about scanf..can't believe it was a simple
N> wrong type...... I don't think C/C++ comments make much
N> difference....

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
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

Charlton

--
Charlton Wilbur

Charlton Wilbur, Apr 10, 2007
9. ### jacob naviaGuest

Charlton Wilbur a écrit :
>>>>>>"N" == Nomad C@gmail com <> writes:

>
>
> N> Thanks for the tip about scanf..can't believe it was a simple
> N> wrong type...... I don't think C/C++ comments make much
> N> difference....
>
> 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"

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.

jacob navia, Apr 10, 2007
10. ### Richard HeathfieldGuest

jacob navia said:

> Charlton Wilbur a écrit :
>>>>>>>"N" == Nomad C@gmail com <> writes:

>>
>>
>> N> Thanks for the tip about scanf..can't believe it was a simple
>> N> wrong type...... I don't think C/C++ comments make much
>> N> difference....
>>
>> 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"

He's not talking nonsense. He makes good points, only one of which you
have left unsnipped.

>
> 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.

> 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.

> 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.

Richard Heathfield, Apr 10, 2007
11. ### jacob naviaGuest

Richard Heathfield a écrit :
> jacob navia said:
>
>
>>Charlton Wilbur a écrit :
>>
>>>>>>>>"N" == Nomad C@gmail com <> writes:
>>>
>>>
>>> N> Thanks for the tip about scanf..can't believe it was a simple
>>> N> wrong type...... I don't think C/C++ comments make much
>>> N> difference....
>>>
>>>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"

>
>
> He's not talking nonsense. He makes good points, only one of which you
> have left unsnipped.
>
>
>>
>>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.
>
>
>>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

Please show me *some* example for those...

jacob navia, Apr 10, 2007
12. ### Charlton WilburGuest

>>>>> "jn" == jacob navia <> writes:

jn> Charlton Wilbur a écrit :

>> 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).

jn> Look Mr, if you "care deeply about what is standard and
jn> non-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.

jn> The C99 standard is the current standard. Others have only an
jn> historical interest.

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

Charlton

--
Charlton Wilbur

Charlton Wilbur, Apr 10, 2007
13. ### jacob naviaGuest

Charlton Wilbur a écrit :
>>>>>>"jn" == jacob navia <> writes:

>
>
> jn> Charlton Wilbur a écrit :
>
> >> 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).

>
> jn> Look Mr, if you "care deeply about what is standard and
> jn> non-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.
>
> jn> The C99 standard is the current standard. Others have only an
> jn> historical 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

jacob navia, Apr 10, 2007
14. ### Richard HeathfieldGuest

jacob navia said:

<snip>

> Excuse me, I have yet to find a C compiler that does NOT accept

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, Apr 10, 2007
15. ### Richard HeathfieldGuest

jacob navia said:

> Charlton Wilbur a écrit :
>>>>>>>"jn" == jacob navia <> writes:

>>

<snip>

>> jn> The C99 standard is the current standard. Others have only an
>> jn> historical 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.

> 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.

Richard Heathfield, Apr 10, 2007
16. ### jacob naviaGuest

Richard Heathfield a écrit :
> jacob navia said:
>
> <snip>
>
>>Excuse me, I have yet to find a C compiler that does NOT accept

>
>
> 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

> 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.

> 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.

> gcc

Gcc accepts // comments in C mode unles you force it not to.
This is a lie too.

>
> 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.

> 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.

jacob navia, Apr 10, 2007
17. ### Clever MonkeyGuest

jacob navia wrote:
> Richard Heathfield a écrit :
>> jacob navia said:
>>
>> <snip>
>>
>>> Excuse me, I have yet to find a C compiler that does NOT accept

>>
>>
>> I get diagnostic messages for // comments from all of the following
>> compilers (among others) when they are invoked in their conforming mode:
>>

[...]
>> 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.

Clever Monkey, Apr 10, 2007
18. ### user923005Guest

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;
}
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 * x;
}
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);
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;
}

user923005, Apr 10, 2007
19. ### Clever MonkeyGuest

jacob navia wrote:
> Charlton Wilbur a écrit :
>>>>>>> "jn" == jacob navia <> writes:

>>
>>
>> jn> Charlton Wilbur a écrit :
>>
>> >> 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).

>>
>> jn> Look Mr, if you "care deeply about what is standard and
>> jn> non-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.
>>
>> jn> The C99 standard is the current standard. Others have only an
>> jn> historical 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.
>

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.

> 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.

> 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?

> 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

Clever Monkey, Apr 10, 2007
20. ### Richard HeathfieldGuest

jacob navia said:
> Richard Heathfield a écrit :
>> jacob navia said:
>>>Excuse me, I have yet to find a C compiler that does NOT accept

>>
>> 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
>
>> 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.

> You have surely tweaked the compiler to
> put it in some configuration that doesn't accept //

Yes, it's called "conforming mode".

> 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.

>> 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.

>> 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 //.

> This is a lie too.

No, you're probably not lying. You probably believe your claims. But
they are still false claims.

>> 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.

> Obviously conforming to C89.

Right.

> 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.

>> 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.

> gcc does

No, it doesn't. See http://gcc.gnu.org/c99status.html

> and many other compilers have adopted C99.

Actually, very few conform to it.

> 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.

> 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.

Richard Heathfield, Apr 10, 2007