Question

In: Computer Science

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 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.

in c++

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.

Solutions

Expert Solution

Problem 2:

Code:

#include <iostream>

using namespace std; 

int main()
{
    string upc;
    cout<<"Enter the UPCs: ";           
    cin>>upc;                           //Take input from user
    int i,no,sum = 0,val;             //initialize variable
    
    for (i=0; i<=10;i++){
        
        no = stoi(upc.substr(i,1));   //substring by 1 and fetch integer value
        if(i%2 == 0) {
            no = no * 3;              //multiply with 3 every alternate place starting from 1st digit
        }
        sum = sum + no;               //add the digits
    }
   
    no = stoi(upc.substr(i,1));
    val = 10 - sum % 10;    
    if (val== no) {
        
        cout<<"Valid Product Code";     //print if UPCs is valid
    }
    else{
        cout<<"Invalid Product Code";   //print if UPCs is not valid
    }
    return 0;
}

Code Snippet:

Output:

Problem 3:

Code:

#include <iostream>

using namespace std;

int main()
{
    string word;
    cout<<"Enter the word: ";           
    cin>>word;                                  //Take input from user
    srand((unsigned) time(0));                  //generates random no. every time
    
    //initialize variable
    int i,j,n,length;                           
    char temp;
    string word;
    
    length = wrd.length();                      //calculate length of word

    for (n=1; n<=length;n++){                   //repeat till length
        
        word = wrd;
        i = 1 + (rand()%(length-1));            // 1 <= i < length
        j = (rand() % (length-i)) + i+1;        // i+1 <= j <= length
        cout<<n<<". i="<<i<<" j="<<j;
        temp = word[j-1];
        word[j-1]=word[i-1];
        word[i-1]=temp;
        cout<<" New word = " <<word<<endl;      //print new word

    }
    return 0;
}

Code Snippet:

Output:


Related Solutions

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...
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...
In a retail, wholesale or manufacturing company the most common revenue account is Sales. In a...
In a retail, wholesale or manufacturing company the most common revenue account is Sales. In a nonprofit the revenue is separated into what two classifications based on the intention of donors?
One of the most common types of fertilizers used by farmers is a form of the...
One of the most common types of fertilizers used by farmers is a form of the highly soluble salt ammonium chloride (NH4Cl). At a certain farm, water runoff from fields carries dissolved ammonium chloride to a nearby pond, resulting in a dissolved concentration of NH4Cl in the pond of 5 mg/L. At the same time, ammonia gas (NH3) is being produced by cows that feed around the farmer’s pond, resulting in an atmospheric level of 500 ppb above the water...
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...
Q1. franchising and Independent operator are the most common forms of retail operating models in almost...
Q1. franchising and Independent operator are the most common forms of retail operating models in almost all countries in the world. Discuss the pros and cons about it and state the reasons why. in details Q2. Pavilion is situated at the major tourists and CBD area of KL. Assuming you are the sales manager of a high end products outlet in Pavilion, and intend to boost up your sales after the reopening post the Recovery Movement Control Order (RMCO). What...
(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 =...
Please suggest most recent one advanced material products. 1. Specific properties of the product 2. The...
Please suggest most recent one advanced material products. 1. Specific properties of the product 2. The key characteristic/properties of selected advanced material product 3. Engineering Technology of selected product 4. Market role of the product
Osteoarthritis, or degenerative joint disease, is the most common form of arthritis. The cause of osteoarthritis...
Osteoarthritis, or degenerative joint disease, is the most common form of arthritis. The cause of osteoarthritis is unknown, but the condition is typically associated with aging. It's no suprise, then, that more prescription and over-the-counter (OTC) drugs are being marketed to aging baby boomers. Discuss the current spate of products and related advertising, including events that may result in taking a drug off the market. Please write or type all answers/responses that are legible and understandable. Thank you for taking...
Justify Why is the point method the most common form of job evaluation in Canada?
Justify Why is the point method the most common form of job evaluation in Canada?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT