Could Someone help me with this assignment? I think I have up to the 4th step in
ID: 3710971 • Letter: C
Question
Could Someone help me with this assignment? I think I have up to the 4th step in the program. I'll post my code if needed.
Hacking into and comprising an organization’s networks has become a very serious issue.
Therefore, management has asked you to create and execute a program which simulates hacking activity over a certain timeframe. The program must only use pointers to access any arrays. You are not allowed to use array indexes
A sample execution of the program is as follows :
Enter the number of the web servers: 6
Enter the number of hours to simulate: 5
Hacking attempts per hour:
Hour Server1 Server2 Server3 Server4 Server5 Server6
0 0 0 0 0 0 0
1 4 1 0 7* 5 2
2 2 3 4* 1 0 2
3 0 3 1 4* 3 1
4 1 4 5* 1 5* 3
5 5* 2 3 2 0 4
Total 12 13 13 15* 13 12
Project 4 - Pseudocode
1. Prompt user for number of servers and hours
2. Accept user input
3. Create a dynamic array for the servers and one for the totals (See example 12-6)
4. Call initialize function.
5. Output heading lines and the zero hour line
6. Loop through hours of the simulation
6a. Loop through each server assigning number of hack attempts (call rand_set)
6b. Determine pointer value to highest number of hack attempts (part of rand_set)
6c. Output an hour’s line of information. (call print)
7. Output the total line.
Next Page à
Some if the identifiers needed are :
int server_count; // number of servers
int *highest_attempts; // pointer to highest number in
// server array
int *last; // pointer to last value in array
Suggested functions are :
initialize: Function that places zeros in all elements of the arrays. It also calls srand to initialize the random number seed, sets the value of highest-attempts to NULL and appropriately initializes last based on size.
print: Prints the values in the array. print first goes to a new line. It then prints each array value with spaces before and after. It prints the element pointed to by highest_attempts with an * after.
rand_set: Randomly assigns a value to an element in the server array. highest_attempts is appropriately changed.
These functions should all work properly if server_count is changed.
NOTE : Example 5-6 on the textbook for an explanation of how to generate a random number.
Here is a sample program that generates random numbers between 0 and 9:
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
int main( )
{
int i;
srand (time(NULL));
for(int j = 0; j < 5; j++)
cout << rand() % 10 << " ";
}
Explanation / Answer
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int getRandNUmber(void)
{
return rand() % 10;
}
void getUserInputs(int &server, int &hour)
{
cout << "Enter the number of the web servers: ";
cin >> server;
cout << "Enter the number of hours to simulate: ";
cin >> hour;
}
void initialize(int numOfServer, int numOfHour, int ** &serverHourArray, int *&totalArray)
{
srand (time(NULL));
int count = 1;
for (int hour = 0 ; hour < numOfHour; hour++)
{
for (int server = 0; server < numOfServer; server++)
{
*(*(serverHourArray + hour) + server) = getRandNUmber();
*(totalArray + server) += *(*(serverHourArray + hour) + server);
}
}
}
void printValues(int numOfServer, int numOfHour, int ** &serverHourArray, int *&totalArray)
{
int maxServerIndex = 0;
//Print Headings
cout << left << setw(8) << "Hour";
for (int server = 1; server <= numOfServer; server++)
{
cout << "server" << server << " ";
}
cout << endl;
//print zero hour hack attempts
cout << left << setw(8) << 0; //zero hour
for (int server = 1; server <= numOfServer; server++)
{
cout << left << setw(8) << 0; //hack attempt in zero hour
}
cout << endl;
//print hourly hack attempts
for (int hour = 0 ; hour < numOfHour; hour++)
{
//check for max element in row
maxServerIndex = 0;
for (int server = 0; server < numOfServer; server++)
{
if (*(*(serverHourArray + hour) + server) > *(*(serverHourArray + hour) + maxServerIndex))
{
maxServerIndex = server;
}
}
cout << left << setw(8) << hour + 1;
for (int server = 0; server < numOfServer; server++)
{
if (server == maxServerIndex)
{
cout << "*";
}
cout << left << setw(8) << *(*(serverHourArray + hour) + server);
}
cout << endl;
}
//get max element in total array
maxServerIndex = 0;
for (int server = 0; server < numOfServer; server++)
{
if (*(totalArray + server) > *(totalArray + maxServerIndex))
{
maxServerIndex = server;
}
}
//Print Total Array
cout << "Total : ";
for (int server = 0; server < numOfServer; server++)
{
if (server == maxServerIndex)
{
cout << "*";
}
cout << left << setw(8) << *(totalArray + server);
}
cout << endl;
}
int main( )
{
int numOfServer = 0;
int numOfHour = 0;
getUserInputs(numOfServer, numOfHour);
/* create a 2-d array for server - hour */
int **serverHourArray = new int* [numOfHour];
for (int hour = 0; hour < numOfHour; hour++)
{
*(serverHourArray + hour) = new int [numOfServer];
}
int *totalArray = new int [numOfServer];
for (int server = 0; server < numOfServer; server++)
*(totalArray + server) = 0;
initialize(numOfServer, numOfHour, serverHourArray, totalArray);
cout << "Hacking attempts per hour:" << endl;
printValues(numOfServer, numOfHour, serverHourArray, totalArray);
return 0;
}