134k views
2 votes
cpp Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates a threshold. Output all integers less than or equal to that last threshold value. Ex: If the input is:

User Frida
by
4.7k points

1 Answer

2 votes

Answer:

#include <iostream>

int main() {

// The length of the list

int l;

// The array containing the numbers

int *list;

// Threshold, last value in array

int thresh;

// Read in first value and create an array with the size of that value

scanf("%d", &l);

list = (int*) malloc(l * sizeof(int));

for(int i = 0; i < l; i++) {

scanf("%d", &list[i]);

}

thresh = list[l - 1];

// Print the values

for(int i = 0; i < l - 1; i++) {

if (list[i] <= thresh) {

printf("%d ", list[i]);

}

}

printf("\\");

free(list);

}

Step-by-step explanation:

The program assumes at least two values are provided. The length of the list and the threshold.

We first read in an integer, and find out how many more integers we need to read. We create an array with the appropriate size, and loop through to fill the array with values. We know the last value is the threshold, and so we do one more loop to compare the values in the array with the threshold.

User Valentasm
by
4.7k points