All code must be in C++ using G++ compiler Write a program where a hacker tries
ID: 3809602 • Letter: A
Question
All code must be in C++ using G++ compiler
Write a program where a hacker tries to guess a PIN. The program should have an array of five integers named PIN, and should initialize each PIN number with a random number in the range of 0 through 9. Now a hacker tries to guess the PIN stored in the system by entering 5 digits at the console. The system reads the numbers entered by the user into an array named GuessedPIN. After reading the numbers entered by the hacker, the program is to compare the corresponding elements in the two arrays and keep a count of the digits that match. For example, the following shows the PIN array and the GuessedPIN array with sample numbers stored in each. There are two matching digits (array elements 2 and 4) PIN Guessed PINExplanation / Answer
The code is given below :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include<stdio.h>
#include<stdlib.h>
#include <cstdlib>
#include <iostream>
int main()
{
int pin[5] = {0,0,0,0,0};// generated using random number
int tmp = 0, tmp_rand = 0;
unsigned long int guessedpin = 0;
int den = 0, tmp_number = 0;
int counter = 0;
for(tmp = 0; tmp < 5; tmp++)
{
tmp_rand = rand() % 10; // Temporary random number
pin[tmp] = tmp_rand;
}
std::cout << "please enter 5 digit positive number to hack the system" << std::endl;
std::cin >> guessedpin;
std::cout << std::endl;
for(tmp = 0; tmp < 5; tmp++)
{
den = pow(10, 4 - tmp); // Denominator
tmp_number = (guessedpin - (guessedpin % den)) / den; // get the digit separately from whole number to compare with pin
guessedpin = guessedpin - (tmp_number * den);
if(tmp_number == pin[tmp]) // Comparision
{
counter++;
std::cout << "Array element "<<tmp+1<<" (value : "<< pin[tmp] << ") matches." << std::endl; // Necesaary prints
}
}
std::cout << "There are total " << counter << " matching digits." << std::endl;
if(counter == 5)
std::cout << " and password hacked" << std::endl;
}
Save the file as pin.cpp and run the command g++ pin.cpp which will give a.out file.
Run the command ./a.out and you will find the proper output.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.