Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Recursive problem

Recursive problem

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelptutorial
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Haim Nachum
    wrote on last edited by
    #1

    Hi. Suppose i have a function that takes an array and its length. That function should search the entire array for each element and return a result. example: int rec(int arr[], int length) { if(length == 0) return 0; //search array for element length - 1; searchingarr(arr,length -1); rec(arr[],length - 1); } Now . i cant use loops(while or for), no static variables. only one function inside the rec and use only recursive. i cant put a sign in the array that points to the end either. the problem here is that i want that each time im calling "searchingarr" i paste him the length of the array. here, the length is substracted by 1 each time i call rec.

    Richard Andrew x64R 1 Reply Last reply
    0
    • H Haim Nachum

      Hi. Suppose i have a function that takes an array and its length. That function should search the entire array for each element and return a result. example: int rec(int arr[], int length) { if(length == 0) return 0; //search array for element length - 1; searchingarr(arr,length -1); rec(arr[],length - 1); } Now . i cant use loops(while or for), no static variables. only one function inside the rec and use only recursive. i cant put a sign in the array that points to the end either. the problem here is that i want that each time im calling "searchingarr" i paste him the length of the array. here, the length is substracted by 1 each time i call rec.

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      Could you restate your question exactly? What is the function looking for? What is it supposed to return?

      H 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        Could you restate your question exactly? What is the function looking for? What is it supposed to return?

        H Offline
        H Offline
        Haim Nachum
        wrote on last edited by
        #3

        the function(searcharr) should take an element and check if there is a sequence of summed elements that is equal to the index of that element. example arr{ 1 , 3, 2 , 0 , 0 , 3} arr[2] = 3. because 2(i=2) + 0(i=3) + 0(i=4) gives us the value 3 that is the length of the sequence that when summed gives us the value of the index(2).

        Richard Andrew x64R 1 Reply Last reply
        0
        • H Haim Nachum

          the function(searcharr) should take an element and check if there is a sequence of summed elements that is equal to the index of that element. example arr{ 1 , 3, 2 , 0 , 0 , 3} arr[2] = 3. because 2(i=2) + 0(i=3) + 0(i=4) gives us the value 3 that is the length of the sequence that when summed gives us the value of the index(2).

          Richard Andrew x64R Offline
          Richard Andrew x64R Offline
          Richard Andrew x64
          wrote on last edited by
          #4

          But arr[2] = 2, not 3.

          H 1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            But arr[2] = 2, not 3.

            H Offline
            H Offline
            Haim Nachum
            wrote on last edited by
            #5

            arr{ 1 , 3, 2 , 0 , 0 , 3} the index 2 in the array have a value that is equal to the sequence of 3 elements(2 , 0 , 0) thus the returning value is 3.

            Richard Andrew x64R 1 Reply Last reply
            0
            • H Haim Nachum

              arr{ 1 , 3, 2 , 0 , 0 , 3} the index 2 in the array have a value that is equal to the sequence of 3 elements(2 , 0 , 0) thus the returning value is 3.

              Richard Andrew x64R Offline
              Richard Andrew x64R Offline
              Richard Andrew x64
              wrote on last edited by
              #6

              So what is your algorithm? Can you write the algorithm in words?

              O N 2 Replies Last reply
              0
              • Richard Andrew x64R Richard Andrew x64

                So what is your algorithm? Can you write the algorithm in words?

                O Offline
                O Offline
                Omegaclass
                wrote on last edited by
                #7

                [Message Deleted]

                1 Reply Last reply
                0
                • Richard Andrew x64R Richard Andrew x64

                  So what is your algorithm? Can you write the algorithm in words?

                  N Offline
                  N Offline
                  Nelek
                  wrote on last edited by
                  #8

                  It looks like homework.

                  Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson Rating helpfull answers is nice, but saying thanks can be even nicer.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups