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 it out that'd be great. BTW, the code isn't completely finished (ie the file to write on isn't being written on yet) but this doesn't effect this segmentation fault as far as I know.
I'm using Linux Red Hat to run the program and Emacs to compile it.
Syntax: - void swap (float *a, float *b);
-
-
int main ( )
-
{
-
-
float diam[20], a, b;
-
int swap_count=0, n;
-
-
FILE *fptr1, *fptr2;
-
fptr1=fopen("a10.dat", "r");
-
fptr2=fopen("a12.txt", "w");
-
-
fscanf(fptr1,"%f", &diam[n]);
-
-
do
-
{
-
for(n=0; n<=19; n++)
-
{
-
diam[n]=a;
-
diam[n+1]=b;
-
if(a>b)
-
{
-
swap_count++;
-
void swap (float a, float b);
-
}
-
else
-
{
-
a=a;
-
b=b;
-
swap_count=0;
-
}
-
}
-
}while(swap_count>=1);
-
-
printf("Diameter=%f\n", diam[n]);
-
-
-
fclose (fptr1);
-
fclose (fptr2);
-
}
-
-
void swap (float *a, float *b)
-
{
-
float temp;
-
-
temp=*a; *a=*b; *b=temp;
-
-
}
8 5808
This may not be causing your segfault, but when using the swap function, you should not say "void swap(float a, float b);" This is creating a new function named swap that takes two floats as arguments. You should instead use "swap(a, b);", which will actually use the swap function you created with your variables a and b.
It's right here:
for(n=0; n<=19; n++)
{
diam[n]=a;
diam[n+1]=b;
etc...\
When n is 19, then n+1 is 20. Your array is 20 which means the largest index is 19. That is, diam[20] is outside your array bound.
Hence the seg fault.
So to fix that I should just replace the n<=19 with a different number such as....err...20? I'm not exactly sure how to fix this.
I tried different numbers in place of 19 (such at 18-21) and still get the segmentation fault.
i am not sure what you are trying to achieve...but i guess 24. void swap (float a, float b); should be swap(&a,&b)
1. u haven't initialized 'a' and 'b' so in the for loop..it should be
a=diam[n];
b=diam[n+1];
2. function swap(&a,&b);
The program is supposed to read in 20 values from a file and put them in an array. Then it is supposed to sort these values from smallest to largest using the swap function to compare individual values and change them.
I tried do what was listed above but to no avail. The error I get when I change void swap (float a, float b) to swap (&a, &b) is this:
error: cannot convert `float*' to `float' for argument `1' to `voidswap(float, float)'
Help?
tried different numbers in place of 19 (such at 18-21) and still get the segmentation fault.
Your calculated index value must be in the range 0-19. Those are your 20 array elements.
error: cannot convert `float*' to `float' for argument `1' to `voidswap(float, float)'
I have no idea what that's about. It compiles fine for me as swap(&A,&b).
That error looks like you have swap that has float arguments and not float* arguments. You didn't change that and not tell us, did you??
The program is supposed to read in 20 values from a file and put them in an array. Then it is supposed to sort these values from smallest to largest using the swap function to compare individual values and change them.
I tried do what was listed above but to no avail. The error I get when I change void swap (float a, float b) to swap (&a, &b) is this:
error: cannot convert `float*' to `float' for argument `1' to `voidswap(float, float)'
Help?
Hi,
the program will not suppose to do, what you expected, i am seeing lot of mistake here.
1)
fscanf(fptr1,"%f", &diam[n]);
what will happen ,when "n" is a junk val, because not inited.
2) what are you tring to achive
diam[n]=a;
diam[n+1]=b;
3) Wrong statement
void swap (float a, float b);
Different compiler may say different error, but it is not valied atleast here.
4) what are you doing here..
a=a;
b=b;
i hope now you know , what is you requiremnet , so write write your own code.
Regards
Arul
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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:...
|
by: Peter Dragun |
last post by:
I am generally new to programming under Unix, but know how to code under
Windows. I have to create a simple program, that takes the following
information from a file using redirection:
4 4 4 4 4...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: jr.freester |
last post by:
I have created to classes Matrix and System. System is made up of
type matrix.
----------------------------------------------------------------------------------
class Matrix
{
private:
int...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |