Question

In: Computer Science

2. Medium problems 2-1. Given a positive decimal number, write a function that transforms it into...

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.

Solutions

Expert Solution

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:


Related Solutions

Write a function decimalToBinary(n) that converts a positive decimal integer n to a string representing the...
Write a function decimalToBinary(n) that converts a positive decimal integer n to a string representing the corresponding binary number. Do the conversion by repeatedly dividing the number n by 2 using integer division, keepting track of the remainders, until the number is reduced to 0. The remainders written in reverse order form the binary number string. Do integer division of 5 by 2, so that N//2 is 2 with remainder 1. Now divide 2 by 2 to get 1 with...
Write a function DIVISORS in MIPS. This function takes a positive number from the register a0...
Write a function DIVISORS in MIPS. This function takes a positive number from the register a0 and prints in a column all of its divisors including 1 and the number itself. Don't forget that printing a number and printing a character requires two different system calls. Here is an example. If the number in a0 is 6 the following output appears: 1 2 3 6
Write a function posnum that prompts the user to enter a positive number and loops to...
Write a function posnum that prompts the user to enter a positive number and loops to error-check. It returns the square root of the positive number entered by the user. It calls a subfunction in the loop to print an error message. The subfunction has a persistent variable to count the number of times an error has occurred. Here is an example of calling the function: >> squarerootvalue = posnum Enter a positive number: -5 Error #1: Follow instructions! Does...
A formula with a positive integer (less than 32 bits) and a positive decimal (number with...
A formula with a positive integer (less than 32 bits) and a positive decimal (number with decimal points) is expressed in the median formula. Change the given median to postfix and write a program that outputs the results of the calculation. operand ::= positive integer or positive error Positive integer ::= A number expressed as less than 32 bits consisting of 0 to 9. Positive integer representation of 0, 0100, 00934, 1056, 65535 is allowed Positive decimal ::= Positive integer...
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change...
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change it to convert an octal number to decimal in perl language.
1.Write a Java program that inputs a binary number and displays the same number in decimal....
1.Write a Java program that inputs a binary number and displays the same number in decimal. 2.Write Java program that inputs a decimal number and displays the same number in binary.
A) Fill in the blanks. (Enter an exact positive number as an integer, fraction, or decimal.)...
A) Fill in the blanks. (Enter an exact positive number as an integer, fraction, or decimal.) In a normal distribution, x = 3 and z = −1.19. This tells you that x = 3 is ________ standard deviations to the _____ (left or right) of the mean B) Fill in the blanks. (Enter an exact positive number as an integer, fraction, or decimal.) In a normal distribution, x = −2 and z = 6. This tells you that x =...
Write a function such that given a number N, display the N*N multiplication matrix from 1...
Write a function such that given a number N, display the N*N multiplication matrix from 1 to N. Then, write a C++ program such that, it prints the multiplication matrices of numbers 1,4,7, and 10 using a loop concept. Check the sample output below, to see how the program should work. Make sure to have your output exactly the same as the below output.
Write a program (preferably in Java) that, given an arithmetic expression, first transforms it to a...
Write a program (preferably in Java) that, given an arithmetic expression, first transforms it to a postfix form, and then computes its value (by using stack-based algorithms). Assume that all the numbers in the arithmetic expression are one-digit numbers, i.e., each of these numbers is either 0, or 1, or 2, ..., or 9. For example, your program should correctly process expressions like 2+3*4, but there is no need to process expressions like 11+22.
Write a function that counts the number of times a given integer occurs in a Linked...
Write a function that counts the number of times a given integer occurs in a Linked List. What is the time complexity of your algorithm? Justify your answer in python
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT