Page 1 of 2 12 LastLast
Results 1 to 40 of 44

Thread: urgent: Mid() in c++

  1. #1

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197

    urgent: Mid() in c++

    im at a programming copetition in c++ and dont know it well. we have 45 mins left for unrestricted practice:

    How do I do Mid() in c++?
    retired member. Thanks for everything

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Someone posted an example of this not too long ago. Do a quick search.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    oh wel, its over now. They disabled our internet access when it started. I finished 2 of the programs, anfd the judges has only one of them work with their data

    I just started c++ anyways
    retired member. Thanks for everything

  4. #4
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    some type of contest?

  5. #5

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    yeah. A local university put on a programming contest in C++ or java (you got to choose) and I went as a guest. Only two other people showed up from our school, so I got promoted to a competitor even though I barely know c++. There were 10 probelsm you could do, the one who did the most in the leat amount of time won. The problems had you read a file of a given format, do something with the gathered data, then make an output file. The judges would look at the output file generated by their input file to see if you succeeded. A could of the problems were really tough, like one where you were given a grid in one file:
    5
    5
    (5x5 grid)
    and you were supposed to make a spiral of sequencial numbers:
    Code:
    1   2   3   4  5
    16 17  18  19  6
    15 24  25  20  7
    14 23  22  21  8
    13 12  11  10  9
    (look at the order of the numbers if you are confused)
    I got one of them correct, and came close to a hard one, but ran out of time. The winners (who all had 7 correct) each got a Dell laptop and a scholarship to the university.

    I got three free floppy disks out of it
    retired member. Thanks for everything

  6. #6
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    DAMN THATS A NICE PRIZE!!!!! heh but Im guessing most are college aged with a lot of experience. Thats really cool that you have stuff like that near you

    how much time were you given? (I really wish they'd have something like this near us!)

    oh and congrats on the free floppys

  7. #7

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    It was just us high schoolers from around florida. Its a pretty good college (embry-riddle) that deals with mostly aviation, about a 20 min drive.
    We had 3 hours to answer 10 problems in colsole applications.
    retired member. Thanks for everything

  8. #8
    Hyperactive Member Alan777's Avatar
    Join Date
    Jan 2001
    Location
    New Zealand
    Posts
    303
    I posted a question on this a couple of days ago too. Havn't got an answer either but I have managed to work out how to do some of these functions myself. Here is what I have got for doing Mid():

    Code:
    #include <string>
    using namespace std;
    
    string Mid(string, long, long);
    
    string Mid(string String, long Start, long Length){
    
    	string result;
    	Start--;
    	result = String.assign(String, Start, Length);
    	return result;
    }
    The reason I have got Start-- is so the first character can be number 1 like in VB.
    This works.
    This might be a $#!+ way of doing it, but it's hard to get an answer in this forum. Hope it helps.

  9. #9
    Fanatic Member
    Join Date
    Aug 2001
    Posts
    746
    thats cool, id be satisfied with the floppy disks. especially since i need them to flash my BIOS and im to lazy to go buy one.
    FlameWave Technologies - internet tools

  10. #10

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    Id be happier if i got the laptop
    The results should be on their webpage; ill try and find it
    retired member. Thanks for everything

  11. #11
    Zaei
    Guest
    I posted several string functions about a month back. Search the forums before posting, or complaining that you dont get answers.

    Z.

  12. #12

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    I actually saw those, but didnt use them. Im just wondering if there is a short way to do mid, like the way parksie gave me for split()
    retired member. Thanks for everything

  13. #13
    Fanatic Member sbasak's Avatar
    Join Date
    Aug 2001
    Location
    Globe Trotter
    Posts
    524

    here's a short mid function in C

    char* mid(char* str,int s,int f)
    {
    char *nstr;

    nstr=str; // store starting address of str in nstr
    nstr=str+f; // point to f th position of string
    *nstr='\0'; // assign \0 at that position, so indicate end of string by force!
    nstr=str; // again store starting address of str in nstr
    str=str+s-1;// modify str's starting address to begin from s th position
    return str; // return address of str, it now contains required mid string

    }
    Life is a one way journey, not a destination. Travel it with a smile and never regret anything.
    Yesterday is history, tomorrow is a mystery, today is gift - that's why we call it present.

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    memcpy should do the trick
    target=memcpy (target, source+offset,length);
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I think string::substr is the same as mid...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  16. #16

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    thanks everyone

    (again)
    (note thread date)
    retired member. Thanks for everything

  17. #17
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Originally posted by Alan777
    I posted a question on this a couple of days ago too. Havn't got an answer either but I have managed to work out how to do some of these functions myself. Here is what I have got for doing Mid():

    Code:
    #include <string>
    using namespace std;
    
    string Mid(string, long, long);
    
    string Mid(string String, long Start, long Length){
    
    	string result;
    	Start--;
    	result = String.assign(String, Start, Length);
    	return result;
    }
    The reason I have got Start-- is so the first character can be number 1 like in VB.
    This works.
    This might be a $#!+ way of doing it, but it's hard to get an answer in this forum. Hope it helps.
    Doesnt string class have its own mid?
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  18. #18
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Hah, I didnt read the entire thing b4 posting... but anyways, here's a quick mid function (C compliant):

    PHP Code:
    char Mid (char dest, const char srcint startint length) {
      
    strcpy (destsrc+start);
      
    dest[length] = 0;
      return 
    dest;

    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  19. #19
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    BTW, its been quite some time... lol
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  20. #20
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yep. Interesting thing with the number spiral btw...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  21. #21
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Actually I'd be interested to see the code for the spiral.
    It looks like a pain in the hole.

    I'm guessing it would involve subtracting the (row + column) or something from twenty or some variation or something of that...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  22. #22
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No, I just built a finite state machine that fill a 2d array, then outputted the array.

    Code is attached.
    Attached Files Attached Files
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  23. #23
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I couldn't come up with a formula that gives me the value based on xy-coordinates and table dimensions.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  24. #24
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Actually I wouldn't mind giving that algorithm a go.
    Gimme a few minutes
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  25. #25
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Christ its difficult
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  26. #26
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yeah, indeed. I'm sure it's possible, but I have no idea how.

    The finite state machine approach is slower but far simpler.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  27. #27
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I think you're doing it the only way it can be done.
    You would have to create some algorithm, that simple "goes left", then "goes up" etc.

    I can't see how this could be done with loops.
    Well, not without a huge number of if statements - in which case you'd be nearly better off just writing out the numbers
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  28. #28
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No, because it needs to be dynamic. Look at the code I attached (a little bit above). It is the whole code needed to solve that, and the main algorithm contains one for loop, one switch and 4 if. Not that much.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  29. #29
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    We've come up with a way of doing with loops sorta.
    Illl post the englishy algorithm with pictures in a sec
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  30. #30
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Basically, you do it by creating nested squares, and then fill those squares.

    So if you had a 3x3 box thang, it'd be :


    So you would go around the outer box, and then go around each nested box.
    If the total number of elements is odd, then you will have a square with one element in the middle.
    Otherwise, you will have four elements in the middle.
    So for a 4x4 box :


    You would go around the outside, counting upwards.
    And then move into the inner box, and again, count upwards from the top left.
    So you could probably use a recursive function to do the counting in each box and sub box.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  31. #31
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Problem is, my code is really fast for the method it uses (which has a theoretical difference to your algorithm but no practical), the only good way to speed it up would be a formula that you pass the x and y dimensions and the x and y coordinates and it would return the number there, without loops or such a thing.
    Like:
    int getatxy(int x, int y, int sizeX, int sizeY);

    int i = getatxy(3,3,5,5);
    So i would be 25.

    This speeds it up because only then you can go line by line left to right and output the data. Any other way and you have to store it in a temporary 2d array (as I did) or use some sort of gotoxy function (which requires stupid calculations where to go).
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  32. #32
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I dont think there's a calculation though to work out what the value at position (x, y) would be though...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  33. #33
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Assuming I got what you're discussing -- for an iterative approach
    start with this spiral algorithm:

    Code:
    #define K1  1  // constant ...  <1 slows growth >1 speeds growth of radius
    #define TRANSFORM 1  // transform to origin = 360, 360
    #define VALUE  360
    void spiral(double *x, double *y, double theta){
        double radius;
        radius = K1 * exp( ( M_E * theta) / (2 * M_PI) );
        *x=cos(theta)*radius;
        *y=sin(theta)*radius;
        if(TRANSFORM){
        	*y+=VALUE;
        	*x+=VALUE;
        }
        return;
    }
    usage:
    Code:
    max=*M_PI;
    double x,y;
    for(x=0,y=0,theta=0;theta<4*M_PI;theta+=.01,plot(x,y));
    plot() draws the point on the device.

    CB's method would be better than this one for a fibonacci spiral - IMO.

  34. #34
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Wow, and how long did you have to do this again?? Lemme see if i can come up with something without copying or looking at anyone's code

    PHP Code:

    [danmno formulathe array method is sooooo much simpler]
    -- and 
    all i could come up with... 
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  35. #35
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    jim, no you misunterstood this. The problem we're trying to solve is stated somewhere early in this thread. It goers like this.

    Read in two integers a,b. Then output a table with dimensions axb with the fields filled with increasing numbers starting at 1, going around the box in a spiral.
    E.g. if the input is 5, 6 then output
    Code:
     1  2  3  4  5
    18 19 20 21  6
    17 28 29 22  7
    16 27 30 23  8
    15 26 25 24  9
    14 13 12 11 10
    This must work for any a and b entered (except of course if it goes so far to the right that it automatically goes to the next line)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  36. #36
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    to get the value at x,y

    there's 4 quadrants in the spiral in which the numbers are incrementing in 4 different directions. I decide to call them A,B,C and D in the order they are passed trough from start. A the top quadrant, B the right C the bottom D the left. My function f(x,y) retrieves the value in the incrementing spiral depending on in which quadrant, which revolution and the position in the quadrant relative to just passed corner.

    To efficiently determine quadrant we first determine the excentricity of the rectangle e=W>H, the smaller side quadrants should be triangle shaped while the larger side quadrant should be trapezoids, and it would be easier to determine if it were in the triangles first and then determine which side of the half of the rectangle if not in the triangles. The special case when theres a hole in the middle E, when W and H are not even is left when all else fails.
    The quadrant Q(x,y)={
    if not e:
    A if |floor((W-1)/2)|-y<=floor((W-1)/2)
    C if |floor(W/2)|+y-H+1<=floor(W/2)
    B if x>ceil(W/2)
    D if x<floor(W/2)
    E if none of the above
    if e:
    B if |floor((H-1)/2)|+x-W+1<=floor((H-1)/2)
    D if |floor(H/2)|-x<=floor(H/2)
    B if y>ceil(W/2)
    D if y<floor(W/2)
    E if none of the above
    }
    A and C side length are W-1 decrementing with 2 each revolution
    similarly B and D are H-1-2R
    each full revolution is thus 2(H+W)-8R, and the cumulative sum is
    2R(H+W)-8*sumof(R)=2(H+W)-4R(R-1)
    The revolution R for the quadrants are
    y for A
    W-x-1 for B
    H-y-1 for C
    x for D
    and the position P is
    x-y for A
    y-W+x for B
    W-x+y for C
    H-y-x for D
    The value should be 2R(H+W)-8*sumof(R)=2(H+W)-4R(R-1) + S + P
    where S=
    0 for A
    W-1-2R for B
    W+H-2-2R for C
    2W+H-3-2R for D
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  37. #37
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    But surely you're assuming the dimensions of the grid ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  38. #38
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    forgot to mention, W and H are the grid dimensions, f(x,y) should be W*H for E obviously
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  39. #39
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    My head is spinning.

    What would that mean in code?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  40. #40
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    lots of ifs and stuff, I think writing it in SQ would look considerably cleaner and consise
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width