Thread: Transforming array variables

  1. #16
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    Quote Originally Posted by matsp View Post
    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
    Mats this is where I've gotten to. What step should I take next?
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define MAXCOLS 80
    
    int main()
    {
       
        int i;
        char string[MAXCOLS];
       char expr[];
        double array[2][3]={{4.5,13,12},{32,12.6,2.5}};
                     
    
        printf("Enter an expression:\n");
        while ((expr[i++]=getchar()) != '\n');
         expr[--i] ='\0';
    
        /* this will come in later
        printf("Expression\n");
        printf("&#37;s", string);*/
        
        getchar();
        return 0;
     }
    Last edited by ben2000; 08-01-2007 at 05:21 PM.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ben2000 View Post
    Mats this is where I've gotten to. What step should I take next?
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define MAXCOLS 80
    
    int main()
    {
       
        int i;
        char string[MAXCOLS];
       char expr[];
        double array[2][3]={{4.5,13,12},{32,12.6,2.5}};
                     
    
        printf("Enter an expression:\n");
        while ((expr[i++]=getchar()) != '\n');
         expr[--i] ='\0';
    
        /* this will come in later
        printf("Expression\n");
        printf("%s", string);*/
        
        getchar();
        return 0;
     }
    And why would you want to use getchar() instead of "fgets()"? In the above code, it achieves almost exactly the same thing - the only main difference is the lack of checking for expr overflowinng.

    I do believe you should use "string" to input things into, rather than "expr" - as the latter is not defined to a length, and you are printing "string", rather than "expr".

    --
    Mats

  3. #18
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    Quote Originally Posted by matsp View Post
    And why would you want to use getchar() instead of "fgets()"? In the above code, it achieves almost exactly the same thing - the only main difference is the lack of checking for expr overflowinng.

    I do believe you should use "string" to input things into, rather than "expr" - as the latter is not defined to a length, and you are printing "string", rather than "expr".

    --
    Mats
    Well I used string there cos when and after extracting the values and concatenating them I'll probably need to store it in the string array.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, I've completed my variant:
    Code:
    Enter expression using red, green and blue as variables:green+red+blue
    result[0]: 68.500000
    result[1]: 36.549999
    Slight rounding error on the second case, but that's normal for "float" - need "double" to make it more precise (or print with fewer digits).

    --
    Mats

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ben2000 View Post
    Well I used string there cos when and after extracting the values and concatenating them I'll probably need to store it in the string array.

    Yes, probably a good idea to use two strings, one for the original and one for the "modifed" - if nothing else, it makes it easier to cope with the resulting variable being longer or shorter than the variable without having to do a bunch of complex string-shuffling.

    --
    Mats

  6. #21
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    Quote Originally Posted by matsp View Post
    So, I've completed my variant:
    Code:
    Enter expression using red, green and blue as variables:green+red+blue
    result[0]: 68.500000
    result[1]: 36.549999
    Slight rounding error on the second case, but that's normal for "float" - need "double" to make it more precise (or print with fewer digits).

    --
    Mats
    You mean you've written a code that did what I'm trying to do just now, then you must be too good. No doubt. I wish I'm in your position you're vast in this.

  7. #22
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    Mats please work me through.

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, I've been programming for about 24 years now, and the last 20 or so I've been paid to do so by some small and large companies.

    Writing little bits of code to scan strings and do math expressions is quite easy compared to working on a Linux kernel, Xen Hypervisor or a Windows graphics driver.

    Of course SOME OF the code in these projects aren't particularly challenging, but other pieces certainly are - and all of the code I wrote today is MUCH smaller than almost any of the many dozens of files that are part of those big projects - the source code for all of the above counts in megabytes. The file I just created for this project is about 3.5KB, and less than 200 lines of code.

    Oh, and I cheated - I don't transform the expression to postfix first, I just parse the string and calculate the infix operations, using recursion and push/pop functions to evaluate in correct order:
    Code:
    Enter expression using red, green and blue as variables:5+5*5
    result[0]: 30.000000
    
    Enter expression using red, green and blue as variables:5*5+5
    result[0]: 30.000000
    
    Enter expression using red, green and blue as variables:5+gray
    Error - invalid variable gray
    The last one shows that it also supports error handling. I haven't implemented parenthesis, so you can't do things like "5*(3+4)". I'm sure that could be done quite easily, but it didn't seem like it was necessary for what you are trying to do.

    --
    Mats

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ben2000 View Post
    Mats please work me through.
    Yes, sure - but I'm not giving you my code - because that's not the point here, is it?

    So, what have you done so far?

    You got the string read in, yes? [And I don't really care if you use fgets() or a sequence of getchar() - it is REALLY not important]

    So, next step is to replace the "red", "green" and "blue" with the respective values from the "array", right? How do you think you should go about that?

    --
    Mats

  10. #25
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    That's shocking, can you imagine 24yrs and I'm just one month into it. Well I hope to be like you someday, I envy you a lot. Like you know I've gotten into my infix-to-postfix-evaluate working, just trying to get this bit, so I can parse the string of values to it. If you can help me out here, I'll crown you.

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, I'm trying to get you to think about ONE part of the problem at a time...

    Say your string is "red+green+blue", what do you need to do to make that into the numerical form (sorry, can't remember the numbers).

    Work on that - in fact, you could even ignore the input, just do
    Code:
    strcpy(string, "red+green+blue");
    Obviously, try to write it so that it supports all math operations and perhaps also numbers in the expression - but start with the above bit. [using strcpy means that you don't have to type the expression every time you "try out your current code" - which is good until you have it working to the level where you need to try "different variations"].

    I must say, I probably couldn't write an "infix to postfix" interpreter when I'd been programming a month - but then I never tried that - I was programming basic and assembler with no teacher available...

    --
    Mats

  12. #27
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    well I'm thinking of how to go about it but have not come up with something yet.

  13. #28
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    Mats please can you give your code even at a price, I'm working on a project and I'm really short of time. Please, I've spent so much time on this whole code. I'm statistically inclined, due to the fact that this whole thing came impromptu, I was reading the language up and at the same time trying to put it together. The major code, code I needed to write, I've managed to put it together, but this bit I thought would have been easy but it appears quite difficult. I can't quite think clearly, probably because I'm racing against time. Please if you can assist me I'll so much appreciate it, this is the only bit holding me down. Even at a price I'm ready to pay. Please, and please, just assist me.

  14. #29
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's not really my style.

    Is this a "work project" or a "school project"?

    Really, the part of parsing a variable so that it becomes the value of the string is quite easy.

    Look at the hint I gave earlier, then think about what you need to do. It's not very different from parsing a string for digits - the operators are of course similar in either case.

    Sorry I didn't reply to your post earlier, but it didn't appear in my "new posts" - I only just went back to see if you posted anything last night after I gone to bed.

    --
    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