Reputation: 159
I have a string describing the function and I need to check if this is function This idea came to me to put the string array then check
the string: "1 11.1 2 3.2 3 44.1 4 5.4 5 7.4 6 1111.0"
the string describe the function (1,11.1),(2,3.2) (3,44.1) etc...
and I try to insert it to array like arr[0][0]=1 and arr[1][0]=11.1,arr[0][1]=2,arr[1][1]=3.2... And i need help how to do it or get advice to another idea how to check If the string is function?I hope I explained myself better..thanks
in to example the string:
"1 11.1 1 3.2 3 44.1 4 5.4 5 7.4 6 1111.0"
isn't describe function because we have (1,11.1) and (1,3.2) the question is how to insert the string into array
EDIT for clarity:
He is asking the following question: Given a string of (apparently) evenly many floats, let's represent it in the form:
"x1 y1 x2 y2 x3 y3 ... xn yn"
Then the string "defines a function" if for every xi
and xj
with i
different from j
, if the values at xi
and xj
are the same, then the values at yi
and yj
are the same.
He wants to know how to check whether a string "defines a function."
Upvotes: 1
Views: 99
Reputation: 40145
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
{//split string by strtok, destroy input by strtok
char input[] = "1 11.1 2 3.2 3 44.1 4 5.4 5 7.4 6 1111.0";
char *array[10][2];
char *item;
char **to = &array[0][0];
int i, count=0;
for(item = strtok(input, " "); item != NULL; item = strtok(NULL, " ")){
*to++ = item;
++count;
}
for(i = 0;i<count / 2;++i){
printf("%s, %s\n", array[i][0], array[i][1]);
}
}
printf("\n");
{//convert double by strtod
char *input = "1 11.1 1 3.2 3 44.1 4 5.4 5 7.4 6 1111.0";
double array[10][2];
double item;
double *to = &array[0][0];
char *p;
int i, count=0, pairSize;
for(p = input; *p != '\0';){
item = strtod(p, &p);
if(*p == ' ' || *p == '\0'){
*to++ = item;
++count;
} else {
printf("invalid number\n");
break;
}
}
pairSize = count / 2;
for(i = 0;i<pairSize;++i){
printf("%g, %g\n", array[i][0], array[i][1]);
}
//Check the monotonic increase
for(i = 0;i+1<pairSize;++i){
if(array[i][0] >= array[i+1][0]){
printf("invalid sequence at array[%d][0] = %g and array[%d][0] = %g\n",
i, array[i][0], i+1, array[i+1][0]);
}
}
}
return 0;
}
Upvotes: 2