C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 9th, 2004, 07:49 AM
am0k am0k is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 3 am0k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Decompressing Arrays into a File

I need to decompress words in a compressed file into a new one. It must be a function and, pls try to level the skill level with what you see. thanks

#include<iomanip> //getline
#include<cstdlib>
#include<iostream>
#include<string> //string
#include<fstream> //ifstream and ofstream
using namespace std;
int search(ifstream& myfile, string name[], string temp);
int sort(ifstream& myfile, string name[], int freq[]);
void compress(ifstream& myfile, string name[], int freq[]);
const int MAX = 50; //Max amount of data is 50
int main()
{
string name[MAX]; //arrays for the words
int size, freq[0]; //arrays for frequency of words
string FileName, OutFileName; //Filenames
char cd; //compression or decompression check
ifstream myfile; //file command for incoming file
ofstream outfile; //file command for output file

myfile.open("input.dat");

cout << "Do you want to compress or decompress? (C/D)? ";
cin >> cd;

switch(cd)
{
case 'C': case 'c':
{
sort(myfile, name, freq);
compress(myfile, name, freq);
}
break;
case 'D': case 'd':
{
//decompress
}
break;
default:
{
cout << "Please enter C or D. " << endl;
return 1;
}
}

system("PAUSE");
return 0;
}
int search(string name[], string tmpword)
{
int ctr = 0;

for(ctr = 0; ctr < MAX; ctr++)
{
if(name[ctr] == tmpword)
{
return ctr;
}
}
return -1;


}
int sort(ifstream& myfile, string name[], int freq[])
{
int product, temp;
string tmpword, OutFileName, FileName;
ofstream outfile, outfile2;
int N = -1, F = -1, ctr = 0, size;

cout << "What's the name of the full sized file? ";
cin >> OutFileName;
outfile.open(OutFileName.c_str());

cout << OutFileName <<".cmp " << "created." << endl;

while(myfile && N < MAX)
{
getline(myfile, tmpword);
product = search(name, tmpword);

if(product != -1)
{
freq[product]++;
}
else
{
N++;
F++;
name[N] = tmpword;
freq[F] = 1;
}
size = N + 1;
}

outfile << size << endl;
for(temp = 0; temp < (N + 1); temp++)
{
outfile << temp << ". " << name[temp] << " frequency " << freq[temp] << endl;
}
myfile.clear();
myfile.close();
outfile.close();
}
void compress(ifstream& myfile, string name[], int freq[])
{
int product, temp;
string tmpword, OutFileName, FileName;
ofstream outfile;
ofstream outfile2;
int N = -1, F = -1, ctr = 0, size;

myfile.open("input.dat");
outfile2.open("xylz.cmp");


while (myfile && N < MAX )
{
getline(myfile, tmpword);
product = search(name, tmpword);

if(product != -1) // Word Found, Update Frequency
{
outfile2 << product << " ";
}
else
{
N++;
name[N] = tmpword;
F++;
freq[F] = 1;
}
}
myfile.close();
myfile.clear();
}

Reply With Quote
  #2  
Old December 9th, 2004, 08:11 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
You can't just come here and dump your assignment code in the hopes that someone will do it for you.

If you have specific questions about something then ask,

-KM-

Reply With Quote
  #3  
Old December 9th, 2004, 08:30 AM
am0k am0k is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 3 am0k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I wasn't expecting somon to do the assignment, i was hoping somone would simply give me some insight on the decompression part of my problem, i already have the compression, arrays assigned, and func calls. Just needed a little hel on my decompression function.

Reply With Quote
  #4  
Old December 9th, 2004, 10:41 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
The decompression will depend on how it was compressed to start with. Perhaps if you provide us with some details.

-KM-

Reply With Quote
  #5  
Old December 9th, 2004, 06:45 PM
am0k am0k is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 3 am0k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ok, thnks for any insight. I have a word list, no whitespace and only one word per line. I took the words and placed them into a word list of repeating words with their frequencies, which functions perfectly. My compression file is to display how many words there are, they only count once if they are repeated, each word once, then a row of numbers displaying each line of the file and the data it holds. Then i should be able to create a deecompressed file of the compressed file that will seperate the words from their table into the original file.

Here is an example output:



Example Interaction with the user: Do you want to compress or decompress? (C/D) CWhat's the name of the full-size file? xyz1xyz1.cmp created

Example Interaction with the user: Do you want to compress or decompress? (C/D) DWhat's the name of the compressed file? xyz1.cmpxyz1.cmp.decmp created

Input file theumbrellathecatthecowtheblackcat

The word list (numbers show position in the list) 0. the frequency 4 1. umbrella frequency 1 2. cat frequency 2 3. cow frequency 1 4. black frequency 1

The compressed (output) file 5theumbrellacatcowblack0 1 0 2 0 3 0 4 25thecatumbrellacowblack0 1 0 2 0 3 0 4 1 0 4 2

Decompressed Output file: thecattheumbrellathecowtheblackcattheblack umbrella//I'm also having some trouble getting the words to show up in my compressed file...sometimes it will work when i put a for loop in there, but its rare.-am0k

Reply With Quote
  #6  
Old December 10th, 2004, 06:12 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
There's a fundamental flaw in what you are trying to do. Once you have compressed the information down into the number of occurances there is no way you will be able to get the ordering back. The best you will be able to do when decompressing is list the words out the number of times they occured but not in the same order.

-KM-

Reply With Quote
  #7  
Old December 10th, 2004, 10:10 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
Perhaps using something similar to indexes?

For exmaple...
Code:
If your wordlist is:
     cat
     dog
     rabbit
     cat
     cat
     dog
     animal

Set up an index like:
     0cat
     1dog
     2rabbit
     3animal

Then the wordlist could become:
     0120013
A flaw could be that the words can't contain numbers... but you've already define a flaw where the words can't contain spaces, so perhaps that's not a problem for you.

You also might benefit by using binary... in my example, what happens on the tenth word?

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Decompressing Arrays into a File


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT