An array of chars

Pages: 123
closed account (23q2T05o)
sooo I started like this:

int size=0;
size=strlen(array)+1;
char** newArray = new(nothrow)char* [size];
if (!newArray) {
cout << "There is not enough memory!" << endl;
}

use Binary Search Tree or an AVL
Your read_word_from_the_array is not doing any parsing to figure out the words in the array. The array is an array of individual characters.

If you have a string like "Once upon a time", how do you just print each word individually on its own line? Attempt to do that.

strcpy(temp, array[j - 1]);This doesn't make any sense. temp is an array, but array[j - 1] is an individual character.
http://www.cplusplus.com/reference/cstring/strcpy/
You're not sorting words, you're just sorting characters (and incorrectly).
Again, focus on one thing at a time. Don't focus on sorting the words yet. Just focus on figuring out what the individual words are.
Last edited on
no, not char **, just char *.

look at this:
char c[] = "abc 123";
c[3] = 0; //replace the space with a end of string zero. you need to find the spaces
//in your code, i knew where it was and hard coded it here for a quick example.
char *cp[2]; //some char*s (these be words)
cp[0] = c; //first word -- you can get more fancy or assume its first location of array.
cp[1] = &c[4]; //second word, using the space/location you found mentioned above,
//again I hard coded the location because short example.
cout << cp[0] << '|' << cp[1]; //look what happens. should be abc|123 -- each word is just a pointer back into the original data (with the zeros inserted).
when you sort it, you sort these pointers, not the data: you swap pointers, not data, etc. this is critical, swapping data attempts will make an unholy mess.



again, you do not need any char**. this will make it harder to code. I am giving you the brute force, direct approach and it will also be efficient; you only have to crawl over the original data one time (to add zeros and count words), followed by one sort of only pointers, and one more loop to count dupes. Granted its a hack, but without any tools, the only solutions will be hacks of one degree or another, so may as well go all in.
one more time, as an algorithm --
- read data
- replace all 10,13, and 32 with 0 in the ascii letters (end of lines and spaces)
- save a pointer (in an array) to the start of each word (its the letter after the 10/13/32s you found) and count the words as you do this (doubles as word count and how many pointers to words you have used in your array. array should be able to handle every other letter eg "a a a a a ..... #" so 1/2 the size of the MB array I think is max?
- make your own strcmp
- sort the pointers by the pointed to text value using strcmp you wrote
- count the duplicates in the sorted array by array[n] == array[n+1] test

nothing fancy. 5 or 6 loops, 2 arrays, 2 functions (strcmp and sort). all in 1-d arrays, not 2-d. Only major library call was the letter count from cin.
Last edited on
closed account (23q2T05o)
so that is what I did but it is not working okay and also MAX_SIZE should be =1MB but when I enter the number my visual studio doent work and want to move things to the heap :/

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
using namespace std;

const int MAX_SIZE = 1048;
const int MAX_SIZE_WORD = 65;

void sortWordArray(char** arr, int size)
{
	for (int i = 0; i < size; i++)
	{
		int minIndex = i;

		for (int j = i + 1; j < size; j++)
		{
			if (strcmp(arr[j], arr[minIndex]) < 0)
			{
				minIndex = j;
			}
		}
		if (i != minIndex)
		{
			std::swap(arr[i], arr[minIndex]);
		}
	}
}

void clearWordArray(char** arr, int i)
{
	while (i > 0) {
		--i;
		delete[] arr[i];
	}
	delete[] arr;
}

int main() {
	char array [MAX_SIZE];

	cout << "Please enter the text and in the end place a #: " << endl;
	cin.getline(array, MAX_SIZE);

	cout << "The text that you entered like: " << endl;
	cout << array << endl;

	int counter = 0;
	int size = 0;
	size = strlen(array) + 1;

	for (size_t i = 0; i < size; i++)
	{
		if (array[i] == ' ' || array[i]=='#') 
		{
			counter++;
		}
	}

	cout << "The number of all words is: " << counter << endl;
	

	char** newArray = new(nothrow)char* [size];
	if (!newArray) {
		cout << "There is not enough memory!" << endl;
		return 1;
	}
	for (size_t i = 0; i < size; i++)
	{
		strcpy(newArray[i], array);
	}
	

	//sorting the array

	sortWordArray(newArray, size);

	cout << "The sorted array looks like this: " << endl;
	for (size_t i = 0; i < size; i++)
	{
		cout << newArray[i] << endl;
	}

	//clean the dynamic memory
	clearWordArray(newArray, size);

	return 0;
}
there is a setting on your project in visual studio to increase the stack size. google how to change that value, or try to find it... see if you can make it big enough.
if not, use a pointer.
instead of an array you can do this. absolutely nothing else changes in what I said.

char * big = new char(MB);
...
delete[] big; //before main ends.

1048 is a kilobyte. you want the number I gave you for a MB.
you don't need a max word size. Nothing I said requires this (if you have a use for it, fine, but I don't see one?).
Last edited on
Example of parsing a string to print each word:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cstring>
#include <iostream>

using namespace std;

int main ()
{
    char input[] = "Once upon a time.";
    const char delims[] = " .,";

    // https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm
   
    // get the first token
    char* token = strtok(input, delims);
   
    // walk through other tokens
    while(token != nullptr) {
        cout << token << '\n';
        token = strtok(nullptr, delims);
    }
}


jonnin means:
1
2
3
char * big = new char[MB];
...
delete[] big; //before main ends. 
Last edited on
yes, thanks.

... and he can't use strtok, I think that would not fly since he can't use string.h (is that in string.h? I forget? even so, I suspect its banned?) .... did you think I was suggesting the zero injection hax to be sadistic? :)

if you want a function to modify a pointer, pass a pointer reference. ** works but is crude.
void sortWordArray(char*&arr, int size)
Last edited on
izlez meant the std::string class.
I assume he or she can use anything in string.h or cstring, but yeah I don't know for sure. strtok just seems like a sane choice here since it's one of those "C++ with out the ++" classes.
Last edited on
closed account (23q2T05o)
i cant use strtok
closed account (23q2T05o)
okay so everywhere i have written ** i have to change it to * and where it is * i have to remove the *?
and I told you how to do it without. but I can't tell that you are following my design (which is fine) but if you are going to do something else, its on you to lay out the steps you need and to design it.

I am not sure what the pointer question was there. either you need to modify the pointer inside the function, in which case it should be a reference, or not, in which case it can be single *. the routines that deal with the array of pointers may actually be ** depending on how you coded what I said, so don't blindly replace everything, just make sure it all fits together into what you are building.
Last edited on
closed account (23q2T05o)
yes i am trying something by my own and there is some problem in my code and idk what to do to so it can sort the stuff correctly, its really confusing to follow someone's steps because programming is somethings new to me
closed account (23q2T05o)
please i just need someon to tell me why it doenst sort the things correctly! i am really confused
I get that. so before you code, lay out the steps you want for your design. You don't have to use mine; I totally get that what I said is a little fast and loose and not easy for a beginner (its the easiest to code, but not trivial to understand).

it will be easier to code if you write down what you want to do and how you will get there. Don't have to have details, just like my list for the approach, make your list for your approach in words. It will help you a ton, then you can start to code the pieces you need.

You will get respect and help here for trying to do it on your own. The help will go better if you provide us with your idea/steps / approach so we can understand how you are going about it.
Last edited on
closed account (23q2T05o)
i did that with the steps and the things that i have written are from the steps but there is something that doesnt do it right and i just need help

1
2
3
4
5
	for (size_t i = 0; i < size; i++)
	{
		strcpy(newArray[i], array);
	}
	

(1) This is just copying your entire array size times. You're still not parsing array to separate out each word. That's what strtok does.
(2) You didn't initialize newArray[i]. It's a junk pointer.

You can write the logic similar to strtok yourself using a loop and some if statements.

Here's example example without using strtok. I didn't test edge cases, there is much room for improvement.
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
// Example program
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char input[] = "Once upon\n a time.";
    char* words[100] {};
    
    int curr_word_index = 0;
    char* last_word = &input[0]; // TODO: account for when input doesn't start with a word

    int n = strlen(input);
    
    int num_words = 0;
    for (int i = 0; i < n; i++)
    {
        if (input[i] == ' ' || input[i] == ',' || input[i] == '.')
        {
            // TODO: Don't point to empty words (e.g. two spaces right next to each other)

            input[i] = '\0';
            words[curr_word_index] = last_word;
            curr_word_index++;
            
            last_word = input + i + 1;
            num_words++;
        }
    }

    for (int i = 0; i < num_words; i++)
    {
        cout << words[i] << '\n';
    }
}


What you end up with is an array of char* that you can play around with.

but there is something that doesnt do it right
This tells us nothing.
Last edited on
closed account (23q2T05o)
please I just want my code to work, all of you gave me enough ideas, thanks for that but i am just trying to work on my code and my mistakes
i added a new line please just help me to correct my code pleaseeeeeeee
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
using namespace std;

const int MAX_SIZE = 1048;
const int MAX_SIZE_WORD = 65;

void sortWordArray(char** arr, int size)
{
	for (int i = 0; i < size; i++)
	{
		int minIndex = i;

		for (int j = i + 1; j < size; j++)
		{
			if (strcmp(arr[j], arr[minIndex]) < 0)
			{
				minIndex = j;
			}
		}
		if (i != minIndex)
		{
			std::swap(arr[i], arr[minIndex]);
		}
	}
}

void clearWordArray(char** arr, int i)
{
	while (i > 0) {
		--i;
		delete[] arr[i];
	}
	delete[] arr;
}

int main() {
	char array [MAX_SIZE];

	cout << "Please enter the text and in the end place a #: " << endl;
	cin.getline(array, MAX_SIZE);

	cout << "The text that you entered like: " << endl;
	cout << array << endl;

	int counter = 0;
	int size = 0;
	size = strlen(array) + 1;

	for (size_t i = 0; i < size; i++)
	{
		if (array[i] == ' ' || array[i]=='#' || array[i]=='/0') 
		{
			counter++;
		}
	}

	cout << "The number of all words is: " << counter << endl;
	

	char** newArray = new(nothrow)char* [size];
	if (!newArray) {
		cout << "There is not enough memory!" << endl;
		return 1;
	}

	for (size_t i = 0; i < size; i++)
	{
		newArray[i] = new char[size];

		if (!newArray)
		{
			cout << "There is no memory!" << endl;
			clearWordArray(newArray, i);
			return 1;
		}
		strcpy(newArray[i], array);
	}
	

	//sorting the array

	sortWordArray(newArray, size);

	cout << "The sorted array looks like this: " << endl;
	cout << newArray;

	//clean the dynamic memory
	clearWordArray(newArray, size);

	return 0;
}
closed account (23q2T05o)
so everything until the //sorting the arry works fine
but then the otput is not sorted, i think that working with ** is messing it up but idk what to change so it works fine
1. Add #include <cstring> to the top of your code. This is where strlen and strcpy are defined.
2. It's '\0' not '/0'
3.
1
2
	cout << "The sorted array looks like this: " << endl;
	cout << newArray;

This is printing a char**, which prints an address.
You need to loop through newArray and print each newArray[i].

1
2
3
4
5
	cout << "The sorted array looks like this: " << endl;
    for (int i = 0; i < size; i++)
    {
        cout << newArray[i] << '\n';
    }


4. You forgot to do (nothrow) on line 68. Eventually this may matter... but one thing at a time.
Last edited on
Pages: 123