Question

C - Language Write a loop that sets each array element to the sum of itself...

C - Language

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.

#include <stdio.h>

int main(void) {
   const int SCORES_SIZE = 4;
   int bonusScores[SCORES_SIZE];
   int i;

   for (i = 0; i < SCORES_SIZE; ++i) {
      scanf("%d", &(bonusScores[i]));
   }

   /* Your solution goes here */

   for (i = 0; i < SCORES_SIZE; ++i) {
      printf("%d ", bonusScores[i]);
   }
   printf("\n");

   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

int main(void) {
   const int SCORES_SIZE = 4;
   int bonusScores[SCORES_SIZE];
   int i;

   for (i = 0; i < SCORES_SIZE; ++i) {
      scanf("%d", &(bonusScores[i]));
   }

   // Looping from 0 to SCORES_SIZE-1 (except for the last element )
   for (i = 0; i < SCORES_SIZE-1; ++i) {
      // computing sum of itself and the next element for index i
      // And replacing the value at index i with the computed result
      bonusScores[i]=bonusScores[i]+bonusScores[i+1];
   }

   for (i = 0; i < SCORES_SIZE; ++i) {
      printf("%d ", bonusScores[i]);
   }
   printf("\n");

   return 0;
}
Add a comment
Know the answer?
Add Answer to:
C - Language Write a loop that sets each array element to the sum of itself...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • for c CHALLENGE ACTIVITY 9.7.1: Decrement array elements. Write a loop that subtracts 1 from each...

    for c CHALLENGE ACTIVITY 9.7.1: Decrement array elements. Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex lowerScores = {5,0,2,-3) becomes {4.0, 1.0). 1 include <stdio.h> 3 int main(void) 0 const int SCORES_SIZE - 4; int lowerScores [SCORES_SIZE]: int i; for (1 - 0; i < SCORES_SIZE; ++) { scanf("%d", &(lowerScores[i])); /" Your solution goes here / { 15 for (i = 0; i...

  • C++ Write a loop that subtracts 1 from each element in lowerScores. If the element was...

    C++ Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}. #include <iostream> using namespace std; int main() { const int SCORES_SIZE = 4; int lowerScores[SCORES_SIZE]; int i; for (i = 0; i < SCORES_SIZE; ++i) { cin >> lowerScores[i]; } /* Your solution goes here */ for (i = 0; i < SCORES_SIZE;...

  • Write a loop that sets newScores to oldScores shifted once left, with element 0 copied to...

    Write a loop that sets newScores to oldScores shifted once left, with element 0 copied to the end. Ex: If oldScores = {10, 20, 30, 40), then newScores = {20, 30, 40, 10). Note: These activities may test code with different test values. This activity will perform two tests, both with a 4-element array (int oldScores[4]). See "How to Use zyBooks". Also note: If the submitted code tries to access an invalid array element, such as newScores[9] for a 4-element...

  • In the C language Double any element's value that is less than controlVal. Ex: If controlVal...

    In the C language Double any element's value that is less than controlVal. Ex: If controlVal = 10, then dataPoints = {2, 12, 9, 20} becomes {4, 12, 18, 20}. #include <stdio.h> int main(void) { const int NUM_POINTS = 4; int dataPoints[NUM_POINTS]; int controlVal; int i; scanf("%d", &controlVal); for (i = 0; i < NUM_POINTS; ++i) { scanf("%d ", &(dataPoints[i])); } /* Your solution goes here */ for (i = 0; i < NUM_POINTS; ++i) { printf("%d ", dataPoints[i]); }...

  • Write a loop that subtracts 1 from each element in lowerScores. If the element was already...

    Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}. public class StudentScores { public static void main (String [] args) { final int SCORES_SIZE = 4; int[] lowerScores = new int[SCORES_SIZE]; int i = 0; lowerScores[0] = 5; lowerScores[1] = 0; lowerScores[2] = 2; lowerScores[3] = -3; /* Your solution goes here */...

  • Write a loop that subtracts 1 from each element in lowerScores. If the element was already...

    Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}. 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[] lowerScores = new int[SCORES_SIZE]; int i; for (i = 0; i < lowerScores.length; ++i) { lowerScores[i] = scnr.nextInt(); }...

  • For any element in keysList with a value greater than 50. print the corresponding value in...

    For any element in keysList with a value greater than 50. print the corresponding value in itemsList, followed by a comma (no spaces) Ex: If the input is 32 105 101 35 10 20 30 40 the output is 20,30, 1 include <stdio.h> 2 3 int main(void) { 4 const int SIZE LIST - 4 5 int keysList[SIZE LISTI: 6 int itemslist[SIZE_LIST); 7 int 1; 8 9 scanf("%d", &keysList[0]); 10 scanf("%d", &keyslist[1]); 11 scanf("%d", &keysList[2]); 12 scanf("%d", &keysList[]); 13 14...

  • C programming only please 5.2.3: Printing array elements with a for loop. Write a for loop...

    C programming only please 5.2.3: Printing array elements with a for loop. Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, then backwards. End each loop with a newline. Ex: If courseGrades = {7, 9, 11, 10}, print: 7 9 11 10 10 11 9 7 Hint: Use two for loops. Second loop starts with i = NUM_VALS - 1. (Notes) Note: These activities may test code with...

  • Write a C program to do the following...in this EXACT order: a. Declare an integer array...

    Write a C program to do the following...in this EXACT order: a. Declare an integer array named alpha of 50 elements. b. Use a for loopt to initialize each element of alpha to its index value (e.g. alpha[0] = 0, alpha[1]=1, etc.). c. Output the value of the first element of the array alpha. d. Output the value of the last element of alpha. e. Set the value of the 25th element of the array alpha to 62 and output...

  • 1. Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the...

    1. Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the total extra credit received. Full credit is 100, so anything over 100 is extra credit. Ex: If testGrades = {101, 83, 107, 90}, then sumExtra = 8, because 1 + 0 + 7 + 0 is 8. What i am given: import java.util.Scanner; public class SumOfExcess { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int NUM_VALS =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT