Please someone help me out with converting array variable names

Started by
15 comments, last by ToohrVyk 16 years, 8 months ago
Quote:Original post by Ben2k
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.

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.
Advertisement
Quote:Original post by Ben2k
Quote:Original post by ToohrVyk
Look 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;}
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.
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.
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.
Quote:Original post by smitty1276
Why 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
Quote:Original post by Ben2k
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.


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);}

This topic is closed to new replies.

Advertisement