Velocity Reviews > Segmentation Fault....

# Segmentation Fault....

Guest
Posts: n/a

 04-10-2007
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

 04-10-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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

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

 04-10-2007
On 10 Apr, 15:45, "(E-Mail Removed)" <(E-Mail Removed)> 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...

Daniel Rudy
Guest
Posts: n/a

 04-10-2007
At about the time of 4/10/2007 7:45 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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

Guest
Posts: n/a

 04-10-2007
On 10 Apr, 16:09, Daniel Rudy <(E-Mail Removed)> wrote:
> At about the time of 4/10/2007 7:45 AM, (E-Mail Removed) 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....

jacob navia
Guest
Posts: n/a

 04-10-2007
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
Guest
Posts: n/a

 04-10-2007
(E-Mail Removed) a écrit :
> 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

 04-10-2007
>>>>> "N" == Nomad C@gmail com <(E-Mail Removed)> 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
(E-Mail Removed)

jacob navia
Guest
Posts: n/a

 04-10-2007
Charlton Wilbur a écrit :
>>>>>>"N" == Nomad C@gmail com <(E-Mail Removed)> 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.

Richard Heathfield
Guest
Posts: n/a

 04-10-2007
jacob navia said:

> Charlton Wilbur a écrit :
>>>>>>>"N" == Nomad C@gmail com <(E-Mail Removed)> 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.