# Please someone help me out with converting array variable names

## Recommended Posts

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 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:34.5+13*21 I haven't got any clue on how to go about this. Can someone help me out?

##### Share on other sites
Your question was kind of confusing, but is this what you are after?

#define RED   0#define GREEN 1#define BLUE  2double array[2][3] = { { 34.5, 13, 21, }, { 11, 16, 9.55 } };double output = array[RED][0] + array[BLUE][0] * array[GREEN][0];

##### Share on other sites
Quote:
 Original post by Mike.PopoloskiYour question was kind of confusing, but is this what you are after?*** Source Snippet Removed ***

You know general infix expression can be of the form 32*12+5. But instead of direct value input, I want to use column variable names of an array, then extract the corresponding elements to form a general infix expression. I have written the infix to postfix converter but it accepts the general form. What basically I'm trying to achieve is; if I input y=red*blue+green it will evaluate the expression for each row with coresponding elements of red, blue green in each row. Assuming column 1 in the array is red, column 2 is blue and column three is green. The output will be a string of sort 23*23.5+22 Assuming these are the corresponding values or a particular row which will then be passed to the infix-postfix function. Please assist me.

##### Share on other sites
so you want an expression (infix) to be of: y = mx + b to go to

##### Share on other sites
Quote:
 Original post by Alpha_ProgDesso you want an expression (infix) to be of: y = mx + b to go to

I don't intend to pass y to the infix-to-postfix converter only the right hand of the equalitity. Say a user inputs red+blue+green will give a string output of 23.4+22+33 assuming these are the corresponding values for row one in the array which I will now parse to my infix to postfix converter.I have done that bit of infix to postfix conversion but accepts it accepts expression of the form 12.5+63+12 or any other. Please someone should assist me!

[Edited by - Ben2k on August 1, 2007 3:50:19 PM]

##### Share on other sites
Look into std::string namely its find and replace members, to replace all occurences of red, green and blue by their corresponding value.

##### Share on other sites
Quote:
 Original post by ToohrVykLook into std::string namely its find and replace members, to replace all occurences of red, green and blue by their corresponding value.

please I'm a beginner, I don't really understand or come across std::string. Could you please put me through?

##### Share on other sites
Quote:
Original post by Ben2k
Quote:
 Original post by ToohrVykLook into std::string namely its find and replace members, to replace all occurences of red, green and blue by their corresponding value.

please I'm a beginner, I don't really understand or come across std::string. Could you please put me through?

No offsense intended, but I smell some homework question. So: is it a homework question?

##### Share on other sites
Quote:
Original post by Emmanuel Deloget
Quote:
Original post by Ben2k
Quote:
 Original post by ToohrVykLook into std::string namely its find and replace members, to replace all occurences of red, green and blue by their corresponding value.

please I'm a beginner, I don't really understand or come across std::string. Could you please put me through?

No offsense intended, but I smell some homework question. So: is it a homework question?

I don't really blame you Emmanuel, do you know when I left school? I studied statistics and graduated 7years ago. I pray I don't come across people like you. Had it been I studied computer science, I wouldn't have been here to seek help and for you to smell rubbish. If you can't help me, why not hold your peace for men of good will to put me through.

##### Share on other sites
Quote:
 Original post by Ben2kI don't really blame you Emmanuel, do you know when I left school? I studied statistics and graduated 7years ago. I pray I don't come across people like you.

What, people who want to make sure it's not a homework question before they give out the answer?

What is wrong with that? Perhaps you should just relax a little and not take it as an insult. On this forum there is an official policy not to do people's homework.
So when in doubt, people ask if it is homework.
If it isn't, there's no problem. But if it is, people should figure it out for themselves, which means all we can provide here are hints, not complete answers/explanations.

So, since it isn't a homework question, I'll try to help. (That is, I'll try to ask some clarifying questions because I can't really make sense of the question)

Quote:
 I want to use column variable names of an array

This bit puzzles me a bit.
Elements in an array don't have names. That's pretty much the point. They have an integer index in the array, but no name.
So how do you mean red, blue and green are the names of the columns?

If you have something like this:
int red = 0;int green = 1;int blue = 2;

Then if you have a two-dimensional array, you can refer to one column like this:
array[red], which would get you an array containing all the red values. Is this what you mean?
How is the array declared, and how are the columns "named"?

You then want to be able to enter an expression like this:
red+blue*green, where red, blue and green are all arrays. (This is all C/C++ code, right, and not a just a string containing the expression?)

And the output should then give you one string of the form:
"r0+g0*b0" where what, r0,g0,b0 are the first items in each column? Or should the string contain something like this for every item in the column (something like "r0+g0*b0 r1+g1*b1 r2+g2*b2")?

It'd be really helpful if you could explain exactly what it is you want the code to do. (Perhaps even what you need it for)
Try writing up some code for how you'd want to use it, or something.

##### Share on other sites
Quote:
Original post by Ben2k
Quote:
 Original post by ToohrVykLook into std::string namely its find and replace members, to replace all occurences of red, green and blue by their corresponding value.

please I'm a beginner, I don't really understand or come across std::string. Could you please put me through?

std::string is the beginner's way of manipulating strings of text in C++ (as opposed to the legacy char* approach, which is only ever useful when it's manipulated by experts with a lot of time on their hands).

Either way, the member functions are detailed here. The code below is untested, but hopefully conveys the idea.

#include <sstream>#include <ostream>#include <string>// Replace all occurences of "needle" in "haystack" by "value",// return the modified haystack.std::string replace(  const std::string & needle,   std::string haystack,  const std::string & value) {  std::string::size_type index = 0;  while ((index = haystack.find(needle,index)) != std::string::npos)  {    haystack.replace(index,needle.size(),value);    index += value.size();  }  return haystack;}// Turn a floating-point value to a stringstd::string to_string(double i){  std::stringstream converter;  converter.setf(std::ios::fixed);  return (converter << i).str();}// Convert "red + blue * green" to "34.5 + 13 * 2"std::string convert(std::string input, double rgb[3]){  input = replace("red",input,to_string(rgb[0]));  input = replace("green",input,to_string(rgb[1]));  input = replace("blue",input,to_string(rgb[2]));  return input;}

##### Share on other sites
Thanks the thing I meant is this asuuming this is my data where red, blue and green are headers this is a 2*3 array.
red blue green
23.5 34 12
12 23 13.4

I want a situation where by if I enter the expression "red-ble+green" I will get the output string as "23.5-34+12" for row 1, "12-23+13.4" for row two. That's no matter the combination of operators I'll still get the corresponding values with the operators in the right places between the values, not actually evaluating it to get the result.

##### Share on other sites
ToohrVyk, thanks so much, I really appreciate your effort and time but I'm working with C not C++. I wish it was in C.

##### Share on other sites
Why not use an RGB datatype?
struct RGB{  float red;  float blue;  float green;};RGB *arr = array; //assume the array is defined as you didfor (int i = 0; i < n; i++){  RGB *rgb = arr + i;  float result = rgb.red + rgb.blue * rgb.green;}
EDIT: I just realized what you are talking about... ToohrVyk's answer will work.

##### Share on other sites
Quote:
 Original post by smitty1276Why not use an RGB datatype?*** Source Snippet Removed ***EDIT: I just realized what you are talking about... ToohrVyk's answer whoul work.

Thanks to you all for your time, thing I meant is this asuuming this is my data where red, blue and green are headers this is a 2*3 array.
red blue green
23.5 34 12
12 23 13.4

I want a situation where by if I enter the expression "red-blue+green" I will get the output string as "23.5-34+12" for row 1, "12-23+13.4" for row two. That's no matter the combination of operators I'll still get the corresponding values with the operators in the right places between the values, not actually evaluating it to get the result. I'm not trying to evaluate the result straight away but to have values replace the corresponding headers in the expression

##### Share on other sites
Quote:
 Original post by Ben2kToohrVyk, thanks so much, I really appreciate your effort and time but I'm working with C not C++. I wish it was in C.

Now would be a good time to let the "engineer" lobe of your brain say a few things to the "hacker" lobe, about how C is possibly the worst imaginable tool to do string manipulation with (C++ isn't that brighter, though, but it still has a sane concept of string that C doesn't), and about how choosing one's tools well is more important than actually using them.

An overflow-free, portable and reasonably performant C approach would be the following. Make sure to check it, it may contain subtle unintended bugs.
#include <string.h>char* to_string(double d, size_t *size){  char* buffer = 0;  *size = snprintf(buffer,0,"%f",d) + 1;  buffer = malloc(size);  snprintf(buffer,*size,"%f",d);  return buffer;}int evaluate_size_change(char* needle, char* haystack, size_t size_value){  size_t size_needle = strlen(needle);  int size_delta = (int)size_value - (int)size_needle;  int size_change = 0;  while (strstr(haystack,needle))  {     size_change += size_delta;    haystack += size_needle;    ++haystack;  }  return size_change;}void replace(  char *needle,  char **haystack,   size_t *size_haystack,   char *value,  size_t value_size){  char *source = *haystack;  size_t size_dest = *size_haystack +     evaluate_size_change(needle,*haystack,value_size);  char *dest = malloc(size_dest + 1);  char *srcptr = source, *findptr, *dstptr = dest;  char *lastptr = dest + size_dest + 1;   *dest = '\0';   while (findptr = strstr(srcptr,needle))  {    *findptr = '\0';    strncat(dstptr,srcptr,lastptr-dstptr);    dstptr += findptr - srcptr;    strncat(dstptr,value,lastptr-dstptr);    dstptr += value_size;    srcptr = findptr + value_size;  }  strncat(dstptr,srcptr,lastpr-dstptr);  *haystack = dest;  *size_haystack = size_dest;  free (source);}char *replace(char *input, double rgb[3]){  size_t size_red, size_blue, size_green;  char *red = to_string(rgb[0],&size_red);  char *green = to_string(rgb[1],&size_green);  char *blue = to_string(rgb[2],&size_blue);  int size_source = strlen(input);  char *text = malloc(size_source + 1);  strcpy(text,input);  replace("red",&text ,&size_source,red,size_red);  replace("green",&text ,&size_source,green,size_green);  replace("blue",&text ,&size_source,blue,size_blue);  free(red);  free(green);  free(blue);  return text;}

Compare with the PHP equivalent (PHP is an excellent string manipulation language, and is more readable than PERL):
function transform(& $rgb,$input) {  return str_replace(     array ("red","green","blue"),    array ( (string)$rgb[0], (string)$rgb[1], (string)$rgb[2] ),$input);}

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627719
• Total Posts
2978790

• 9
• 21
• 14
• 12
• 42