Please help urgently!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deltamami0999
    New Member
    • Feb 2008
    • 16

    Please help urgently!

    I need to get this done before midnight tonight and I really need some help...Can someone show me how to code these problems? I have to show the rest of the class how to code it

    1. Write a function that accepts a pointer to an array of integers and a single integer. Search the array for the integer and return a pointer to that element in the array. Do not use array notation.

    2. Write a function that accepts an integer, prompts the user for that number of values, loads the values into an array (without using array notation) and returns a pointer to the array to the calling function.

    3. Write a recursive function that excepts two integers, x and n, calculate x to the nth power and returns the result to the calling function.

    Thanks,
    Teacher in Training
  • Studlyami
    Recognized Expert Contributor
    • Sep 2007
    • 464

    #2
    Can someone help me explain these problems? I'm trying to teach my little brother how to code these ...
    Now its "you have to show the rest of the class", but it has to be done by midnight? hmm.
    We are not here to do your home work for you so don't ask.

    Comment

    • deltamami0999
      New Member
      • Feb 2008
      • 16

      #3
      He's in the class that i'm doing my student teaching...the teacher just gave me these problems to see if i could work them out but I haven't taken C++ since high school

      Originally posted by Studlyami
      Now its "you have to show the rest of the class", but it has to be done by midnight? hmm.
      We are not here to do your home work for you so don't ask.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Regardless of your intent, it is against our site policy to just give out code on demand. And I hate to sound impertinent, but if you are student teaching a programming class, shouldn't you know how to code?

        Comment

        • deltamami0999
          New Member
          • Feb 2008
          • 16

          #5
          i do, but i'm stuck.....can you help me figure this out?

          Originally posted by Ganon11
          Regardless of your intent, it is against our site policy to just give out code on demand. And I hate to sound impertinent, but if you are student teaching a programming class, shouldn't you know how to code?

          Comment

          • oler1s
            Recognized Expert Contributor
            • Aug 2007
            • 671

            #6
            If you are asking us to pity you, you get the exact opposite. I feel sorry for the students, who are going to learn from someone who has no clue what he is doing. Moreover, your approach was not "I need to learn" but "I need to somehow get my work done for me". Your attitude won't get you any respect on technical forums.

            It's too much to hope that you'll use modern C++ idioms to answer the above three questions. Maybe you'll figure out the answer to them the C way. But I'll make some points anyway:

            Q1: This question is deceiving. That function should not simply take in a pointer to an array and the integer to search. That is, either you should be using some sort of container for an array, like a vector, or you should be passing in the size of the array.

            Also, I consider it bad design to pass around pointers unnecessarily. Passing pointers here is unnecessary. Well, if you pass in an array directly, it will decay to a pointer. Ideally though, you want the function to promise that the array won't be modified. Also, returning a pointer is a bad idea. You get no penalty for returning an integral index, but it makes ownership semantics much clearer. If you return a pointer, the big question is going to be, who manages that pointer?

            Q2: A vector might be more appropriate in place of an array. Or if going the array route anyway, returning a raw pointer is problematic. Because now you have used new in one place, and must bet that delete will be called appropriately later on. Using a smart pointer like shared_array or whatever would be more appropriate here.

            Comment

            • deltamami0999
              New Member
              • Feb 2008
              • 16

              #7
              i'm just trying to freshen up on my skills... here's what i got so far on the first problem...its wrong but i tried...
              int main()



              {

              int myArray [5]={22,23,34,45,1 2,}

              int *myArray = new int [size];

              for ( int i = 0; i<size; i++)

              {

              cout<<(myArray+ i);

              }

              return myArray;

              this is the first one...they havent covered vectors and they didn't cover it in my former C++ class so i'm not sure what to do now


              Originally posted by oler1s
              If you are asking us to pity you, you get the exact opposite. I feel sorry for the students, who are going to learn from someone who has no clue what he is doing. Moreover, your approach was not "I need to learn" but "I need to somehow get my work done for me". Your attitude won't get you any respect on technical forums.

              It's too much to hope that you'll use modern C++ idioms to answer the above three questions. Maybe you'll figure out the answer to them the C way. But I'll make some points anyway:

              Q1: This question is deceiving. That function should not simply take in a pointer to an array and the integer to search. That is, either you should be using some sort of container for an array, like a vector, or you should be passing in the size of the array.

              Also, I consider it bad design to pass around pointers unnecessarily. Passing pointers here is unnecessary. Well, if you pass in an array directly, it will decay to a pointer. Ideally though, you want the function to promise that the array won't be modified. Also, returning a pointer is a bad idea. You get no penalty for returning an integral index, but it makes ownership semantics much clearer. If you return a pointer, the big question is going to be, who manages that pointer?

              Q2: A vector might be more appropriate in place of an array. Or if going the array route anyway, returning a raw pointer is problematic. Because now you have used new in one place, and must bet that delete will be called appropriately later on. Using a smart pointer like shared_array or whatever would be more appropriate here.

              Comment

              • gpraghuram
                Recognized Expert Top Contributor
                • Mar 2007
                • 1275

                #8
                Originally posted by deltamami0999
                i'm just trying to freshen up on my skills... here's what i got so far on the first problem...its wrong but i tried...
                int main()



                {

                int myArray [5]={22,23,34,45,1 2,}

                int *myArray = new int [size];

                for ( int i = 0; i<size; i++)

                {

                cout<<(myArray+ i);

                }

                return myArray;

                this is the first one...they havent covered vectors and they didn't cover it in my former C++ class so i'm not sure what to do now
                Why do two declarations have the same name.
                int *myArray and
                myArray[5]

                Try to work on the idea first and then start writing the code
                Raghuram

                Comment

                • kandigirl2009
                  New Member
                  • Feb 2008
                  • 4

                  #9
                  its hard when you're going by Daniel Liang's style....its so computer science based and they're learning how to just hack at code--

                  Originally posted by gpraghuram
                  Why do two declarations have the same name.
                  int *myArray and
                  myArray[5]

                  Try to work on the idea first and then start writing the code
                  Raghuram

                  Comment

                  Working...