Thread: Transforming array variables

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    49

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

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    Quote Originally Posted by matsp View Post
    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. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    Quote Originally Posted by anon View Post
    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. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    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. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Does your current code actually output the correct result?
    It looks to me as if you are adding it twice...

    --
    Mats

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    Quote Originally Posted by matsp View Post
    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. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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. #12
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    Quote Originally Posted by matsp View Post
    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. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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. #14
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    I can use fgets but that will read in the entire expression at once, provideing a pointer to the string array

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM