Question

In: Computer Science

The purpose of this C++ programming assignment is to practice using an array. This problem is...

The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest.


For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable.


Background
The world-known gangster Vito Deadstone is moving to New York. He has a very big family there, all of them living in Lamafia Avenue. Since he will visit all his relatives very often, he is trying to find a house close to them.


Problem
Vito wants to minimize the total distance to all of them and has blackmailed you to write a program that solves his problem.


Input
The input consists of several test cases. The first line contains the number of test cases.


For each test case you will be given the integer number of relatives r ( 0 < r < 500) and the street numbers (also integers) s1, s2, ..., si, ..., sr where they live ( 0 < si < 30000 ). Note that several relatives could live in the same street number.


Output
For each test case, your program must write the the optimal Vito's house number and minimal sum of distances from the optimal Vito's house to each one of his relatives. The distance between two street numbers si and sj is dij = |si-sj|. Note: The red font below indicates user input

Sample Input/output

Number of test case: 2

Test case #1
Number of relative: 2

Street numbers: 2 4

Vito's house number: 2

sum of distances: 2

Test case #2

Number of relative: 3

Street numbers: 2 4 6

Vito's house number: 4

sum of distances: 4



Note: If you input the above sample data from the keyboard, the output will be printed on your screen right after each test case.


Another sample input and output:

Number of test case: 1

Test case #1
Number of relative: 10

Street numbers: 2 12 23 10 3 40 20 19 4 10

Vito's house number: 10

sum of distances: 85

Solutions

Expert Solution

C++ code:

//=======================================================

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

int main()
{
   int test;
   cout << "Number of test case: ";
   cin >> test;
   int t = 0;
   while (t<test)
   {
       t++;
       cout << "Test case #" << t << endl;
       cout << "Number of relative: ";
       int relative;
       cin >> relative;
       int *arr = new int[relative]; // arr store streat location of relative
       cout << "Street numbers: ";
       for (int i = 0; i < relative; i++)cin >> arr[i];
       //arr sorting
       for (int i = 1; i < relative; i++)
       {
           int key = arr[i];
           int j;
           for (j = i - 1; j >= 0; j--)
           {
               if (arr[j]>key) arr[j + 1] = arr[j];
               else break;
           }
           arr[j + 1] = key;
       }
       int mid;
       if (relative % 2 == 0) mid = relative / 2 - 1;
       else mid = relative / 2;
       int best_streat = arr[mid];

       int distance=0;
       for (int i = 0; i < relative; i++)
       {
           distance += abs(best_streat - arr[i]);
       }
       cout << "Vito's house number: " << best_streat<<endl;
       cout << "sum of distances:    " << distance << endl;
   }

system("pause");
   return 0;
}

sample input/output

//===========================================================

explanation: Optimal street for vito's house is median street of all relatives. In case of odd relative there is only single median but in case of even relative there are 2 median. vito can stay at any location sum of distance between vito house and relatives house will remain same. Median street have equal number of street at its both side. If we choose any random location (x) and there are uneven relatives on both side. Then he should move toward more relatives to decrease total distance because vito is reducing distance from more relatives and increasing distance from less relative.He will move till relatives around its street are equal.


Related Solutions

Objective: The purpose of this programming assignment is to practice using STL containers. This problem is...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is selected from the online contest problem archive (Links to an external site.), which is used mostly by college students world wide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest (Links to an external site.). For your convenience, I copied the description of the problem below with my note on the...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering Inheritance. Core Level Requirements (up to 6 marks) The scenario for this assignment is to design an online shopping system for a local supermarket (e.g., Europa Foods Supermarket or Wang Long Oriental Supermarket). The assignment is mostly concentrated on the product registration system. Design and draw a UML diagram, and write the code for the following classes: The first product category is a fresh...
Problem 2. Purpose: practice algorithm design using dynamic programming. A subsequence is palindromic if it is...
Problem 2. Purpose: practice algorithm design using dynamic programming. A subsequence is palindromic if it is the same whether read left to right or right to left. For instance, the sequence A,C,G,T,G,T,C,A,A,A,A,T,C,G has many palindromic subsequences, including A,C,G,C,A and A,A,A,A (on the other hand, the subsequence A,C,T is not palindromic). Assume you are given a sequence x[1...n] of characters. Denote L(i,j) the length of the longest palindrome in the substring x[i,...,j]. The goal of the Maximum Palindromic Subsequence Problem (MPSP)...
In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will...
In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will use the concept of object aggregation (i.e., has-a relationship between objects). You will implement a Java application, called MovieApplication that could be used in the movie industry. You are asked to implement three classes: Movie, Distributor, and MovieDriver. Each of these classes is described below. The Movie class represents a movie and has the following attributes: name (of type String), directorsName (of type String),...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
*C PROGRAMMING LANGUAGE* a) Given an array of size n, sort the array using pointers using...
*C PROGRAMMING LANGUAGE* a) Given an array of size n, sort the array using pointers using malloc or calloc. Examples: Input: n = 5, A= {33,21,2,55,4} Output: {2,4,21,33,55} b) Write a function that declares an array of 256 doubles and initializes each element in the array to have a value equal to half of the index of that element. That is, the value at index 0 should be 0.0, the value at index 1 should be 0.5, the value at...
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
C++ PROGRAMMING. S-DES: The purpose of this assignment is to implement algorithm for encryption with the...
C++ PROGRAMMING. S-DES: The purpose of this assignment is to implement algorithm for encryption with the simplified DES-type algorithm. · Item #1. Write a C++ program that performs one round of the simplified DES-type algorithm. Test your code with a plaintext = 011100100110 and K = 010011001.
C programming problem < purpose: need to find time and space complexities in this problem and...
C programming problem < purpose: need to find time and space complexities in this problem and which function(algorithm) will be used in this problem> I have to design an efficient algorithm which solves this problem. Also, it needs to include time and space complexities of this algorithm. There is a matrix (N times N) of integers which rows and columns are sorted in non-decreasing order. A sorted matrix and integer M will be given and we need to find the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT