Question

In: Computer Science

In C. Write a loop that subtracts 1 from each element in lowerScores. If the element...

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

Solutions

Expert Solution

#include <stdio.h>

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

    lowerScores[0] = 5;
    lowerScores[1] = 0;
    lowerScores[2] = 2;
    lowerScores[3] = -3;

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

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

#include <stdio.h>

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

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

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

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

Related Solutions

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}. please bold the solution 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) {...
Write a loop that sets each array element to the sum of itself and the next...
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...
Write a for loop from 1 to 3 using i as the variable. For each value...
Write a for loop from 1 to 3 using i as the variable. For each value of i: Create a vector x of 10 random numbers between 0 and 1. Create a second vector t which is equal to ten integers from 1 to 10. Plot x versus t in figure 1. Use hold on to keep each plot. Use a different color for the line for each value of i. At the very end, add the text 'time' using...
C LANGUAGE ONLY Write a C program to count the frequency of each element in an...
C LANGUAGE ONLY Write a C program to count the frequency of each element in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements of the array: element [0]: 25 element [1]: 12 element [2]: 43 Expected output: The frequency of all elements of an array: 25 occurs 1 times 12 occurs 1 times 3 occurs 1 times
write an operational semantic for a while loop in c++
write an operational semantic for a while loop in c++
Write a Formula for the ionic compound that forms from each pair of element Aluminum and...
Write a Formula for the ionic compound that forms from each pair of element Aluminum and oxygen Beryllium and iodine Calcium and sulfur Calcium and iodine
(C++) Write a nested loop that reads an integer n (n>0) from the keyboard and print...
(C++) Write a nested loop that reads an integer n (n>0) from the keyboard and print out "n" lines as follows: 0 0 1 0 1 2 0 1 2 3 … 0 1 2 3 … n-1
Write a C++ code using while loop which is getting the two integers from the user,...
Write a C++ code using while loop which is getting the two integers from the user, and output how many numbers are multiples of 5, and how many numbers are multiples of 7 between the two integers (inclusive).
Write a program to reverse each integer number on array (size 3) using while loop. C++...
Write a program to reverse each integer number on array (size 3) using while loop. C++ Input: 3678 2390 1783 Output: 8763 0932 3871
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT