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