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 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...
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 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...
THE FOLLOWING IS CODED IN C Write a function that sets each element in an array...
THE FOLLOWING IS CODED IN C Write a function that sets each element in an array to the sum of the corresponding elements in two other arrays. That is, if array 1 has the values 2,4, 5, and 8 and array 2 has the values 1, 0, 4, and 6, the function assigns array 3 the values 3, 4, 9, and 14. The function should take three array names and an array size as arguments. Test the function in a...
For c++ Write the code to initialize an array such that each element gets two times...
For c++ Write the code to initialize an array such that each element gets two times the value of its index (e.g. 0, 2, 4, 6, …)
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT