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...
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...
Project Assignment The purpose of this assignment is for you to gain practice in applying the...
Project Assignment The purpose of this assignment is for you to gain practice in applying the concepts and techniques we learned in class. In order to complete the assignment, please do the following: 1. find or create a data set containing values of at least one interval or ratio variable for at least one-hundred cases (n >= 100); 1 2. provide basic descriptive statistics to summarize the central tendency and variability of the data; 3. provide at least one table...
Using the C Programming language, write a program that sums an array of 50 elements. Next,...
Using the C Programming language, write a program that sums an array of 50 elements. Next, optimize the code using loop unrolling. Loop unrolling is a program transformation that reduces the number of iterations for a loop by increasing the number of elements computed on each iteration. Generate a graph of performance improvement. Tip: Figure 5.17 in the textbook provides an example of a graph depicting performance improvements associated with loop unrolling. Marking:- Optimize the code for an array of...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Programming II: C++ - Programming Assignment Fraction Object with Operator Overloads Overview In this assignment, the...
Programming II: C++ - Programming Assignment Fraction Object with Operator Overloads Overview In this assignment, the student will write a C++ program that implements a “fraction” object. When writing the object, the student will demonstrate mastery of implementing overloaded operators in a meaningful way for the object. When completing this assignment, the student should demonstrate mastery of the following concepts: · Mathematical Modeling - Fractions · Operator Overloading – Binary Operators (Internal Overload) · Operator Overloading – Binary Operator (External...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT