Hi,

I would like to get some suggestions on how to change this program from array to vectors. This program reads an ASCII input file and outputs the character frequency into a text file. I got it to work with the arrays, but I can't figure out where to do the changes so that an vector can accomplish the same thing.

Thank you.

#include <iostream>
#include <fstream>
#include <cctype>

using namespace std;

	//declare void functions
void initialize (int& lc, int set[]);
void readText(ifstream& intext,ofstream& outtext,  char& ch, int set[]);
void countCh (char ch, int set[]);
void totalCh(ofstream& outtext, int lc, int set[]);


int main ()
{
		//declare variables
	int lineCounter;			//variable to sotre the line count
	int asciiCount;				//array to store letter count
	char ch;					//varaible to store a character
	ifstream infile;			//input file stream variable
	ofstream outfile;			//output file stream variable

		// check for file
	infile.open ("textIn.txt");

	if (!infile)
	{
		cout << "Cannot open the input file." << endl;
		
		return 1;
	}

	outfile.open("textOut.txt");


		//call functions
	initialize (lineCounter, asciiCount);
	infile.get(ch);
	readText(infile, outfile,  ch, asciiCount);
    totalCh (outfile, lineCounter, asciiCount);
	cout << endl;

	infile.close();
	outfile.close();

	return 0;
}

void initialize (int& lc, int set[])
{
	int j;
	lc = 0;

	for (j = 0; j < 127; j++)
		set [j] = 0;
}

void readText(ifstream& intext,ofstream& outtext, char& ch , int set[])
{
    while (intext)					 //process the text
	{
       	countCh (ch, set);			 //call function count character

        intext.get(ch);				 //read the next character
    }
    outtext << ch;					 //output the newline character
} 
void countCh (char ch, int set[])
{
	int index;

	index = static_cast<int>(ch) - static_cast<int>('A');

	if (0 <= index && index < 127)
		set[index]++;
}

void totalCh(ofstream& outtext, int lc, int set[])
{
	int index;

	outtext << endl;
	outtext << "The number of characters in this file are: " << endl;
	outtext << endl;

	for (index = 0; index < 127; index++)
		outtext << static_cast<char>(index + static_cast<int> ('A')) << " count = " <<set[index] << endl;
}

Recommended Answers

All 4 Replies

Quick question, does it have to be a vector? Could it be a map?

Quick question, does it have to be a vector? Could it be a map?

yea, it has to be a vector. I am taking a data structure class, and I have to do the same program but with different data structures. I got the array part, but I don't know how to change it to a vector to get all the ascii characters.

>> I got it to work with the arrays, but I can't figure out where to do the changes so that an vector can accomplish the same thing.


?????????

This doesn't compile. In particular, lines 8, 18 and 37 don't match:

The initialize function expects an array, but you give it an integer(yet the comment describes it as an array.

int asciiCount;	//array to store letter count

Presumably this is supposed to be?

int asciiCount[128];	//array to store letter count

Anyway, you can make it a vector pretty easily:

vector <int> asciiCount(128, 0);

Then you can treat it just like an array and use the [] operator to access elements.

>> I got it to work with the arrays, but I can't figure out where to do the changes so that an vector can accomplish the same thing.


?????????

This doesn't compile. In particular, lines 8, 18 and 37 don't match:

The initialize function expects an array, but you give it an integer(yet the comment describes it as an array.

int asciiCount;	//array to store letter count

Presumably this is supposed to be?

int asciiCount[128];	//array to store letter count

Anyway, you can make it a vector pretty easily:

vector <int> asciiCount(128, 0);

Then you can treat it just like an array and use the [] operator to access elements.

I got it now. Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.