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 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...
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)
Write a C++ code that keeps reading integer numbers (x) until a negative number is entered....
Write a C++ code that keeps reading integer numbers (x) until a negative number is entered. Every time x is entered, the program does the following: calculates and prints the product of odd numbers. calculates and prints the count of numbers that ends with 19. Example, 19, 3219, 50619,....etc calculates and prints the percentage of the numbers that contain 3 digits exactly, such as, 301, 500, 206,...etc.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT