In: Computer Science
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
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;
}