473,386 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

segmentation error when using fscanf

I am trying to output uniform numbers into another file and it runs into segmentation faut on the bolded line below.. Maybe it runs out of memory?.? or is it just too much looping and functions around?.?
----------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

main(){
double *x, rst[1];
unsigned int n, i;
double *dmalloc(unsigned int);
void MCG(double *, unsigned int);
void CatFO(double *, unsigned int, double *);
void outputdata(double *, unsigned int);
char ans;

printf("Enter the number of uniform number you want to generate: ");
scanf("%u", &n);

x=dmalloc(n);
MCG(x,n);

for(i=0;i<n;i++){
printf("%2.10f ", x[i]);
}

CatFO(x,n,rst);

printf("\n\n The mean is %10.10f, and the variance is %10.10f\n\n", rst[0],rst[1]);
printf("\n Would you like to save the random numbers to a file (Yes -> y, No -> else)? :");
scanf("%s", &ans);

if (ans == 'y' || ans == 'Y'){
outputdata(x,n);}

}

void outputdata(double *x, unsigned int n){
unsigned int i;
FILE *outfile;
char filename[FILENAME_MAX];
printf("\n What is the name of the file? : ");
scanf("%s", &filename);
outfile = fopen(filename, "w");
if (outfile == NULL){
printf("Can't open output file!\n");
exit(1);
}
for (i=0;i<n;i++){
fprintf(outfile, "%2.10f ", x[i]);
}
fclose(outfile);
}


void MCG(double *x, unsigned int n){
unsigned int i, y , a = 572629L , m= UINT_MAX;
y = a * (m-1)/2;
for(i=0; i<n; i++){
y = a*y ;
x[i]= (double)y /((double)m + 1) ;
}
}

double *dmalloc(unsigned int n){
double *x;
x = (double *)malloc((unsigned int)(n*sizeof(double)));
if (x==NULL){
printf("\nError: couldn't allocate %u double\n",n);
exit(1);
}
return(x);
}

void CatFO(double *x, unsigned int n, double *rst){
unsigned int i;
rst[0]=0., rst[1]=0.;
for(i=0; i<n; i++){
rst[0] += x[i]-x[0];
rst[1] += (x[i]-x[0])*(x[i]-x[0]);
}
rst[1] = (rst[1]-rst[0]*rst[0]/(double)n)/(double)(n-1);
rst[0] = rst[0]/(double)n + x[0];
}
Nov 21 '06 #1
1 1862
horace1
1,510 Expert 1GB
in function CatFO() you access array elements rst[0] and rst[1] which requires two elements to be allocated to rst. In main() you only allocate 1 which results in memory corruption when you execute CatFO(), i.e.
Expand|Select|Wrap|Line Numbers
  1. double *x, rst[1];
  2.  
it should be
Expand|Select|Wrap|Line Numbers
  1. double *x, rst[2];
  2.  
Nov 22 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: picknicker187 | last post by:
hi, the program below is supposed to read data out of a txt file into an array and the second part of the data to a linked list. 8 is the number of words, the 10 terms below represent...
5
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...
1
by: Nasser | last post by:
Hello, I am coding a mathematical model with VC++. When I debug the code, I face with no erroe, but during executing, I face with below error "Unhandled exception at 0x0040c275 in Tar.exe:...
8
by: SP | last post by:
The following code crashes after I add the two nested FOR loops at the end, I am starting to learn about pointers and would like to understand what I'm doing wrong. I think the problem is the way...
3
by: wastedhello | last post by:
hi im farely new to programming, doing it for university, but i cant figure out why this isnt working. it comes up with segmentation faults. can anyone help #include <stdio.h> #include...
1
by: psrujan | last post by:
Hi , I have little bit of knowledge in C++. When I run the following program its gives me segmentation error. I tried to do all the possible things by reading online but not able to figure out...
5
by: lancer6238 | last post by:
Dear all, I'm trying to implement the Smith-Waterman algorithm for DNA sequence alignment. I'm aligning a 2040 bp query sequence against a 5040 bp sequence. I'll be trying to implement a parallel...
1
by: Dameon99 | last post by:
Hi, I have just spent many hours debugging my code and now when I run it I get the following error: "An access violation (Segmentation fault) raised in your program" I have researched on this...
8
by: parkskier | last post by:
Ok, so I got a segmentation fault when I tried to run the program I created. I understand what this error means, but I don't know where it is. If someone can quickly look through my code and point...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.