47.2k views
3 votes
For any element in keysList with a value smaller than 40, print the corresponding value in itemsList, followed by a space.

Ex: If keysList = {32, 105, 101, 35} and itemsList = {10, 20, 30, 40}, print:


10 40


#include int main (void) { const int SIZE_LIST = 4; int keysList[SIZE_LIST]; int itemsList[SIZE_LIST]; int i; keysList[0] = 13; keysList[1] = 47; keysList[2] = 71; keysList[3] = 59; itemsList[0] = 12; itemsList[1] = 36; itemsList[2] = 72; itemsList[3] = 54; //Loop from 0 to NUM_VALS-1 and accessing corresponding elements. for(i=0;i

User Nurchi
by
5.0k points

2 Answers

5 votes

Final answer:

To print values from itemsList corresponding to keys in keysList that are smaller than 40, a loop is used to check and print the matching itemsList values.

Step-by-step explanation:

The question asks to iterate over a list of key values (keysList) and print the corresponding values from another list (itemsList) when the key values are less than 40. To accomplish this task, we need to use a loop to compare each key to the specified threshold (40), and if it's smaller, output the corresponding item from itemsList followed by a space. Here is a corrected version of the C code snippet to achieve the desired result:

#include

int main (void) {
const int SIZE_LIST = 4;
int keysList[SIZE_LIST];
int itemsList[SIZE_LIST];
int i;

keysList[0] = 13;
keysList[1] = 47;
keysList[2] = 71;
keysList[3] = 59;
itemsList[0] = 12;
itemsList[1] = 36;
itemsList[2] = 72;
itemsList[3] = 54;

for(i = 0; i < SIZE_LIST; ++i) {
if(keysList[i] < 40) {
printf("%d ", itemsList[i]);
}
}

return 0;
}

This loop iterates from 0 to SIZE_LIST-1, checking if each element in keysList is smaller than 40. If the condition is true, it prints the corresponding element from itemsList followed by a space.

User Gary Lopez
by
4.4k points
6 votes

Final answer:

To print the corresponding values from itemsList for any element in keysList with a value smaller than 40, you can use a for loop to iterate through the indices of keysList and check if the value is less than 40. If it is, print the corresponding value from itemsList.

Step-by-step explanation:

In this code, we have two arrays: keysList and itemsList. The goal is to iterate through the elements of keysList and print the corresponding values from itemsList if the value in keysList is less than 40.

To achieve this, we can use a for loop that iterates through the indices of keysList. Inside the loop, we can check if the value at the current index is less than 40. If it is, we can print the value at the same index in itemsList, followed by a space.

Here's the modified code:

#include <stdio.h>

int main(void) {
const int SIZE_LIST = 4;
int keysList[SIZE_LIST];
int itemsList[SIZE_LIST];
int i;

keysList[0] = 13;
keysList[1] = 47;
keysList[2] = 71;
keysList[3] = 59;

itemsList[0] = 12;
itemsList[1] = 36;
itemsList[2] = 72;
itemsList[3] = 54;

for (i = 0; i < SIZE_LIST; i++) {
if (keysList[i] < 40) {
printf("%d ", itemsList[i]);
}
}

return 0;
}

Running this code will produce the output: 12 36