Results 1 to 4 of 4

Thread: C+ For Engineers, function help

  1. #1
    Join Date
    Mar 2006
    Location
    United States, -7:00 GMT
    Posts
    1,790
    Mentioned
    2 Post(s)
    Quoted
    2 Post(s)

    Default C+ For Engineers, function help

    (20 pts) Define the function sumOdd(int inArrayPR[], int arraySizePR)that returns the sum of the values in odd indexes inArrayPR[] or -999999 if there are no odd indexes. It is guaranteed that arraySizePR is positive and at least 1.

    I don't get what an index is I guess. bubbles[0] = 1, bubbles[1] = 2, where the 0,1 is the index, isn't that correct? So the only way the -999999 is going to show up is if the array size is 1? Or do I misunderstand what an index is.

    ALL help is needed as the next 5 questions deal with indexes...
    Last edited by Ejjman; 03-23-2012 at 03:53 AM.

    hakuna matata ;)

  2. #2
    Join Date
    Jun 2007
    Location
    La Mirada, CA
    Posts
    2,484
    Mentioned
    1 Post(s)
    Quoted
    3 Post(s)

    Default

    Yes, you have the correct concept of an index, it is the number you have "indexed" into the array/how far you are "into" the array. If the only element in the array is element at index 0, then it should return -999999, otherwise you need to add together the odd indexed elements.

    [1] + [3] + [5] +....etc

    Also is this for C or C++? I assume C++

    "Failure is the opportunity to begin again more intelligently" (Henry Ford)


  3. #3
    Join Date
    Mar 2006
    Location
    United States, -7:00 GMT
    Posts
    1,790
    Mentioned
    2 Post(s)
    Quoted
    2 Post(s)

    Default

    Ah ok that is what I thought. Thanks man!

    It is for C#, unfortunately. But its a required for EE at my school so I can't really avoid it :\

    hakuna matata ;)

  4. #4
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

    Assuming I'm late,

    csharp Code:
    int sumOdd(int[] arr, int l) {
                int sum = 0;
                if (l < 2)
                    return -999999;
                for (int i = 0; i < arr.Length; i++) { // do we count 0? If not, i = 1.
                    if (i % 2 != 0)
                        sum += arr[i];
                }
                return sum;
            }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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