'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<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;
}
Solution 1:[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:[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:[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:[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.
Solution 5:[5]
Perhaps, this is what are you expecting:
#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;
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
Solution 6:[6]
//This is the Java version of your problem, if anyone is interested.
public class StudentScores {
public static void main (String [] args) {
final int SCORES_SIZE = 4;
int[] bonusScores = new int[SCORES_SIZE];
int i = 0;
bonusScores[0] = 10;
bonusScores[1] = 20;
bonusScores[2] = 30;
bonusScores[3] = 40;
for (i = 0; i < SCORES_SIZE - 1; ++i) {
bonusScores[i] = (bonusScores[i] + bonusScores[i+1]);
}
for (i = 0; i < SCORES_SIZE; ++i) {
System.out.print(bonusScores[i] + " ");
}
System.out.println();
return;
}
}
Solution 7:[7]
Simple loop:
/* Your solution goes here */
for (i = 0; i < bonusScores.length; ++i)
{
if (i == bonusScores.length -1)
{
break;
}
bonusScores[i] = (bonusScores[i] + bonusScores[i+1]);
}
for (i = 0; i < bonusScores.length; ++i)
{
System.out.print(bonusScores[i] + " ");
}
System.out.println();
Solution 8:[8]
for(i = 0; i < bonusScores.length - 1; i++)
{
bonusScores[i] += bonusScores[i + 1];
}
Solution 9:[9]
This is what I got and all answers are correct:
import java.util.Scanner;
public class StudentScores {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
final int SCORES_SIZE = 4;
int[] bonusScores = new int[SCORES_SIZE];
int i;
for (i = 0; i < bonusScores.length; ++i) {
bonusScores[i] = scnr.nextInt();
}
for(i = 0; i < SCORES_SIZE - 1; ++i){
bonusScores[i] = bonusScores[i] +
bonusScores[i + 1];
}
for (i = 0; i < bonusScores.length; ++i) {
System.out.print(bonusScores[i] + " ");
}
System.out.println();
}
}
Solution 10:[10]
I'm pretty sure you can add an || statement but this helps it add the following element minus the last element since there is none past it.
The issue I see is you account for one element being smaller than the next element but what if its larger? then > before
My solution:
for (i = 0; i < bonusScores.length-1; ++i)
if ( bonusScores[i] <= bonusScores[i +1] )
{
bonusScores[i] = (bonusScores [i] + bonusScores[i+1]);
}
else if ( bonusScores[i] >= bonusScores[i +1] )
{
bonusScores[i] = (bonusScores [i] + bonusScores[i+1]);
}
else {
bonusScores[i] = bonusScores[i];
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
