1. Transforming array variables

I want to use variable instead of numbers as infix. Say I have red*blue+green as infix, where these are red, blue, green are variables names representing each array columns for n*3 array. Get the corresponding numerical row values for each row and use it to substitute the variables in the input to get new infix. eg
Code:
`array[2][3]={34.5,13,21,11,16,9.55}`
where red,blue and green are the column names.

Infix input : red+blue*green
infix output:
Code:
` 34.5+13*21`

2. First question is if these variable names are "constant", or if the variable names are "variable". That is, are all the variable names known to the application, or can variables be defined by the user? (Can the user say "blah=11" somewhere, and then say "blah + 4" expecting to get 15?).

I would also prefer if you use braces inside your array initializer - whilst what you've written is perfectly valid, it's making it hard to read what's what (particularly if you need MORE variables later on, and the list gets quite long, it's hard to keep track of which number goes where.

Code:
`array[2][3]={ {34.5,13,21}, {11,16,9.55} };`
If you have three variables, why is there two sets of them? What is the second set of values for?

--
Mats

3. What have you tried?

I assume you might get different operators between red, blue and green as input? You'd probably parse the input for these operators and output as requested?

4. Originally Posted by matsp
First question is if these variable names are "constant", or if the variable names are "variable". That is, are all the variable names known to the application, or can variables be defined by the user? (Can the user say "blah=11" somewhere, and then say "blah + 4" expecting to get 15?).

I would also prefer if you use braces inside your array initializer - whilst what you've written is perfectly valid, it's making it hard to read what's what (particularly if you need MORE variables later on, and the list gets quite long, it's hard to keep track of which number goes where.

Code:
`array[2][3]={ {34.5,13,21}, {11,16,9.55} };`
If you have three variables, why is there two sets of them? What is the second set of values for?

--
Mats
the column names are constant the user doesn't need to be modified by the user. Assuming red is the name of column one in the array, red should always refer to the elements in column one. The column names red, blue, green are not part of the elements of the array, just names used to designate each field. So in the above array for example red should refer to 34.5 and 11 respectively for each row assuming red is the name given to column one in the array and so on. So basically, if the user inputs red+blue*green, I want the output string to be of the form
34.5+13*21 for row 1 and 11+16+9.55 for row 2.

5. Originally Posted by anon
What have you tried?

I assume you might get different operators between red, blue and green as input? You'd probably parse the input for these operators and output as requested?
Sure different operators *,+,-, / can be between the names which I call psuedo operands.

6. So what have you tried?

This is a string parsing / concatenating assignment isn't it?

May the user also input: "green+red*blue"?

Code:
```While GetsnextSubstring
GetColumnNumberFromSubstring //for example
Concatenate
End While```

7. Do all expressions have just red, green and blue in them, or are there numbers as well? E.g. are you allowed to say "red *4 + blue *3 / green" (and is that supposed to evaluate in mathematical order, or just "as it's stated" (that is, are you calculating blue *3 / green and red * 4 before adding them)?

--
Mats

8. This is what I've done but not what I wanted cos this is static I need to make it dynamic so users can input expression with any combination operators but this has to be done with infix to postfix that bit I've gotten right.
Code:
```#include<stdio.h>
#include <stdlib.h>
#include<math.h>
#define red 0
#define blue 1
#define green 2
main(){
int nrows=2;
int ncol=3;
/*Calculating the output yi for y=red+blue*green*/
float y[nrows];
b=0;
do{
for (a=0; a<nrows; ++a){
y[a]=0;
c=0;
y[a]+= array[a][red] + array[a][blue]*array[a];
++b;}}while(b<nrows);

for(a=0;a<nrows; ++a)

printf("&#37;.2f\n", y[a]);
getchar();
printf("\n\n");
}```

9. Does your current code actually output the correct result?
It looks to me as if you are adding it twice...

--
Mats

10. Originally Posted by matsp
Does your current code actually output the correct result?
It looks to me as if you are adding it twice...

--
Mats
I missed out the ( ) around (array[a][red] + array[a][blue]*array[a])

But I don't know where to go from here to achieve what I want to do

11. Check the sum of your variables. Is it really the same value as you get when you calculate by hand or using a calculator?

Now, this isn't getting you towards a solution really - as your colours aren't really available as variables to a user entering an expression, as you pointed out earlier. So perhaps we should discuss possible solutions here...

What do you think you need to do in order to translate the string "red+green+blue" into the numerical equivalent using your array of values? What is the first thing you need to do with this string? And how do you do that?

--
Mats

12. Originally Posted by matsp
Check the sum of your variables. Is it really the same value as you get when you calculate by hand or using a calculator?

Now, this isn't getting you towards a solution really - as your colours aren't really available as variables to a user entering an expression, as you pointed out earlier. So perhaps we should discuss possible solutions here...

What do you think you need to do in order to translate the string "red+green+blue" into the numerical equivalent using your array of values? What is the first thing you need to do with this string? And how do you do that?

--
Mats
to read in the string but I don't know what funtion to use in reading it in

13. What function do you use to read strings in when you need to do it in other applications you've written?

I was actually referring to the next step, once the string has been read in - so perhaps while you are digging round your old files, you could ponder that one...

--
Mats

14. I can use fgets but that will read in the entire expression at once, provideing a pointer to the string array

15. And what's the problem with reading all the string at once? Yes, you can do it one char at a time too - do you prefer to do that? If so use getchar() or fgetc().

--
Mats