Question

In: Computer Science

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 any value of k.
2. For those of you who have prior programming experience, think about how you might simulate this process using arrays.

Solutions

Expert Solution

Thanks for the question, Here is the algorthm using Arrays

====================================================================

DECLARE numbers[3] as INTEGER

num <- GET 'First number: "
numbers[0] <- num

num <- GET 'Second number: "
numbers[1] <- num

num <- GET 'Third number: "
numbers[2] <- num

closest_num = numbers[0]

for i = 0 to 2:

if ABSOLUTE_VALUE(5-numbers[i])<ABSOLUTE_VALUE(5-closest_num)
closest_num = numbers[i]

i <- i + 1

OUTPUT closest_num

====================================================================

Code in C Language

#include<iostream>
#include<math.h>
using namespace std;

int main(){
  
   int numbers[3];
   int compare_to = 5;
  
   printf("Enter first number: "); scanf("%d", &numbers[0]);
   printf("Enter second number: "); scanf("%d", &numbers[1]);
   printf("Enter third number: "); scanf("%d", &numbers[2]);
  
   int closet_number = numbers[0];
  
   int i;
   for(i=0; i<3; i++){
       if(abs(closet_number-compare_to)>abs(numbers[i]-compare_to))
           closet_number = numbers[i];
   }
  
   printf("The nearest number to %d is %d", compare_to, closet_number);


   return 0;
}

===================================================================


Related Solutions

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...
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)
In C++ Given a sorted list of integers, output the middle integer. A negative number indicates...
In C++ Given a sorted list of integers, output the middle integer. A negative number indicates the end of the input (the negative number is not a part of the sorted list). Assume the number of integers is always odd. Ex: If the input is: 2 3 4 8 11 -1 the output is: Middle item: 4 The maximum number of inputs for any test case should not exceed 9. If exceeded, output "Too many numbers". Hint: First read the...
python coding Suppose a list of positive numbers is given like the following list (remember this...
python coding Suppose a list of positive numbers is given like the following list (remember this is only an example and the list could be any list of positive numbers) exampleList: 15 19 10 11 8 7 3 3 1 We would like to know the “prime visibility” of each index of the list. The “prime visibility” of a given index shows how many numbers in the list with indexes lower than the given index are prime. For instance, in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT