Getting a for loop to work with negative numbers

19,750

Solution 1

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 <stdio.h>

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 

Solution 2

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.

Solution 3

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<stdio.h>

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

Solution 4

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.

Share:
19,750
pckofwolfs
Author by

pckofwolfs

Updated on June 11, 2022

Comments

  • pckofwolfs
    pckofwolfs almost 2 years

    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<stdio.h>
    
    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;
    }
    
  • pckofwolfs
    pckofwolfs about 8 years
    How would I communicate that into code. This makes sense, and I know it has to do with my if statement, but I don't know how I could figure it out to do all numbers until the last one?
  • s_b
    s_b about 8 years
    If you know the total size of your array, it should be easy to stop the loop at the last but one.
  • pckofwolfs
    pckofwolfs about 8 years
    The book just generates the numbers and runs it through the code. I know no idea what the numbers will be until it passes the first set of numbers the book uses.
  • pckofwolfs
    pckofwolfs about 8 years
    Thank you for your help. I'm really unsure why I had some of those random things in there. Thank you again. Your explanations did a great job explaining where I went wrong.
  • Vlad from Moscow
    Vlad from Moscow about 8 years
    @pckofwolfs No at all. We beginners should help each other.:)
  • double-beep
    double-beep about 5 years
    Welcome to Stack Overflow! While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.