Reading from text file and storing data into 2D array

I want to create a program to load all unique words of a specific file in an array
and display them and also a program to load and display all words of a specific file with their occurrences as 2D array
Kindly help me in the creation of this program which logic will be used in this program
Last edited on
I want to create a program to load all unique words of a specific file in an array
and display them and also a program to load and display all words of a specific file with their occurrences as 2D array
Kindly help me in the creation of this program which logic will be used in this program


What part of this are you having difficulty with? What have you attempted so far?
Can you read text from a file? Do you know how to use arrays? What is meant by a word? Do you exclude punctuation etc etc. Is "its" the same as "it's"...

Do you have to use array or could you use std::map which would probably be more suitable?
Last edited on
I could not understand one thing how to store unique words in an array and all words of specific file in 2D array I can't understand how to create a logic for that The requirement is to use array not maps
This program is finding unique words through using maps but I want to do same work by using array
// C++ program to print unique words in a string
#include <bits/stdc++.h>
using namespace std;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Prints unique words in a file
void printUniquedWords(char filename[])
{
	// Open a file stream
	fstream fs(filename);

	// Create a map to store count of all words
	map<string, int> mp;

	// Keep reading words while there are words to read
	string word;
	while (fs >> word)
	{
		// If this is first occurrence of word
		if (!mp.count(word))
			mp.insert(make_pair(word, 1));
		else
			mp[word]++;
	}

	fs.close();

	// Traverse map and print all words whose count
	//is 1
	for (map<string, int> :: iterator p = mp.begin();
		p != mp.end(); p++)
	{
		if (p->second == 1)
			cout << p->first << endl;
	}
}

// Driver program
int main()
{
	// Create a file for testing and write something in it
	char filename[] = "E:/C++ NUML/New folder/Doc1.txt";
	//ofstream fs(filename, ios::trunc);
//	fs << "geeks for geeks quiz code geeks practice for qa";
//	fs.close();

	printUniquedWords(filename);
	return 0;
}
Last edited on
I want to do same work by using array

Why? You would do a lot LESS work if you used a map.


as 2D array

Well, you can't. A word is a string and a frequency is an integer, whereas an array - 1D, 2D or whatever - has elements of a single datatype.
You could have a 1D array of pair<string,int> ... but then, you might as well have a map.


Are you sure that "2D array" isn't simply referring to how you OUTPUT your data? i.e. as 2 columns of a table?
Construct a program to load all unique words of a specific file in an array
and display them
Construct a program to load all unique words of a specific file in an array
and display them
Design and develop a search engine that will take a word from user as
input and will suggest top 5 most relevant documents in specified system
directory. For every search operation:
 Array will be a two dimensional array that will maintain filename along with
the frequency/occurrences of provided word in each document.
After loading your system should be able to display the top 5 five most
relevant documents on the basis of word’s frequency/occurrence in
document
Because it is requirement of my school assignment
Last edited on
Yeah, but that says (before you went back and edited it):
"array" - not 2-d array
... and it doesn't say what type of array you have to use
... and it doesn't say count the occurrences
... and it doesn't say that you can't use a std::map (or std::set) to establish a nice sorted collection of unique ones.

If ALL you want to do is find the unique words then put them first in a std::set.
If you also want to count the occurrences of unique words then you can use a std::map.

Either way, put them in a uniqueness-enforcing container first, then copy them to whatever array you want afterwards.
Last edited on
Construct a program to load and display all words of a specific file with
their occurrences as 2D array
The second part is this
I have never studied that std::map So i don't know nothing about it That's why I can't use it and also my tutor has never taught that std::map method

Array will be a two dimensional array that will maintain filename along with
the frequency/occurrences of provided word in each document.

After loading your system should be able to display the top 5 five most
relevant documents on the basis of word’s frequency/occurrence in
document
Last edited on
Well for starters, this is the 'modern c++' map version:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// C++ program to print unique words in a string
#include <fstream>
#include <iostream>
#include <string>
#include <map>

// Prints unique words in a file
void printUniquedWords(const std::string& filename) {
	std::fstream fs(filename);

	if (!fs) {
		std::cout << "Cannot open file\n";
		return;
	}

	std::map<std::string, size_t> mp;

	for (std::string word; fs >> word; ++mp[word]);

	// Traverse map and print all words whose count is 1
	for (const auto& [wd, cnt] : mp)
		if (cnt == 1)
			std::cout << wd << '\n';
}

// Driver program
int main() {
	const std::string filename {"Doc1.txt"};

	printUniquedWords(filename);
}


And using an array instead of a map:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// C++ program to print unique words in a string
#include <fstream>
#include <iostream>
#include <string>

// Prints unique words in a file
void printUniquedWords(const std::string& filename) {
	constexpr size_t maxWrds {100};

	struct Hist {
		std::string wrd;
		size_t cnt {1};
	};

	std::fstream fs(filename);

	if (!fs) {
		std::cout << "Cannot open file\n";
		return;
	}

	Hist uniq[maxWrds];
	size_t got {};

	for (std::string word; got < maxWrds && (fs >> word); ) {
		bool fnd {};

		for (size_t i = 0; i < got; ++i)
			if (uniq[i].wrd == word) {
				++uniq[i].cnt;
				fnd = true;
				break;
			}

		if (!fnd)
			uniq[got++].wrd = word;
	}

	// Traverse array and print all words whose count is 1
	for (size_t i = 0; i < got; ++i)
		if (uniq[i].cnt == 1)
			std::cout << uniq[i].wrd << '\n';
}

// Driver program
int main() {
	const std::string filename {"Doc1.txt"};

	printUniquedWords(filename);
}

there is error constexpr was not declared in this scope
'maxWrds' was not declared in this scope
Construct a program to load and display all words of a specific file with
their occurrences as 2D array

Design and develop a search engine that will take a word from user as
input and will suggest top 5 most relevant documents in specified system
directory. For every search operation:
1. Array will be a two dimensional array that will maintain filename along with
the frequency/occurrences of provided word in each document

2.After loading your system should be able to display the top 5 five most
relevant documents on the basis of word’s frequency/occurrence in
documents


Can you tell me what logic will be used for these programs?
Last edited on
there is error constexpr was not declared in this scope
'maxWrds' was not declared in this scope


What compiler/c++ version are you using? constexpr requires at least c++11
Now it is running fine bcz I ran it on VS 2019 But can you tell me in detail that how this code is working bcz I can't understand the execution of program
What line(s) don't you understand?

It opens the file and prints a message if cannot open

It iterates over the file reading each word (or until maximum different words are read).

For each word read, if it already exists in uniq, then increment the usage count. If it doesn't exists then add.

Once the file iteration has finished, then iterate over uniq and display those words with a count of 1.
this part of code
constexpr size_t maxWrds {100};

struct Hist {
std::string wrd;
size_t cnt {1};
};
this part
Hist uniq[maxWrds];
size_t got {};

for (std::string word; got < maxWrds && (fs >> word); ) {
bool fnd {};

for (size_t i = 0; i < got; ++i)
if (uniq[i].wrd == word) {
++uniq[i].cnt;
fnd = true;
break;
}

if (!fnd)
uniq[got++].wrd = word;
}
what does that mean
const std::string

bcz I have only used cout and cin
Last edited on
L8 constexpr means a constant varaible whose value is set at compile time and not as run-time https://en.cppreference.com/w/cpp/language/constexpr

L10-13 defines a struct see https://en.cppreference.com/w/c/language/struct

L22 uniq is defined as an array of type Hist with MaxWrds elements.

L25 word is defined as type std::string. If got (number of different words) is less than maxWrds and word is extracted OK from the file then execute the code in brackets.

L26 define fnd as type bool and initialise to its default value (false here)

L28 - 33 - you should be able to work out what this code does

L35/36 If word not found in array uniq, then add it at the end of the existing words.

L47 - define filename as type std::string, set it's value to "Doc1.txt" and define as const (ie it's value can't be changed once initialised).
Paul5,
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
Topic archived. No new replies allowed.