Question

In: Computer Science

Problem 2: Universal Product Codes(UPCs) Retail products are identified by their Universal Product Codes (UPCs). The...

Problem 2: Universal Product Codes(UPCs) Retail products are identified by their Universal Product Codes (UPCs). The most common form of a UPC has 12 decimal digits: The first digit identifies the product category, the next five digits identify the manufacturer, the following five identify the particular product, and the last digit is a check digit. The check digit is determined in the following way: • Beginning with the first digit multiply every second digit by 3. • Sum all the multiplied digits and the rest of the digits except the last digit. • If the (10 - sum % 10) is equal to the last digit, then the product code is valid. • Otherwise it is not a valid UPC. The expression is: sum = 3.x1 + x2 + 3.x3 + x4 + 3.x5 + x6 + 3.x7 + x8 + 3.x9 + x10 + 3.x11 where the x’s are the first 11 digits of the code. If you choose to add the last digit also in the second step and if the sum is a multiple of 10, then the UPC is valid. Either way, you still need to perform the modular division to check whether the given number is a valid code. In this problem, you need to use either a string or long long integer type for the product code because it is 12 digits long. If you use string, you can convert one character substring of the string in to a single digit integer from left to right using the function stoi(str.substr(i,1)). This way you do not need to get last digit of the number and then divide the number by 10. 1 Problem 3: Translate the following pseudocode for randomly permuting the characters in a string into a C++ program. Read a word. repeat word.length() times Pick a random position i in the word, but not the last position. Pick a random position j > i in the word. swap the letters at positions j and i. Print the word. Please work on this problem after we learn to generate random numbers in the class which is on Wednesday the latest. These problems only deal with simple loop while, for and do loops. You will get a second set of problems next week on nested loops. in loops and string and not fuction that all the info i have

Solutions

Expert Solution

Problem 2: Validity of UPC in C++

#include <iostream>

using namespace std;

int main()
{
    string upc;
    int result = 0;
    cout << "Enter the UPC code: ";
    cin >> upc;
    if (upc.length() < 12){
        cout << "Invalid UPC!" << endl;
        return 0;
    }
    for(int i = 0; i < upc.length() -1; i++){
        if (i%2 == 0){
            result += 3*stoi(upc.substr(i,1));
        }
        else{
            result += stoi(upc.substr(i,1));
        }
    }
    if((10 - result%10) == stoi(upc.substr(upc.length()-1,1))){
        cout << "Valid UPC!" << endl;
    }else{
        cout << "Invalid UPC!" << endl;
    }
}

Problem 3: C++ code

#include <iostream>
#include <random>

using namespace std;

int main()
{
    string word;
    cout << "Enter the word: "; 
    cin >> word;
    cout << "Original word: " << word << endl;

    // define the range in which we can select i (every letter except last)
    int upper = word.length() - 1;
    int lower = 0;
    int i = (rand() % (upper - lower)) + lower; // generate the random int in the range

    // define the range for j (j>i)
    upper = word.length();
    lower = i+1;
    int j = (rand() % (upper - lower)) + lower; // gernerate the random int for j in the range

    char word_i = word[i], word_j = word[j];
    word[i] = word_j;
    word[j] = word_i;
    cout << "Letter locations being swapped are i= " << i << " j=" << j << endl;
    cout << "("<<word_i << " is swapped with " << word_j << ")"<< endl;
    cout <<"After swapping letters: "<< word <<endl;

}


Related Solutions

problem 2 Retail products are identified by their Universal Product Codes (UPCs). The most common form...
problem 2 Retail products are identified by their Universal Product Codes (UPCs). The most common form of a UPC has 12 decimal digits: The first digit identifies the product category, the next five digits identify the manufacturer, the following five identify the particular product, and the last digit is a check digit. The check digit is determined in the following way: • Beginning with the first digit multiply every second digit by 3. • Sum all the multiplied digits and...
Problem 1: We are analyzing 2 products – product X and product Y. Product X requires...
Problem 1: We are analyzing 2 products – product X and product Y. Product X requires 4 Part As and 3Part B’s. Product Y requires 3 Part A’s and 2 Part B’s. The standard cost for Part A is $24 per unit. The standard cost for Part B is $12 per unit. During this month, the company purchased 50,000 units of Part A for $1,220,000 (there was no beginning balance). During this month, the company purchased 50,000 units of Part...
Problem 2 Cabanos Company manufactures two products, Product C and Product D. The company estimated it...
Problem 2 Cabanos Company manufactures two products, Product C and Product D. The company estimated it would incur $160,790 in manufacturing overhead costs during the current period. Overhead currently is applied to the products on the basis of direct labour hours. Data concerning the current period's operations appear below: Product C Product D Estimated Volume (units) 3,400 4,800 Direct Labour Hours per Unit 1.40 1.90 Direct Materials Cost per Unit $ 7.40 $12.70 Direct Labour Cost per Unit $14.00 $19.00...
(Please use Python) Suppose that a company sells five products with product codes p101, p107, p122,...
(Please use Python) Suppose that a company sells five products with product codes p101, p107, p122, p125, and p126. The company has three warehouses, which are located in St. Louis, Chicago, and Kansas City. The retail value for each of the five products and the inventory for each of the warehouses are stored in dictionaries, as shown below. Copy the code below into a code cell, and then execute that cell. prices = {'p101':37.52, 'p117':56.98, 'p122':43.72, 'p125':48.33, 'p126':52.45} inventory =...
What are some of the challenges identified by the World Health Organization (WHO) in bringing universal...
What are some of the challenges identified by the World Health Organization (WHO) in bringing universal health care to countries around the world?
The green and red Company manufactures two products, Product 1 and Product 2. Product 2 was...
The green and red Company manufactures two products, Product 1 and Product 2. Product 2 was developed as an attempt to enter a market closely related to that of Product 1. Product 2 is the more complex of the two products, requiring three hours of direct labour time per unit to manufacture compared to one and one-half hours of direct labour time for Product 1. Product 2 is produced on an automated production line. Overhead is currently assigned to the...
Problem 2 A lot of 111 electronic products are inspected. A sample of 10 products is...
Problem 2 A lot of 111 electronic products are inspected. A sample of 10 products is randomly selected. If 102 of the products are meeting the specifications, FIND the following: (a) The number of samples contains exactly two defective products. (b) The number of samples contains a maximum of one nonconforming product. (c) The number of samples contains a minimum of one nonconforming product. (d) The number of samples contains a minimum of two nonconforming products.
green & red manufactures two products, Product 2 and Product 8. Product 8 is of fairly...
green & red manufactures two products, Product 2 and Product 8. Product 8 is of fairly recent origin, having been developed as an attempt to enter a market closely related to that of Product 2. Product 8 is the more complex of the two products, requiring two hours of direct labour time per unit to manufacture compared to one hour of direct labour time for Product 2. Product 8 is produced on an automated production line. Overhead is currently assigned...
We are analyzing 2 products – product X and product Y. Product X requires 3 Part...
We are analyzing 2 products – product X and product Y. Product X requires 3 Part As and 4 Part B’s. Product Y requires 2 Part A’s and 3 Part B’s. The standard cost for Part A is $12 per unit. The standard cost for Part B is $24 per unit. During this month, the company purchased 50,000 units of Part A for $587,500 (there was no beginning balance). During this month, the company purchased 100,000 units of Part B...
We are analyzing 2 products – product X and product Y. Product X requires 3 Part...
We are analyzing 2 products – product X and product Y. Product X requires 3 Part As and 4 Part B’s. Product Y requires 2 Part A’s and 3 Part B’s. The standard cost for Part A is $12 per unit. The standard cost for Part B is $24 per unit. During this month, the company purchased 50,000 units of Part A for $587,500 (there was no beginning balance). During this month, the company purchased 100,000 units of Part B...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT