0

I have a program with a subroutine that processes numbers that are input into the subroutine.

#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>


float test_subroutine(float x,float y)
{
    float a=105.252342562324;
    float b=108.252234256262;
    float d;
    float e;
    char var;

    d=x*a;
    e=y*b;

    sprintf(var,"%0.2f %0.2f",d,e);

    return var;
 }



 int main()
 {
      float variable[2];
      variable=test_subroutine(2.5,3.5);

 }

Now with this program the idea is I have either a string or variable like this:

368.386 270.63

Whether this is a string or array I want to be able to split this into two individual floating point numbers 368.386 and 270.63. How do I tweak this program to accomplish this?

jms1980
  • 1,017
  • 1
  • 21
  • 37

2 Answers2

0

Take a look at strtok. Then malloc an array of what you want and fill it up.

Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
0

The title you mentioned and the code you have shown doesn't seem to make sense as they dont match. If returning an array from a function is your concern, then please refer this

How to return a local array from a C/C++ function?

The code you have written returns a char but the return type of the function is float and hence this itself will raise an error when you compile.

As of the question that you asked at the last, of converting "368.386 270.63" to two individual floating point numbers, in C we dont have a native data type called String, by convention the language uses array of chars(refer here for more details on this), Now for splitting a character array,you can use the strtok() function. To convert the character array to floating point numbers, use the atof() function.This returns a double, since you want float to be returned, explicitly convert it into float.

Nithin cp
  • 109
  • 2
  • 3
  • 11