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

  • 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 */...

  • 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...

  • I need the programming to be in language C. I am using a program called Zybook...

    I need the programming to be in language C. I am using a program called Zybook Prompt: Write a statement that outputs variable numObjects. End with a newline. Given: #include <stdio.h> int main(void) { int numObjects; scanf("%d", &numObjects); return 0; } What I did so far. I am not sure if its right tho? #include <stdio.h> int main(void) { int numObjects; scanf("%d", &numObjects); printf(" num Objects is "); printf("%d\n", userAge); return 0; }

  • 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...

  • Convert C++ language to PEP/8 assembly language

    Convert this C++ language to PEP/8 assembly language. #include <stdio.h> int main(void) {        int num, rem;     printf("Enter a number: ");     scanf("%d", &num);     printf("Roman numerals: ");             while(num != 0)     {         if (num >= 1000)       // 1000 - m         {            printf("m");            num -= 1000;         }         else if (num >= 900)   // 900 -  cm         {            printf("cm");            num -= 900;         }                 else if (num >= 500)   // 500 - d         {                       printf("d");            num -= 500;         }         else if (num >= 400)   // 400 -  cd         {            printf("cd");            num -= 400;         }         else if (num >= 100)   // 100 - c         {            printf("c");            num -= 100;                                }         else if (num >= 90)    // 90 - xc         {            printf("xc");            num -= 90;                                                       }         else if (num >= 50)    // 50 - l         {            printf("l");            num -= 50;                                                                              }         else if (num >= 40)    // 40 - xl         {            printf("xl");                       num -= 40;         }         else if (num >= 10)    // 10 - x         {            printf("x");            num -= 10;                    }         else if (num >= 9)     // 9 - ix         {            printf("ix");            num -= 9;                                  }         else if (num >= 5)     // 5 - v         {            printf("v");            num -= 5;                                              }         else if (num >= 4)     // 4 - iv         {            printf("iv");            num -= 4;                                                                     }         else if (num >= 1)     // 1 - i         {            printf("i");            num -= 1;                                                                                            }     }     return 0;}

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • IN C PLEASE Find the maximum value and minimum value in miles Tracker. Assign the maximum...

    IN C PLEASE Find the maximum value and minimum value in miles Tracker. Assign the maximum value to maxMiles, and the minimum value to minMiles. Sample output for the given program: Min miles: -10 Max miles: 40 (Notes) 1 tes pass ㄷ Alltes int j; 1 #include <stdio.h> 2 3 int main(void) { 4 const int NUM_ROWS = 2; 5 const int NUM_COLS = 2; 6 int milesTracker [NUM_ROWS] [NUM_COLS]; 7 int i; 8 9 int maxMiles = 0; //...

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
ADVERTISEMENT