This is the problem.

Reversing the elements of an array involves swapping the corresponding elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the middle of the array.

Given an array a , an int variable n containing the number of elements in a , and two other int variables, k and temp , write a loop that reverses the elements of the array.

Do not use any other variables besides a , n , k , and temp .

This is my code, and I can't figure out why its wrong.

for( k = 0; k < n; k++){
avgTemp= 0;
total += temps[n];

avgTemp = total/n;
}

1. n is the number of elements in the array, where k is 0, 1, 2, ..., n-1.

2. There is no need to calculate the average inside the loop. Move that stuff outside.

3. Don't forget to set total to zero first, otherwise you'll just have some random number.

4. If total is not a float or double, your average will be an integer, which is probably not what you want. For floating point division, make sure at least one of the operands is a floating point number.

total = 0;
avgTemp= 0;
for( k = 0; k < n; k++){
  total += temps[k];
  }
avgTemp = float( total ) /n;

Hope this helps.

This is my code, and I can't figure out why its wrong.

for( k = 0; k < n; k++){
avgTemp= 0;
total += temps[n];

avgTemp = total/n;
}

1. n is the number of elements in the array, where k is 0, 1, 2, ..., n-1.

2. There is no need to calculate the average inside the loop. Move that stuff outside.

3. Don't forget to set total to zero first, otherwise you'll just have some random number.

4. If total is not a float or double, your average will be an integer, which is probably not what you want. For floating point division, make sure at least one of the operands is a floating point number.

total = 0;
avgTemp= 0;
for( k = 0; k < n; k++){
  total += temps[k];
  }
avgTemp = float( total ) /n;

Hope this helps.

How does any of the above even remotely accomplish "Reversing the elements of an array involves swapping the corresponding elements of the array..." as stated in the problem?

Do you have any idea how to swap two array values?

This article has been dead for over six months. Start a new discussion instead.