Assignment: Functions
double s_to_d(string) – Converts a decimal number saved as a string to a decimal number saved as a float. Follows the given steps.
1. Create a double accumulator.
2. Find the index i of the decimal point (‘.’).
1. If no decimal point exists, set i to the length of the string.
3. Iterate over each character in the string with the iterator j.
1. If i – j is positive,
1. Pass the current character into c_to_i(char)
2. raise 10 to the power of the i – j – 1 with the myPow(int, int) function.
3. multiply these two results.
4. Add this result to the accumulator.
2. else if i – j is negative,
1. Pass the current character into c_to_i
2. raise 10 to the power of i – j.
3. multiply these two results.
4. Add this result to the accumulator.
4. Return the accumulator.
Comments
Leave a comment