Question

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; ++i) {
cout << lowerScores[i] << " ";
}
cout << endl;

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#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];
}

for (i = 0; i < SCORES_SIZE; ++i) {
   if(lowerScores[i]>0)
      lowerScores[i]-=1;
   else
       lowerScores[i]=0;
}

for (i = 0; i < SCORES_SIZE; ++i) {
cout << lowerScores[i] << " ";
}
cout << endl;

return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ Write a loop that subtracts 1 from each element in lowerScores. If the element was...
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...

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

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • C++ Write a function so that the main() code below can be replaced by the simpler...

    C++ Write a function so that the main() code below can be replaced by the simpler code that calls function MphAndMinutesToMiles(). Original main(): int main() { double milesPerHour; double minutesTraveled; double hoursTraveled; double milesTraveled; cin >> milesPerHour; cin >> minutesTraveled; hoursTraveled = minutesTraveled / 60.0; milesTraveled = hoursTraveled * milesPerHour; cout << "Miles: " << milesTraveled << endl; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using...

  • C++ Write a function SwapArrayEnds() that swaps the first and last elements of the function's array...

    C++ Write a function SwapArrayEnds() that swaps the first and last elements of the function's array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10}. The array's size may differ from 4. #include <iostream> using namespace std; /* Your solution goes here */ int main() {    const int SORT_ARR_SIZE = 4;    int sortArray[SORT_ARR_SIZE];    int i = 0;    sortArray[0] = 10;    sortArray[1] = 20;    sortArray[2] = 30;    sortArray[3] = 40;...

  • Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS elements....

    Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS elements. Ex: If userValues is {2, 2, 1, 2} and matchValue is 2 , then numMatches should be 3. Your code will be tested with the following values: * matchValue: 2, userValues: {2, 2, 1, 2} (as in the example program above) * matchValue: 0, userValues: {0, 0, 0, 0} * matchValue: 50, userValues: {10, 20, 30, 40} (Notes) #include <iostream> #include <vector> using namespace...

  • in C++ Problem: Subtract 4 to any element's value that is greater than maxVal. Ex: If...

    in C++ Problem: Subtract 4 to any element's value that is greater than maxVal. Ex: If maxVal = 10, then dataPoints = {2, 12, 9, 20} becomes {2, 8, 9, 16}. ------------------------ #include #include using namespace std; int main() { int maxVal; int NUM_POINTS; unsigned int i; cin >> maxVal; cin >> NUM_POINTS; vector dataPoints(NUM_POINTS); for (i = 0; i < dataPoints.size(); ++i) { cin >> dataPoints.at(i); } /* Your solution goes here */ for (i = 0; i <...

  • CHALLENGE ACTIVITY 8.4.2: Find char in C string Assign a pointer to any instance of searchChar...

    CHALLENGE ACTIVITY 8.4.2: Find char in C string Assign a pointer to any instance of searchChar in personName to searchResult. #include <iostream> #include <cstring> using namespace std; int main() { char personName[100]; char searchChar; char* searchResult = nullptr; cin.getline(personName, 100); cin >> searchChar; /* Your solution goes here */ if (searchResult != nullptr) { cout << "Character found." << endl; } else { cout << "Character not found." << endl; } return 0; }

  • C++ Program Write a do-while loop that continues to prompt a user to enter a number...

    C++ Program Write a do-while loop that continues to prompt a user to enter a number less than 100, until the entered number is actually less than 100. End each prompt with a newline. Ex: For the user input 123, 395, 25, the expected output is: Enter a number (<100): Enter a number (<100): Enter a number (<100): Your number < 100 is: 25 #include <iostream> using namespace std; int main() { int userInput; /* Your solution goes here */...

  • C++ Given numRows and numColumns, print a list of all seats in a theater. Rows are...

    C++ Given numRows and numColumns, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print a space after each seat, including after the last. Ex: numRows = 2 and numColumns = 3 prints: 1A 1B 1C 2A 2B 2C #include <iostream> using namespace std; int main() { int numRows; int numColumns; int currentRow; int currentColumn; char currentColumnLetter; cin >> numRows; cin >> numColumns; /* Your solution goes here */ cout...

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