Amazon.com Widgets Chapter 10 exercise problem 12 math help
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 15, 2014, 09:12:54 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in C, 3rd ed. (Stephen Kochan)
| |-+ Questions and Help
| | |-+ Chapter 10 exercise problem 12 math help
Pages: [1] Go Down
Print
Author Topic: Chapter 10 exercise problem 12 math help (Read 1375 times)
A31Chris
Newbie
*
Posts: 20


Email




on: January 19, 2012, 12:50:46 AM

ok I need a clue on the math involved in exercise 12.

Quote
Write a function called strToFlot that converts a character string into a floating point value. Have the function accept an optional leading minus sign. So, the call

strToFloat ("-867.6921");

should return the value -867.6921.

I dont want the answer to the math outright. I've been racking my head how to handle the digits to the right of the decimal point. Just want a clue on how to go about it. Smiley
Logged
A31Chris
Newbie
*
Posts: 20


Email




Reply #1 on: January 19, 2012, 01:14:30 AM

Wait I think I got it. I need to just do the whole number like its a whole number and then find out how many places to the right then if-else it by .1 or .01 or .001 etc. I think I'll stop at 5 digits to the right.

Anyone have a clue on a more elegant way of doing it?

Clue please, not an entire answer.
Logged
A31Chris
Newbie
*
Posts: 20


Email




Reply #2 on: January 19, 2012, 02:26:24 PM

Yeah I know, switch-case would be better still for a limit up to five or so....
Logged
A31Chris
Newbie
*
Posts: 20


Email




Reply #3 on: February 13, 2012, 10:35:34 AM

OK I need a suggestion how to deal with a floating point rounding error for this exercise. Here is the output I am getting from a test character decimal string of 123.123:

Code: (Objective-C)
The number of decimal places are: 3
Decimal character string extracted to buffer: 123123
Decimal result=123123
Converted completely back to decimal: 123.123001

Not sure how to deal with the rounding error. Here is the last subroutine with the switch-case structure. If anyone has any suggestions on how to go about this that would be cool.

Code: (Objective-C)
void decPlaceMult(double number)
{
float answer=0;

switch(count)
{
case 1:
answer=number*.1;
break;

case 2:
answer=number*.01;
break;

case 3:
answer=number*.001;
break;

case 4:
answer=number*.0001;
break;

case 5:
answer=number*.00001;
break;

default:
break;
}

printf("Converted completely back to decimal: %f",answer);
getchar();

return;
}
Logged
mitchb
Full Member
***
Posts: 127






Reply #4 on: February 14, 2012, 12:27:07 PM

Does your assignment allow the use of standard library functions? if so use atof().

Code: (Objective-C)
#include <stdio.h>
#include <stdlib.h>

float strToFloat( char *string );

int main (int argc, const char * argv[])
{
   
   
    char numStr[] = "-1234.567";
    float floatNum;
   
    floatNum = strToFloat( numStr );
    printf("%.3f\n", floatNum);
    return 0;
}

float strToFloat( char *string )
{
    return atof(string);
}
Mitch
Logged

If you give a man a program, you will frustrate him for a day;
If you teach him how to program, you will frustrate him for a lifetime;
     - Anonymous
A31Chris
Newbie
*
Posts: 20


Email




Reply #5 on: February 18, 2012, 01:25:01 AM

Does your assignment allow the use of standard library functions? if so use atof().

Code: (Objective-C)
#include <stdio.h>
#include <stdlib.h>

float strToFloat( char *string );

int main (int argc, const char * argv[])
{
   
   
    char numStr[] = "-1234.567";
    float floatNum;
   
    floatNum = strToFloat( numStr );
    printf("%.3f\n", floatNum);
    return 0;
}

float strToFloat( char *string )
{
    return atof(string);
}
Mitch

No it doesn't allow using atof. The point is I believe to learn to create these things ourselves. Smiley

Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.