In: Computer Science
2. Medium problems
2-1. Given a positive decimal number, write a function that transforms it into binary, and prints out the result. You may assume that the string contains at least 1 and no more than 9 decimal digits. The output should only include the binary digits followed by a new line.
Hint: You might need to convert a string to integer. Have a look at the stoi(string) in the C++11 string library. If you need to compile with C++11, include the compilation flag -std=c++11 in your compile command.
Signature: void print_as_binary(std::string decimal_number)
2-2. Given a binary number represented as an array, write a function that takes the array and its size as a parameter, and returns the integer value. You may assume that there are at least 1 and no more than 30 numbers in the array and that all the values are either 0 or 1. The array is ordered with most significant binary digit at the start (index 0) and the least significant digit at the end.
Signature: int binary_to_number(int binary_digits[], int number_of_digits)
Definition: a palindrome is a sequence that reads the same backwards as forwards. Hence, 101, 120021 and 1 are all numerical palindromes. A palindrome array would be of the form [1,2,2,1] for example. An empty array is a palindrome by definition.
2-3. Given an array of integers, write a function to calculate the sum of the elements if it is a palindrome array. If it is not a palindrome array, your function must return -2. Your function must call separate functions to check whether or not the array is a palindrome and to calculate the sum of its elements. If the length is 0 or negative each function must return -1 or false as its result.
Signature: int sum_if_a_palindrome(int integers[], int length)
Signature: bool is_a_palindrome(int integers[], int length)
Signature: int sum_elements(int integers[], int length)
2-4. Given an array of integers, write a function to determine its maximum and minimum elements and then return their sum. Your function must call separate functions to identify the maximum and minimum elements. If the length is 0 or negative each function must return -1 as its result.
Signature: int sum_min_and_max(int integers[], int length)
Signature: int max_integer(int integers[], int length)
Signature: int min_integer(int integers[], int length)
3. Tricky problems
With these problems, we are giving you a free hand at writing the signature of your functions. Follow the templates from the easier problems, and ask for help if unsure! The following two problems are set in the context of a supermarket checkout. There is only one checkout with a single line of customers each with items they are wanting to buy.
Note: The web submission system will ignore these two programs so no marks will be awarded for them even if you submit them.
3-1. Write code to capture the scenario: at the checkout, the customer who is first in the queue pays an amount equal to the value of his/her products and then leaves the supermarket. The next customer in the queue is then served. For each customer in the queue (you have initialised the queue with 10 customers), print out how much they have to pay.
3-2. Write code to capture the scenario: you have been serving customers for a while and your queue no longer has 10 customers in it. A new customer arrives at the checkout.
please go through code and output.
CODE:
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
//2-1
void print_as_binary(std::string decimal_number)
{
int num = stoi(decimal_number);
if(num < 1)
{
cout << "number should not
less than 1 " << endl;
exit(0);
}
for(int i=31; i>=0 ; i--)
{
cout << ((num >> i)
& 1) ;
}
cout << endl;
}
//2-2
int binary_to_number(int binary_digits[], int
number_of_digits)
{
int number = 0;
for(int i=0; i<number_of_digits; i++)
{
number = number | (binary_digits[i]
<< i);
}
return number;
}
//2-3
bool is_a_palindrome(int integers[], int length)
{
int i=0, j=0;
if(length < 1)
{
return false;
}
if(length == 1)
return true;
if(length%2 != 0)
{
j = (length/2) +1;
}
else
{
j = (length/2);
}
for(i=(length/2)-1; i>=0; i--,j++)
{
if(integers[i] !=
integers[j])
{
return
false;
}
}
return true;
}
int sum_if_a_palindrome(int integers[], int length)
{
int sum = 0;
if(is_a_palindrome(integers, length))
{
for(int i=0; i<length;
i++)
{
sum +=
integers[i];
}
}
else
{
return -2;
}
return sum;
}
int sum_elements(int integers[], int length)
{
int sum = 0;
for(int i=0; i<length; i++)
{
sum += integers[i];
}
return sum;
}
//2-4
int max_integer(int integers[], int length)
{
int max=0;
for(int i=0; i<length; i++)
{
if(max < integers[i])
{
max =
integers[i];
}
}
cout << max << " + ";
return max;
}
int min_integer(int integers[], int length)
{
int min =integers[0];
for(int i=0; i<length; i++)
{
if(min > integers[i])
{
min =
integers[i];
}
}
cout << min << " = ";
return min;
}
int sum_min_and_max(int integers[], int length)
{
return (max_integer(integers, length) +
min_integer(integers, length));
}
int main()
{
//2-1
string number;
cout << "Enter positive number : " <<
endl;
cin >> number;
print_as_binary(number);
//2-2
int array[30] =
{0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,0,1,0,0,1,0,1,1};
binary_to_number(array,30);
cout << endl << endl;
//2-3
int integers[6] = {1,2,1,1,2,1};
cout << "Sum of elements: " <<
sum_elements(integers, 6);
cout << endl << endl;
if( sum_if_a_palindrome(integers, 6) == -2)
{
cout << "not palindrome "
<< endl;
}
else
{
cout << "sum of palindrome: "
<< sum_if_a_palindrome(integers, 6);
cout << endl <<
endl;
}
//2-4
cout << "Sum of max and min : " <<
sum_min_and_max(integers, 6);
cout << endl;
}
OUTPUT: