Solved

write a main function

Posted on 2006-10-21
5
199 Views
Last Modified: 2012-05-05
hi,

I want to write a main() function to use the atof function. this what I did so far. please help me re write the main() function.



  #include <stdio.h>
  #include <stdlib.h>
  #include <ctype.h>

  double atof(char s[]);

 main()
   {
      double a;
      char s[3];
     
      s[0]="a";
      s[1]="b";
      s[2]="c";
     
      a= atof(s);
      printf("%d", a);      
   }



   /* atof:  convert string s to double */
   double atof(char s[])
   {
       double val, power;
       int i, sign;

       for (i = 0; isspace(s[i]); i++)  /* skip white space */
           ;
       sign = (s[i] == '-') ? -1 : 1;
       if (s[i] == '+' || s[i] == '-')
           i++;
       for (val = 0.0; isdigit(s[i]); i++)
           val = 10.0 * val + (s[i] - '0');
       if (s[i] == '.')
           i++;
       for (power = 1.0; isdigit(s[i]); i++) {
           val = 10.0 * val + (s[i] - '0');
           power *= 10;
       }
       return sign * val / power;
   }
0
Comment
Question by:rmtogether
  • 3
  • 2
5 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17782371
Hi rmtogether,

1. Print doubles using %lf flag
 printf("%lf", a);  

2. atof is defined in C library
just #include stdlib.h

3. strtod would be much more robust that atof ... depending on your system, you may also have strtof and strtold avaiable to you

Cheers!
sunnycoder
0
 

Author Comment

by:rmtogether
ID: 17782394

this is for my exam study. I need fully understuand the atof funciton. So I wnat to write main function to test atof. could you please help me this?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17782422
1.
      s[0]="a";
      s[1]="b";
      s[2]="c";

should have been

      s[0]='a';
      s[1]='b';
      s[2]='c';

"a" is same as string a\0 while on LHS you have a single character!!! Using single quotes gives you a character, using double quotes makes it a NULL terminated string.

2.
>      char s[3];
You are using three indexes and a string needs to be \0 terminated ... So infact you need 4. Also, always intitalize the array or explicitly add a string terminator so that values outside your array are not seen by your function

      char s[4] = { 0 };

3. As I said,
printf("%lf", a);

4. Currently you would get 0 as output because you are passing "abc" for conversion
0
 

Author Comment

by:rmtogether
ID: 17782454
hi, sunnycoder

i am very glad you are here!!

could you please tell me what isdigit() use for and
what is the meaning of val = 10.0 * val + (s[i] - '0');
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 17782464
isdigit()  checks if the character argument you supplied it is a digit (character in the range of 0 to 9).

>val = 10.0 * val + (s[i] - '0');

s[i] - '0' gets the numerical value of your digit ... e.g. if your digit was 7 ... As part of string, it would have been character '7' .. Note that 7 is not the same as '7' ... '7' is a character with ASCII value 55. Internally stored as integer 55!!!

To get value 7 from character 7, smiply subtract character '0' ... This would give you the "distance" of current character from '0'. Since current characer is a digit, we will get the integer value for that digit as a result of that subtraction

Rest is self explanatory ... simply multiply previous value of val by 10 and add current digit ... Simple place value number system (units, tens, hundreds ... each increasing place get multipled by 10)
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Describe order of operation while copying text 11 115
Trouble linking program with -lcrypt 3 142
Finding a good hash function 4 120
nested if statement in excel help 4 15
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now