Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Wednesday, March 30, 2016

Getting a for loop to work with negative numbers

Getting a for loop to work with negative numbers


Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial scores: 10, 20, 30, 40 Scores after the loop: 30, 50, 70, 40 The first element is 30 or 10 + 20, the second element is 50 or 20 + 30, and the third element is 70 or 30 + 40. The last element remains the same. This is a homework question that I need help with. What I am having problems with is if the bonusScores are negative the example it uses against my code is -100, -200, -300 , -400, -500.

include    int main(void)  {      const int SCORES_SIZE = 4;      int bonusScores[SCORES_SIZE];      int i = 0;        bonusScores[0] = 10;      bonusScores[1] = 20;      bonusScores[2] = 30;      bonusScores[3] = 40;        /* Your solution goes here  */      for (i = 0; i < SCORES_SIZE; i++)      {          if (( bonusScores[i] <= bonusScores[i +1] ) || (bonusScores[i] < bonusScores [i+1]))          {              bonusScores[i] = (bonusScores [i] + bonusScores[i+1]);          }          else          {              bonusScores[i] = bonusScores[i];          }      }      for (i = 0; i < SCORES_SIZE; ++i)      {          printf("%d ", bonusScores[i]);      }      printf("\n");        return 0;  }  

Answer by Mohammad Ali Baydoun for Getting a for loop to work with negative numbers


You made negative numbers an issue by adding that if condition inside the loop. It's redundant. You're just asked to take the array and transform it so that element i represents a[i] + a[i+1] in your original array. This doesn't require any sort of special handling for signs.

Also, note that in your code, you're referring to element i + 1 when this could potentially be an element beyond the bounds of the array (suppose i = 3). When you modify the code to loop correctly, be sure to avoid indexing outside the bounds of the array.

You can completely avoid a check by using an appropriate condition in the for loop. Not only does i have to be less than the size of the array, but i+1 must satisfy this condition too.

Answer by s_b for Getting a for loop to work with negative numbers


Well the sum of two negatives bonus will be a greater negative bonus, so simply go ahead and add the numbers without bothering to check if they are negative or positive.

Your loop should only run till the numbers you want to update. Since you do not need to update the last number, do not go there.
Other than that I think you are quite on track.

Answer by Aleksey Khivrenko for Getting a for loop to work with negative numbers


You should only iterate through your array SCORES_SIZE-1 times. Inside the loop simply add current + next and store it into your array, as such:

include    int main(void)  {      const int SCORES_SIZE = 4;      int bonusScores[SCORES_SIZE];      int i = 0;        bonusScores[0] = 10;      bonusScores[1] = 20;      bonusScores[2] = 30;      bonusScores[3] = 40;        /* Your solution goes here  */      for (i = 0; i < SCORES_SIZE-1; i++)      {            bonusScores[i] = (bonusScores [i] + bonusScores[i+1]);      }      for (i = 0; i < SCORES_SIZE; ++i)      {          printf("%d ", bonusScores[i]);      }      printf("\n");        return 0;  }  

Answer by Vlad from Moscow for Getting a for loop to work with negative numbers


In the assignment there is written

Be careful not to index beyond the last element

However this loop

   for (i = 0; i < SCORES_SIZE; i++)     {          if (( bonusScores[i] <= bonusScores[i +1] ) || (bonusScores[i] < bonusScores [i+1]))          {              bonusScores[i] = (bonusScores [i] + bonusScores[i+1]);          }          else          {              bonusScores[i] = bonusScores[i];          }      }  

tries to use an index beyond the last element when i is equal to SCORES_SIZE - 1

And there is nothing said in the assignment about this condition

if (( bonusScores[i] <= bonusScores[i +1] ) || (bonusScores[i] < bonusScores [i+1]))  

which is the same as

if (( bonusScores[i] <= bonusScores[i +1] ))  

It is not clear why you wrote this condition.

The loop can look the following way

for ( i = 1; i < SCORES_SIZE; ++i )  {      bonusScores[i-1] += bonusScores[i];  }  

Here is a demonstrative program

#include     int main( void )   {      const int SCORES_SIZE = 4;      int bonusScores[SCORES_SIZE];        bonusScores[0] = 10;      bonusScores[1] = 20;      bonusScores[2] = 30;      bonusScores[3] = 40;        for ( int i = 1; i < SCORES_SIZE; ++i )      {          bonusScores[i-1] += bonusScores[i];      }        for ( int i = 0; i < SCORES_SIZE; ++i )      {          printf( "%d ", bonusScores[i] );      }      printf( "\n" );        bonusScores[0] = -100;      bonusScores[1] = -200;      bonusScores[2] = -300;      bonusScores[3] = -400;        for ( int i = 1; i < SCORES_SIZE; ++i )      {          bonusScores[i-1] += bonusScores[i];      }        for ( int i = 0; i < SCORES_SIZE; ++i )      {          printf( "%d ", bonusScores[i] );      }      printf( "\n" );  }      

Its output is

30 50 70 40   -300 -500 -700 -400   

Answer by jpmottin for Getting a for loop to work with negative numbers


Perhaps, this is what are you expecting:

#include     int main(void) {      const int SCORES_SIZE = 4;      int bonusScores[SCORES_SIZE];      int i = 0;        bonusScores[0] = -10;      bonusScores[1] = -20;      bonusScores[2] = -30;      bonusScores[3] = -40;        printf("Negative scores:\n");      for (i = 0; i < SCORES_SIZE; i++)      {          if(i!=SCORES_SIZE-1)              bonusScores[i] = (bonusScores [i] + bonusScores[i+1]);      }      for (i = 0; i < SCORES_SIZE; ++i) {        printf("%d ", bonusScores[i]);      }      printf("\n");        bonusScores[0] = 10;      bonusScores[1] = 20;      bonusScores[2] = 30;      bonusScores[3] = 40;        printf("Positive scores:\n");      for (i = 0; i < SCORES_SIZE; i++)      {          if(i!=SCORES_SIZE-1)              bonusScores[i] = (bonusScores [i] + bonusScores[i+1]);      }      for (i = 0; i < SCORES_SIZE; ++i) {        printf("%d ", bonusScores[i]);      }        return 0;  }  

Voici la sortie console :

Negative scores:  -30 -50 -70 -40   Positive scores:  30 50 70 40   


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.