|
|
Up |
|
|
  |
Author: BammBamm Date: Mar 6, 2008 02:29
Hi,
I have no fortran background at all, but I do have programming
background in other languages. Unfortunately, our teacher instructed
us to write our program in Fortran. Our compiler is g77 under Linux.
Our problem was to print out the twos complement of an 8-bit binary
integer. My approach is to consider it as a character * 8, where each
character can only have either "1" or "0" (I can do the error
checking, so no problem with that).
I plan to loop through the 8 characters, and replace each 0 with 1 and
vice versa, and then add one which means doing a "carry 1" from the
rightmost character to the left if the character is 1. I have done a
quick prototype in JavaScript (in an html page) and in quickbasic
(running under dosemu), but my ability to write it in fortran is
limited by my lack of knowledge in it.
Specifically, I need a way to find (and replace) the nth character in
a string. The rought equivalent of the substring function in
javascript or the Mid function in Basic.
|
| Show full article (1.19Kb) |
|
| | 34 Comments |
|
  |
Author: David JonesDavid Jones Date: Mar 6, 2008 02:47
Bamm wrote:
> Hi,
>
> I have no fortran background at all, but I do have programming
> background in other languages. Unfortunately, our teacher instructed
> us to write our program in Fortran. Our compiler is g77 under Linux.
>
> Our problem was to print out the twos complement of an 8-bit binary
> integer. My approach is to consider it as a character * 8, where each
> character can only have either "1" or "0" (I can do the error
> checking, so no problem with that).
>
> I plan to loop through the 8 characters, and replace each 0 with 1 and
> vice versa, and then add one which means doing a "carry 1" from the
> rightmost character to the left if the character is 1. I have done a
> quick prototype in JavaScript (in an html page) and in quickbasic
> (running under dosemu), but my ability to write it in fortran is
> limited by my lack of knowledge in it.
>
> Specifically, I need a way to find (and replace) the nth character in ...
|
| Show full article (1.45Kb) |
|
| | no comments |
|
  |
Author: Arjen MarkusArjen Markus Date: Mar 6, 2008 03:02
On 6 mrt, 11:29, Bamm gmail.com> wrote:
> Hi,
>
> I have no fortran background at all, but I do have programming
> background in other languages. Unfortunately, our teacher instructed
> us to write our program in Fortran. Our compiler is g77 under Linux.
>
> Our problem was to print out the twos complement of an 8-bit binary
> integer. My approach is to consider it as a character * 8, where each
> character can only have either "1" or "0" (I can do the error
> checking, so no problem with that).
>
> I plan to loop through the 8 characters, and replace each 0 with 1 and
> vice versa, and then add one which means doing a "carry 1" from the
> rightmost character to the left if the character is 1. I have done a
> quick prototype in JavaScript (in an html page) and in quickbasic
> (running under dosemu), but my ability to write it in fortran is
> limited by my lack of knowledge in it.
>
> Specifically, I need a way to find (and replace) the nth character in ...
|
| Show full article (1.81Kb) |
| no comments |
|
  |
Author: BammBamm Date: Mar 6, 2008 03:09
> If you really want to do it as above
It's all I can think of at the moment, and which I have done a quick
prototype in JS and QBX. However if there is a much better way then I
would appreciate some suggestions! :)
> then the n'th character of a string c is refered to as c[n:n], as in
> c(n:n)='x'
> or
> c(n:n)=d(m:m)
> where d is another string.
Wow, yes it works, thanks! Why does n have to be written twice? What
is the significance of the first n and the second n in c(n:n)?
Anyway, with this knowledge my homework should be pretty
straightforward. Thanks again. :)
Regards,
Bamm
|
| |
| no comments |
|
  |
Author: BammBamm Date: Mar 6, 2008 03:12
> Wow, yes it works, thanks! Why does n have to be written twice? What
> is the significance of the first n and the second n in c(n:n)?
I figured out by trial and error that the first n refers to where the
substring should start while the second is where the substring ends.
Sorry if I asked before I tried. Thanks again so much!
|
| |
| no comments |
|
  |
Author: Jan Gerrit KootstraJan Gerrit Kootstra Date: Mar 6, 2008 04:36
Bamm wrote:
>>Wow, yes it works, thanks! Why does n have to be written twice? What
>>is the significance of the first n and the second n in c(n:n)?
>
>
> I figured out by trial and error that the first n refers to where the
> substring should start while the second is where the substring ends.
> Sorry if I asked before I tried. Thanks again so much!
Dear Bamm,
Now you taught me too.
Thank you,
Jan Gerrit
|
| |
| no comments |
|
  |
Author: BammBamm Date: Mar 6, 2008 05:10
> A character in Fortran is (typically) 8 bits. So a
> character * 8 variable is in total 64 bits.
>
> You will need to find a way to get the individual bits out
> of the integer value (without losing the two's complement
> aspects). I suggest you look for bit manipulation functions.
That's a good point, although it might be more difficult to do since
it's my first time to use Fortran.
> (And inform your professor that FORTRAN 77 is now some thirty
> years and has been followed by _three_ later standards, Fortran 90, 95
> and 2003, though, admittedly, the latest is not yet
> completely supported by most compilers).
Unfortunately our instructions were to use Fortran 77 because our code
must compile on our professor's machine. We even use stuff
like .eq. .gt. and .lt. instead of == > and < and we use labels
instead of do-while loops, so I really have to adjust.
|
| |
| no comments |
|
  |
Author: Arjen MarkusArjen Markus Date: Mar 6, 2008 05:20
On 6 mrt, 14:10, Bamm gmail.com> wrote:
>> A character in Fortran is (typically) 8 bits. So a
>> character * 8 variable is in total 64 bits.
>
>> You will need to find a way to get the individual bits out
>> of the integer value (without losing the two's complement
>> aspects). I suggest you look for bit manipulation functions.
>
> That's a good point, although it might be more difficult to do since
> it's my first time to use Fortran.
>
>> (And inform your professor that FORTRAN 77 is now some thirty
>> years and has been followed by _three_ later standards, Fortran 90, 95
>> and 2003, though, admittedly, the latest is not yet
>> completely supported by most compilers).
>
> Unfortunately our instructions were to use Fortran 77 because our code
> must compile on our professor's machine. We even use stuff
> like .eq. .gt. and .lt. instead of == > and < and we use labels
> instead of do-while loops, so I really have to adjust. ...
|
| Show full article (1.14Kb) |
| no comments |
|
  |
Author: BeliavskyBeliavsky Date: Mar 6, 2008 05:22
On Mar 6, 8:10 am, Bamm gmail.com> wrote:
>> A character in Fortran is (typically) 8 bits. So a
>> character * 8 variable is in total 64 bits.
>
>> You will need to find a way to get the individual bits out
>> of the integer value (without losing the two's complement
>> aspects). I suggest you look for bit manipulation functions.
>
> That's a good point, although it might be more difficult to do since
> it's my first time to use Fortran.
>
>> (And inform your professor that FORTRAN 77 is now some thirty
>> years and has been followed by _three_ later standards, Fortran 90, 95
>> and 2003, though, admittedly, the latest is not yet
>> completely supported by most compilers).
>
> Unfortunately our instructions were to use Fortran 77 because our code
> must compile on our professor's...
|
| Show full article (0.97Kb) |
| no comments |
|
  |
|
|
  |
Author: Ken PlotkinKen Plotkin Date: Mar 6, 2008 07:15
On Thu, 6 Mar 2008 05:10:21 -0800 (PST), Bamm gmail.com>
wrote:
>Unfortunately our instructions were to use Fortran 77 because our code
>must compile on our professor's machine. We even use stuff
>like .eq. .gt. and .lt. instead of == > and < and we use labels
>instead of do-while loops, so I really have to adjust.
There is absolutely nothing wrong with .eq., etc., and labels. If the
purpose of your assignment is to figure out how to do something in an
unfamiliar langauge, then f77 is a better choise than f90/95.
Something that would be fun would be to try the same problem in
Fortran IV, which had no string data type.
Another interesting variation (in f77) would be to use internal reads
and writes to manipulate your strings.
Ken Plotkin
|
| |
| no comments |
|
RELATED THREADS |
  |
|
|
|
|
|