PDA

View Full Version : C+ For Engineers, function help



Ejjman
03-23-2012, 03:45 AM
(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...

HyperSecret
03-23-2012, 04:19 AM
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++

Ejjman
03-23-2012, 06:15 PM
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 :\

n3ss3s
03-25-2012, 03:32 AM
Assuming I'm late,


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