473,326 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

help to solve this

C++ Programming Language Assignment 1

Associative Array Class
Problem Statement
Associative arrays are a useful feature found in many languages
particularly in those that are interpretive and support dynamic
variables, such as awk, perl, APL, etc.
An associative array is very much like the much familiar array, with
the only difference that the index specified, can be of any type. (In
arrays, array index HAS TO BE integer). An associative array provides
all the standard array operations of element access and assignment.

In this assignment, you are to make available an associative
array-mechanism using C++. This is done be defining a class, called
AssocArray.
Sample Program
Use of the class is illustrated by the following code. It uses an
associative array to count the frequency of words in the input. After
reading the input, the top ten most frequent words are displayed.

#include <iostream.h>
#include "Assoc.h"

const int TOPNUM = 10;

int main()
{
AssocArray wordCount;
int i, j, added, c[TOPNUM];
char word[128], *w[TOPNUM];

while ( cin >> word ) wordCount[word]++;

for ( i = 0; i < TOPNUM; i++ ) {
c[i] = 0; w[i] = 0;
}

for ( wordCount.start(); !wordCount.last(); wordCount.next() ) {
added = 0;
for ( i = 0; (i < TOPNUM) && !added; i++ ) {
if ( wordCount.value() > c[i] ) {
for ( j = TOPNUM-1; j > i; j-- ) {
c[j] = c[j-1];
w[j] = w[j-1];
}

w[i] = wordCount.key();
c[i] = wordCount.value();
added = 1;
}
}
}

for ( i = 0; i < TOPNUM; i++ )
if ( w[i] )
cout << w[i] << " " << c[i] << endl;

return 0;
}
Specifications
Do not make assumptions about the maximum number of keys for choosing
a representation for the AssocArray. The suggested representation is
an ordered linked list of < key, value > pairs, where the key is a
string (char*) and value is an int
For the class, you are expected to code
default constructor
a constructor with an integer initializer - this should be the initial
value of any key that has been added (default value).
copy constructor
destructor - to ensure against memory leaks
assignment operator
Other methods, like start(), next(), last(), value(), as shown in the
sample have to be implemented.
Operators to be overloaded
[] - for array indexing
+ - for merging two associative arrays. Values added for the same key
value)
- - (a - b) contains those elements (key values) of 'a' that are not
there in 'b'
<< - to output the associative array
For comparisons of the keys of the associative array, you can make use
of the strcmp() function.
// append to file
Jul 22 '05 #1
3 1448
On 17 Oct 2004 03:16:29 -0700, su************@yahoo.co.in (sudeep)
wrote:

[snipped homework assignment]

You didn't tell us where you are stuck with the problem.

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #2
* sudeep:
C++ Programming Language Assignment 1
Write some code and post it along with specific questions.

Otherwise HOMEWORK is off-topic.

However, do give your C-oriented lecturer the following comments,
including this comment: C++ is not C, and a good book to learn
C++ from is "Accelerated C++".

Sample Program
Use of the class is illustrated by the following code. It uses an
associative array to count the frequency of words in the input. After
reading the input, the top ten most frequent words are displayed.

#include <iostream.h>
The <iostream.h> header file is not a standard C++ header.

Corresponding facilities are provided by the <iostream> standard header.

#include "Assoc.h"

const int TOPNUM = 10;
All uppercase should only be used for macros.

int main()
{
AssocArray wordCount;
int i, j, added, c[TOPNUM];
Style: don't declare multiple variables this way.

Style: don't declare variables outside the block where they're used.

It's not a good idea to use global variables.

If your lecturer maintains there are no global variables he/she
should perhaps consider a new profession.

Style: use appropriate data types ('added' should be a 'bool').

char word[128], *w[TOPNUM];
It's not a good idea to teach use of pointers in a programming
assignment #1.

while ( cin >> word ) wordCount[word]++;
Possible buffer overflow (I don't know and don't care to check).
Use std::string instead of array of char.

for ( i = 0; i < TOPNUM; i++ ) {
Style: use '++i', not 'i++'.

c[i] = 0; w[i] = 0;
Bug: use of uninitialized variable 'w'.

Style: 'c' should have been initialized as follows:

int c[TOPNUM] = {0};

not via a loop.
}

And so on...

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #3
sudeep wrote:
C++ Programming Language Assignment 1


http://www.parashift.com/c++-faq-lite/how-to-post.html

see 5.2

HTH,
- J.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: the_proud_family | last post by:
HELP ME PLEASE!! my email is the_proud_family@yahoo.com I can't get the ball to go up right side and then I need it to turn around and keep turning until velocity=0 I have been at it for the ...
2
by: QH Hong | last post by:
Compilation of the folowing program gives error message error C2664: 'solve' : cannot convert parameter 1 from 'double (double)' to 'double (__cdecl *)(double)' Here's the codes ==== class...
15
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new...
1
by: Tito Brezovacki via .NET 247 | last post by:
Hi. The problem is very simple to describe, but tough to solve :( I have a printer that doesn't print on win98 using the usualprocedures (print() in C#) . I could barely get it to work on xpby...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
8
by: SanjaiGandhi | last post by:
Hi ...i am new to programing....pls help to overcome this program.. The Program is..: if a = 557..using for loop or while or dowhile ..we have to get the answer for 5+5+7..that is what ever...
6
by: Kyote | last post by:
I'm trying to make, what I thought, would be a simple application. This application will help me organize files I have a lot of, on my computer(ebooks, pictures, etc..). I'm currently dealing with...
3
by: Dew | last post by:
Hello, iam new to programming, and ive been given a task in my Uni to complete certain exercises but i found it very difficulty to solve them. I was wandering if there is someone who can help me...
3
by: deanchhsw | last post by:
Hello, I'm trying to build a program that solves sudokus and prints out the result on the screen. Here's the code for the class SudokuBoard. this will later be called in a class Sudoku. I'm a newbie,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.