LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-24-2010, 12:54 AM   #1
vjramana
Member
 
Registered: Sep 2009
Posts: 89

Rep: Reputation: 0
Question segmentation fault


Dear Sir/Madam,

I have two files which contains 30 real numbers in each file.The file names are DATA1real.dat and DATA1real.dat. I want to count the occurences of these numbers in pair in a two dimensional bins.

The code that I have written functions well for integer type of data but it fails for real type of data. So to convert the data type from real to integer I used NINT function. But it still fails.

I can compile the code without any error but when I run it it appears a message as "Segmentation fault". There is no other messages which could help. I couldn't figure out what is the wrong with this code.

I have given the two data and the code as well. Please help me to fix this problem. Thank you in advance.

DATA1real.dat = 3.1, 2.0, 3.6, 4.5, 3.2, 2.0, 1.8, 2.9, 3.2, 3.6, 4.1, 5.2, 4.6, 3.0, 2.9, 1.4, 1.5, 1.6, 2.9, 3.7, 4.6, 5.4, 5.9, 5.5, 5.4, 5.2, 5.1, 5.3, 4.9, 4.5

DATA2real.dat = 3.2, 4.9, 3.4, 2.6, 2.6, 1.4, 1.9, 2.7, 3.2, 4.6, 2.6, 4.8, 3.4, 3.9, 4.1, 3.2, 2.6, 3.4, 3.9, 1.1, 2.9, 1.9, 1.1, 3.6, 2.7, 3.5, 2.6, 3.4, 1.7, 4.9

The code is :
program surveyDATA
implicit none
!
! Define variables
integer :: i, j, n, k
integer :: total, dataX, dataY
integer :: experience, level
integer,parameter :: bilData=30
integer,dimension(5,4) :: survey
real,dimension(bilData) :: arrData1, arrData2
integer,dimension(bilData) :: arrData11, arrData22
!
! Initialize the array
do i = 1,5
do j= 1,4
survey(i,j) = 0
end do
end do
!
! Oepn files
open(unit=51,file="DATA1real.dat")
read(51,*) (arrData1(i), i=1, bilData)
!write(*,*) (arrData1(i), i=1, bilData)
!
open(unit=52,file="DATA2real.dat")
read(52,*) (arrData2(j), j=1, bilData)
!write(*,*) (arrData2(j), j=1, bilData)
!
!do k = 1, bilData
! arrData11(k) = NINT(arrData1(k))
! arrData22(k) = NINT(arrData2(k))
!end do

! As responses is read increase the count in table position
do i = 1, bilData
do j = 1, bilData
dataX = NINT(arrData1(i))
dataY = NINT(arrData2(j))
! print*, dataX, dataY
survey(dataX, dataY) = survey(dataX, dataY) + 1
end do
end do
!
! Print out the survey resluts

print*,"************* Results *****************"
do i = 1,5
print*, survey(i,1), survey(i,2), survey(i,3), survey(i,4)
end do
!

! Calculate the total data
total=0.0
do i = 1,5
do j = 1,4
total = total + survey(i,j)
end do
end do
print*, "Total numbers of datas are, ", total

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
end program surveyDATA
 
Old 08-24-2010, 06:39 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
I don't know Fortran. If my answer isn't what you need, try editing the title of your post to mention Fortran, so you attract the attention of someone who does know it.

Edit your first post in the thread, then click the "Advanced" button, then you can edit the thread title. I think the title should be
segmentation fault in Fortran program

Quote:
Originally Posted by vjramana View Post
DATA1real.dat = 3.1, 2.0, 3.6, 4.5, 3.2, 2.0, 1.8, 2.9, 3.2, 3.6, 4.1, 5.2, 4.6, 3.0, 2.9, 1.4, 1.5, 1.6, 2.9, 3.7, 4.6, 5.4, 5.9, 5.5, 5.4, 5.2, 5.1, 5.3, 4.9, 4.5

DATA2real.dat = 3.2, 4.9, 3.4, 2.6, 2.6, 1.4, 1.9, 2.7, 3.2, 4.6, 2.6, 4.8, 3.4, 3.9, 4.1, 3.2, 2.6, 3.4, 3.9, 1.1, 2.9, 1.9, 1.1, 3.6, 2.7, 3.5, 2.6, 3.4, 1.7, 4.9
Some of the numbers in DATA1 are 5.5 or larger and some of the numbers in DATA2 are larger than 4.5.

NINT rounds to the nearest int. It doesn't truncate to the next lower.

Quote:
integer,dimension(5,4) :: survey
So you will get first subscripts up to 6 and second subscripts up to 5, but you only allowed for 5,4.

Quote:
dataX = NINT(arrData1(i))
dataY = NINT(arrData2(j))
! print*, dataX, dataY
survey(dataX, dataY) = survey(dataX, dataY) + 1
Did you intend to use some function that truncates instead of NINT?
Or do you need survey to be bigger?

Last edited by johnsfine; 08-24-2010 at 06:43 AM.
 
1 members found this post helpful.
Old 08-24-2010, 09:43 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You definitely need to step through your program to determine exactly WHERE the segmentation violation is occurring.

If you're using a Gnu FORTRAN compiler (for example, "g77"), then you can use "gdb".

Even if you don't set breakpoints, step through your code, or display variables (all very useful things to do with any debugger), running inside of gdb will at least give you a LINE NUMBER (to help you locate the source of the problem).

Debuggers are your friend

IMHO .. PSM
 
1 members found this post helpful.
Old 08-24-2010, 10:35 PM   #4
vjramana
Member
 
Registered: Sep 2009
Posts: 89

Original Poster
Rep: Reputation: 0
Yea,
Thanks to your for suggestion. I realised that the error was due to the mismatching size of the array. I solved the problem ready.

Regards
Vijay
 
Old 08-25-2010, 12:16 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by vjramana View Post
...
I solved the problem ready.
In which case you can mark your thread as solved using the thread tools above the first post.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation Fault? Mol_Bolom Programming 4 12-14-2008 02:16 AM
Segmentation fault cmplet-noobie Programming 3 04-03-2006 02:52 AM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
segmentation fault ice99 Linux - Software 1 08-19-2005 11:33 AM
Segmentation fault velda.ebel Linux - Security 1 08-08-2005 07:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration