N
Nomad.C
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
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