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 3 1489
* 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 "Accelerate d 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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
past 2 weeks now i give up and call for help. Please if anyone can
gide me through i will be so grateful!! I have pasted my code below
|
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 definition ========
#include "tCls.h"
|
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 RollingFileAppender("MyAppender"))
I want to move this line of code to a constructor in a wrapper class, so
that I can determine the appropriate Appender name during the object's
|
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 the way. (the xpsp2b helped).
Then I realized I could use a com port manipulation to send infothrough to it ! Of course, .net doesn't have what i need so ihad to use .
Everything worked fine on XP but all hell broke on Win98. Irealized that the...
|
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 efficient
navigating within Visual Studio 2005. Let's say your project (or
solution) has dozens of forms and hundreds or even thousands of
routines.
Two Questions:
1) BUILT-IN to Visual Studio 2005. What ideas do you have to quickly
| |
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 numbers we entered to a..that
should be added..
for eg if a = 34 the answe should be 7 that is 3+4
using % and / we can solve this problem..pls help me to get the
problems
|
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 over
25,000 files in a couple hundred directories.
I'm currently trying to collect the list using this
Try
For Each foundfile As String In
My.Computer.FileSystem.GetFiles(mySource,
|
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 solve this problems. I cant tell u how much i will be gratefull if someone can help me. Please if anyone knows how to solve certain task reply to me.Thanks to all in advance.
Tasks
1. Your are going to a restaurant where the tip is expected to be...
|
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, so making this took me hours and hours of time...
// class SudokuBoard, will be called by class Sudoku
import java.util.Scanner;
import java.io.*;
public class SudokuBoard {
private int board = new int;
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |