Question

In: Computer Science

Suppose you have a list containing exactly three integer numbers. Each number could be positive, negative,...

Suppose you have a list containing exactly three integer numbers. Each number could be positive, negative, or zero. Write a C program that first asks the user for each of the numbers. After that, it should determine and display the number from the list that is nearest to the positive number five (5).

Examples:
If the numbers are 0, -3, and 8, then the displayed output is 8.
If the numbers are 6, 5, and 4, then the output is 5.
If the numbers are 3, 4, and 11, then the output is 4.
If the numbers are 4, 1, 6, then it turns out that 4 and 6 are both equidistant from 5. In this situation, the output should be the first number checked that satisfied the criterion. For this example, if you started on the left at 4 and proceeded to the right, the output is 4.

Solutions

Expert Solution

CODE IN C :

#include<stdio.h>
#include<limits.h>
int main()
{
printf("Enter the elements of the list : ");
int list[3];
for(int i = 0; i < 3; i++)
{
scanf("%d", &list[i]);
}
int x; // Element to be searched for
printf("Enter the number to be searched for : ");
scanf("%d", &x);
int diff = INT_MAX; // stores the difference, initialized to the maximum possible value
int ans = 0;
for(int i = 0; i < 3; i++)
{
if(abs(x-list[i])<diff) // if diff is greater than the diff between the presnt value and x, update it
{
diff = abs(x-list[i]);
ans = list[i]; // stores the nearest value to x
}
}
printf("The number from the list that is closest to %d", x);
printf(" is %d", ans);
}

OUTPUT SNIPPET :

  • Comments have been written for your understanding.
  • Output snippet for all the sample cases mentioned in the question have also been attached.

Please give an upvote if you liked my solution. Thank you :)


Related Solutions

Suppose you have a list containing k integer numbers. Each number could be positive, negative, or...
Suppose you have a list containing k integer numbers. Each number could be positive, negative, or zero. Write pseudocode for a program that first asks the user for each of the numbers. After that, it should determine and display the number from the list that is nearest to the positive number five (5). Notes: 1. Try working this out for k = 4 and k = 5. See if you can find an algorithmic pattern that can be extended for...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." In C, Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program in C to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume the length...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program in C language to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters....
1a. What is the largest positive and negative number that you could represent with 7 bits...
1a. What is the largest positive and negative number that you could represent with 7 bits using signed magnitude? Show your work. 1c. Solve the following decimal notation equation using 8-bit binary numbers and 2’s complement notation: 69 - 7 = Show your work
For example, suppose you had a program to determine if a number is positive, negative or zero.
  For example, suppose you had a program to determine if a number is positive, negative or zero. Read in a number and then use the if statement to determine if the number is positive (>0) or negative (<0) or zero (==0): if (x>0)//display positive messageelse if (x<0)   //display negative messageelse//display zero message
Language C: Suppose you are given a file containing a list of names and phone numbers...
Language C: Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
Positive, Negative, or zero. Suppose you are looking at three separate relationships that are modeled by...
Positive, Negative, or zero. Suppose you are looking at three separate relationships that are modeled by linear functions. One linear model has a positive slope, one has a negative slope, and one has a slope of zero. What do these slopes tell you about the relationships they model? Find practical examples of each of the three.
in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT