
Patrick B. answered 12/20/20
Math and computer tutor/teacher
And what EXACTLY is the data structure that is supposed to be used?
Are the ip addresses supposed to be read from file? If so, we also need the
layout of the input file...
Please post a complete description of the problem, the data structures, the input, and
the output...
otherwise, you are going to say the given solution is wrong and it is a WASTE OF TIME
AND ENERGY for ALL PARTIES INVOLVED!!!
Until that time, here is a WORKING PROGRAM that does EXACTLY what you have
asked using the little specification you have given..
The source code has been uploaded to the RESOURCES section if you want it...
Shades of SCRUM programming.... smh
-------------------------------------------------------------------------------------------------
using namespace std;
#include <iostream>
#include <stdlib.h>
#include <string.h>
#define MAX_TABLE_SIZE (10000)
#define MAX_STR_LEN (25)
typedef struct _TTableEntry
{
char ipAddress[MAX_STR_LEN];
int count;
}* TTableEntry;
#define TABLE_ENTRY_SIZE (sizeof(struct _TTableEntry))
#define TTABLE_ENTRY_SIZE (sizeof(TTableEntry))
typedef struct _TTable
{
TTableEntry table[MAX_TABLE_SIZE];
int count;
} *TTable;
Table_Init(TTable _table)
{
memset(_table->table,0,TTABLE_ENTRY_SIZE*MAX_TABLE_SIZE);
_table->count = 0;
}
Table_LinearSearch(TTable _table, char * ipAddr)
{
int iIndexPos=-1;
for (int iLoop=0; iLoop<_table->count; iLoop++)
{
if (strcmp(ipAddr,_table->table[iLoop]->ipAddress)==0)
{
iIndexPos = iLoop;
break;
}
}
return(iIndexPos);
}
Table_IndexerGetIndexAt(TTable _table, int iIndexPos,TTableEntry tableEntry)
{
if ((iIndexPos>=0) && (iIndexPos<_table->count))
{
strcpy(tableEntry->ipAddress,_table->table[iIndexPos]->ipAddress);
tableEntry->count = _table->table[iIndexPos]->count;
}
else
{
memset(tableEntry->ipAddress,0,MAX_STR_LEN);
tableEntry->count=0;
}
}
Table_IncrCount(TTable _table, int iIndexPos)
{
if ((iIndexPos>=0) && (iIndexPos<_table->count))
{
(_table->table[iIndexPos]->count)++;
//cout << " updated frequency at index " << iIndexPos << " to " << _table->table[iIndexPos]->count << endl;
}
}
Table_InsertAddNew( TTable _table, char * ipAddr)
{
int iReturn=0;
if (_table->count<MAX_TABLE_SIZE)
{
int iSearchReturn = Table_LinearSearch(_table, ipAddr);
if (iSearchReturn>-1)
{
Table_IncrCount(_table,iSearchReturn);
}
else
{
_table->table[_table->count]= (TTableEntry)malloc(TABLE_ENTRY_SIZE);
// cout << "created new table entry" << endl;
strcpy(_table->table[_table->count]->ipAddress,ipAddr);
//cout << "copied ip address " << endl;
_table->table[_table->count]->count=1;
// cout << "set frequence to 1 " << endl;
_table->count++;
// cout << "updated table counter" << endl;
}
}
else
{
iReturn=-1;
}
return(iReturn);
}
Input( TTable _table)
{
FILE * fptr = fopen("input.dat","r");
while (feof(fptr)==0)
{
char inbuff[MAX_STR_LEN];
memset(inbuff,0,MAX_STR_LEN);
fgets(inbuff,255,fptr);
//cout << "read " << inbuff << " : ";
int iInsertReturn = Table_InsertAddNew(_table,inbuff);
// cout << "Insert Return = " << iInsertReturn << endl;
}
fclose(fptr);
}
int main()
{
struct _TTable _table;
Table_Init(&_table);
Input(&_table);
for (int iLoop=0; iLoop<_table.count; iLoop++)
{
cout << _table.table[iLoop]->ipAddress << " " << _table.table[iLoop]->count << endl;
}
}