473,656 Members | 2,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Segmentation Fault....

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<=NumOfNu m;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\nTab le 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 '07 #1
157 5193

<No*****@gmail. comwrote in message
news:11******** **************@ y80g2000hsf.goo glegroups.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<=NumOfNu m;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\nTab le 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

Apr 10 '07 #2
On 10 Apr, 15:45, "Noma...@gmail. com" <Noma...@gmail. comwrote:
>
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...

Apr 10 '07 #3
At about the time of 4/10/2007 7:45 AM, No*****@gmail.c om 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
Apr 10 '07 #4
On 10 Apr, 16:09, Daniel Rudy <spamt...@spamt his.netwrote:
At about the time of 4/10/2007 7:45 AM, Noma...@gmail.c om 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 '07 #5
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.

// comments are standard C!!!

jacob
Apr 10 '07 #6
No*****@gmail.c om a écrit :
I don't think C/C++ comments make much difference....
// comments are standard C. Mr Rudy is completely wrong.
Apr 10 '07 #7
>>>>"N" == Nomad C@gmail com <No*****@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
cw*****@chromat ico.net
Apr 10 '07 #8
Charlton Wilbur a écrit :
>>>>>>"N" == Nomad C@gmail com <No*****@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.
Apr 10 '07 #9
jacob navia said:
Charlton Wilbur a écrit :
>>>>>>>"N" == Nomad C@gmail com <No*****@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.
>
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.
Apr 10 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6801
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script works fine and do all what I need. But at the end of the execution, I can read "Segmentation Fault". The segmentation fault appear at the end of my script execution,
3
1939
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers: /var/www/cgi-bin/script.cgi when i debug the program i get Segmentation fault gdb ./script.cgi
16
8976
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those who don't speak french arbre means tree.
3
11428
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc () from /lib/tls/libc.so.6 It's really strange; I just call malloc() like "tmp=malloc(size);" the system gives me Segmentation fault I want to write a code to do like a dynamic array, and the code is as
5
2990
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I understood that a segmentation fault can occur whenever I declare a pointer and I leave it un-initialized. So I thought the problem here is with the (const char *)s in the stuct flightData (please note that I get the same fault declaring as char * the...
18
26094
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)? Thanks.
27
3348
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! #include <stdlib.h> #include <stdio.h> typedef struct _node_t {
7
5873
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our testcases we experiance Segmentation fault from the python libraries. If i know how to handle the exception for Segmentation fault , it will help me complete the run on any testcase , even if i experiance Seg Fault due to any one or many functions in...
3
5170
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection from 10g release2 PHP is configured with the following parameters './configure' '--prefix=/opt/oracle/php' '--with-apxs=/opt/oracle/apache/bin/apxs' '--with-config-file-path=/opt/oracle/apache/conf' '--enable-safe-mode' '--enable-session'...
6
5035
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on gcc. The only difference is that in first I only call "main" and in second call
0
8380
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8296
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8497
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8598
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.