分段故障.... [英] Segmentation Fault....
问题描述
嗨
我正在编写这个简单的程序,但我得到一个分段错误,我是
非常确定阵列足够大而且那里是不是真的没有
缓冲区溢出所以为什么会发生这种情况?
程序如下:
#include< stdio.h>
#include< math.h>
// ------------ ------------------------------------------
//编程II - 课程作业1-4
//此程序计算以下内容:
// - 数字总和
// - 平方数的平方
// - 平均值
// - 平均数的平均值
// - 标准偏差
// - 一组数字的误差
// -------------------------- ----------------------------
//注意:此版本与之前版本不同/>
// 1.)数组用于存储和读取
// 2.)引入两个函数vsum,vsum2
// 3.)数组拒绝接受更多值
// ----------------------- -------------------------------
//功能
double vsum(double x [20],int n){
int i;
float Result = 0;
//计算 - 数字总和
for(i = 1; i< = n; i ++){
结果+ = x [i];
}
返回结果;
}
double vsum2(double x [20],int n){
int i;
float结果= 0;
浮点数;
//计算 - 平方数的总和
for(i = 1; i< n + 1; i ++){
Number = x [i];
结果+ = pow(数字,2);
}
返回结果;
}
int main()
{
int NumOfNum; //数字n要输入的数字
int i; // For循环计数器
double Array [20]; //存储数字的数组
float Sum_1 = 0; //数字总和
浮点数Sum_2 = 0; //数字平方和
浮点Mean_1 = 0; //意思是
浮点Mean_2 = 0; //平均数的平均值
浮点数SD = 0; //标准偏差
float错误= 0; //错误意味着
浮点数= 0;
int计数= 1;
//简介
printf(\ n该程序计算以下内容:\ n);
printf(" - 数字总和\ n-数字平方数\ n-平均值\\ n-平均值
数字平方数\ n;)
printf(& - 标准偏差\ n-一组数字均值错误\ n
\ n");
//数字n数字
printf("要获得以上所有内容,首先需要输入
数字\" n \" \ n") ;
printf(要输入的数字。后跟数字本身。\ n);
scanf("%f",& NumOfNum );
//将数字添加到数组和数组中数字和的计算
for(i = 0; i< = NumOfNum; i ++){
printf(" \ n输入数字%d \\的值) n",Count);
scanf("%f",& Number);
数组[i] =数字;
//使用函数vsum
Sum_1 = vsum(数组,NumOfNum);
//使用函数vsum2
Sum_2 = vsum2(数组,NumOfNum);
点数++;
}
//计算 - 平均值
Mean_1 = Sum_1 / NumOfNum;
//计算 - 平方均值
Mean_2 = Sum_2 / NumOfNum;
//计算 - 标准偏差
SD = sqrt(Mean_2 - pow(Mean_1,2));
//计算 - 误差平均值
错误= SD / sqrt(NumOfNum);
//显示所有结果
printf(" \ n \ nNable of Results:\ n");
printf(" -------------------------------------- ------------- \ n");
printf(&qu ot;数字的总和是:\t\t\t%。2f \ n",Sum_1);
printf("数字平方的总和是:\ t%.2f \ n",Sum_2);
printf(均值为:\ t \ tt \ t \ t%。2f \ n,Mean_1) ;
printf(平均数字的平均值是:\t%.2f \ n,平均值为2);
printf(The Stanard Deviation是:\t\t%。2f \ n",SD);
printf(错误的意思是:\t\t%.2f \ n \ n",错误);
返回0;
}
任何建议都将不胜感激
Chris
< No ***** @ gmail.comwrote在消息中
新闻:11 ********************** @ y80g2000hsf.googlegr oups.com ...
< blockquote class =post_quotes>
嗨
我正在编写这个简单的程序,但我得到了一个分段错误,我是
非常肯定Ť数组足够大,并且没有任何
缓冲区溢出,为什么会发生这种情况?
程序如下:
#include< stdio.h>
#include< math.h>
// ---- --------------------------------------------------
//编程II - 课程作业1-4
//这个程序计算如下:
// - 数字总和
// - 平方数字总和
// - 平均值
// - 平均数字平均值
// - 标准偏差
// - 一组数字的误差
// ------------------ ------------------------------------
//注意:此版本与以前的版本不同。
// 1.)数组用于存储和读取
// 2.)引入两个函数vsum,vsum2
// 3.)数组拒绝接受更多值
// ------------------------ ------------------------------ <无线电通信/>
//函数
double vsum(double x [20],int n){
int i;
float结果= 0;
//计算 - 数字总和
for(i = 1; i< = n; i ++){
结果+ = x [i];
}
返回结果;
}
double vsum2(double x [20],int n){
int i;
float Result = 0;
浮点数;
//计算 - 平方数的总和
for(i = 1; i< n + 1; i ++){
Number = x [i];
结果+ = pow(数字,2);
}
返回结果;
}
int main()
{
int NumOfNum; //数字n要输入的数字
int i; // For循环计数器
double Array [20]; //存储数字的数组
float Sum_1 = 0; //数字总和
浮点数Sum_2 = 0; //数字平方和
浮点Mean_1 = 0; //意思是
浮点Mean_2 = 0; //平均数的平均值
浮点数SD = 0; //标准偏差
float错误= 0; //错误意味着
浮点数= 0;
int计数= 1;
//简介
printf(\ n该程序计算以下内容:\ n);
printf(" - 数字总和\ n-数字平方数\ n-平均值\\ n-平均值
数字平方数\ n;)
printf(& - 标准偏差\ n-一组数字均值错误\ n
\ n");
//数字n数字
printf("要获得以上所有内容,首先需要输入
数字\" n \" \ n") ;
printf(要输入的数字。后跟数字本身。\ n);
scanf("%f",& NumOfNum );
这可能是问题,因为你要求scanf()写一个浮点数给
一个整数。它可能会在这里崩溃,更多liley float和int是32位,并且它b / b写了一个被解释为NumOfNum巨大值的数字
//将数字添加到数组&数字和的计算
for(i = 0; i< = NumOfNum; i ++){
printf(" \ n输入数字%d \\的值) n",Count);
scanf("%f",& Number);
数组[i] =数字;
//使用函数vsum
Sum_1 = vsum(数组,NumOfNum);
//使用函数vsum2
Sum_2 = vsum2(数组,NumOfNum);
点数++;
}
//计算 - 平均值
Mean_1 = Sum_1 / NumOfNum;
//计算 - 平方均值
Mean_2 = Sum_2 / NumOfNum;
//计算 - 标准偏差
SD = sqrt(Mean_2 - pow(Mean_1,2));
//计算 - 误差平均值
错误= SD / sqrt(NumOfNum);
//显示所有结果
printf(" \ n \ nNable of Results:\ n");
printf(" -------------------------------------- ------------- \ n");
printf(" ;数字之和为:\t\t\t%。2f \ n",Sum_1);
printf("数字平方和为:\ t %。2f \ n",Sum_2);
printf(均值为:\t \ t \ t \ t%。2f \ n,Mean_1);
printf(平均数字的平均值是:\t%.2f \ n,平均值为2);
printf(The Stanard Deviation is :\t\t%。2f \ n",SD);
printf(错误的意思是:\t\t%.2f\ nn \\ n,错误);
返回0;
}
任何建议都将不胜感激,谢谢
Chris
-
免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm
>
4月10日15:45,Noma ... @ gmail.com < Noma ... @ gmail.comwrote:
>
我正在编写这个简单的程序,但我得到了一个分段错误,我是
非常确定数组足够大并且实际上没有任何
缓冲区溢出所以为什么会发生这种情况?
使用gcc -Wall编译好给出:
xc:函数`main'':
xc:78:警告:浮点格式,不同类型arg(arg 2)
指向的行:
scanf("%f"& NumOfNum);
正在读取一个int的双倍。
如果我是你,我会从那里开始...
大约在2007年4月4日上午7:45,没有***** @ gmail .com 声明
以下:
嗨
我编码这个简单程序,但是我得到了一个分段错误,我是
非常确定数组足够大并且实际上没有任何
缓冲区溢出所以为什么是发生这种情况?
程序如下:
#include< stdio.h>
#include< ; math.h>
// ------------------------------- -----------------------
//编程II - 课程作业1-4
//此程序计算以下内容:
// - 数字总和
// - 平方数之和
// - 平均值
// - 平方数的平均值
// - 标准偏差
// - 一组数字的误差
// --------------------- ---------------------------------
//注意:这个版本的不同之处在于以前的版本。
// 1.)数组用于存储和读取
// 2.)引入两个函数vsum,vsum2
// 3.)数组拒绝接受更多值
// --------------------------- ---------------------------
这些评论是C ++风格。你用C或C ++编码吗?这个小组
是给C的。如果你想要C ++,那就去大厅的房间。
至于你的代码....查看问题4.11和
FAQ中的6个问题。 http://c-faq.com/
>
任何建议都将不胜感谢
Chris
-
Daniel Rudy
电子邮件地址已经过base64编码以减少垃圾邮件
使用b64decode或uudecode -m
为什么极客喜欢电脑:看聊天日期触摸grep make unzip
条视图手指挂载fcsk更多fcsk是喷雾umount睡眠
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
<No*****@gmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr 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
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...
At about the time of 4/10/2007 7:45 AM, No*****@gmail.com 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
这篇关于分段故障....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!