Not limiting the size of an array with a base struct?

Hello all. I am taking a beginning C++ class and am having problems with a section in my assignemnt. I thought with arrays, you had to assign them a size. From reading the assignment requirements below, the instructor is saying that we are not allow to limit the size. Am I reading the requirements section correcly, or, is there a way create an array without limiting its size?? Would any of you be able to point me in the right direction? Below are the requirents as well as the required output from the program.


Assignment requirements:
Let me say this another way in case this makes more sense. You will need to declare a struct with two fields, an int and a char. (This should be declared above main, so that it is global; that is, so it can be used from anywhere in your program.) Then you need to declare an array (not global!) whose elements are those structs. The int in each struct will represent the number of times that the char in the same struct has occurred in the input. This means the count of each int should start at 0 and each time you see a letter that matches the char field, you increment the int field.

Don't forget that limiting the length of the input is prohibited. If you understand the above paragraph you'll understand why it is not necessary to limit the length of the input.

Assingment Output:

Enter a sequence of characters (end with '.'): do be Do bo. xyz

Letter: Number of Occurrences
o 3
d 2
b 2
e 1
The number of different characters you can read is limited so you can read the input of any size even with a fixed length array.
You can have variable-sized arrays in C++ but you don't need them for this.
Last edited on
In C or C++ as far as I know no array can be defined without a specific size. But I could be wrong.

However, there is a simple solution to your problem. Instead of using arrays you might want to use pointers to store unspecified amount of data. You can declare a pointer of char type in your struct and increment the count of the int variable as data gets stored in the other variable.

Here is one example which ran perfectly when I compiled it. It does what you want to do, it reads a line from the input buffer till the full stop '.' character is encountered and then displays what the user has entered. Along with the length of the string.

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
#include <cstdio.h>
#include <cstdlib.h>
#include <conio.h>

typedef struct
{
	int i;
	char *ch;
} test_t;

void pause(void)
{
	printf("\nPress any key to continue...");
	fflush(stdin);
	_getch();
	return;
}

int main()
{
	test_t test = {0};

	atexit(pause); /* Pause :) */

	test.ch = (char *)malloc(sizeof(char));

	printf("Enter a line: ");
	while((*(test.ch + test.i++) = _getche()) != '.') /* Get the input from the user */
	{
		realloc(test.ch, (test.i+1)*sizeof(char)); /* Allocate one extra space for the next character*/
		if(test.ch == 0)
		{
			printf("Error reallocating memory\n"); /* Print error message upon realloc() failure */
			exit(EXIT_FAILURE);
		}
	}

	*(test.ch + test.i) = '\0'; /* Terminate the string with a NULL character */

	printf("\nYou entered: %s which is %d characters long\n", test.ch, test.i); /* Print what the user entered */

        free(test.ch) /* Free allocated memory */

	return 0;
}


Hope that helps.

[EDIT]
OMG! My first post!
[\EDIT]
Last edited on
You can resize arrays: http://www.cplusplus.com/doc/tutorial/dynamic/ or use resizable containers http://www.cplusplus.com/reference/stl/
That code is C, not C++
Bazzy, that code is perfectly valid in C++ also.

But yes I didn't know we could resize arrays. I'm new to C and I have to learn a lot of things... But I felt that I could answer to this thread correctly.

The code I posted will work in C++. (at least it does on my C++ compiler). I prefer printf than using cout; don't ask why.

EDIT: Do you mean I can store 7 values in an array originally declared to hold 5? I don't know how to do that without using pointers and the first link you posted applies to pointers only - as far as I know - and not with arrays.

Can you please post an example which resizes an array? I'm curious as to how you do that without using pointers.
Last edited on
that code is perfectly valid in C++ also.
You are using C headers, they aren't standard in C++ - you should have <cstdio> -
Most C++ compilers are also C compilers so standard libraries provide both versions

Do you mean I can store 7 values in an array originally declared to hold 5? I don't know how to do that without using pointers and the first link you posted applies to pointers only - as far as I know - and not with arrays.
An array is an unmovable pointer to the first element. You can create an array on the heap of any size ( you'll need to copy it if you want to enlarge it )

Can you please post an example which resizes an array? I'm curious as to how you do that without using pointers.
You can do this with standard containers:
1
2
3
4
#include <deque>
//...
deque<int> array(5); // size = 5
array.resize(7);
Oh, I didn't know :O

Thanks, Bazzy. That is interesting... I'm just a beginner and I didn't even know a header file named <deque> existed.

I was trying to help someone but I got helped instead. Thanks again I'll do a bit of research on that later.... I'm too lazy to do that atm.

But what I posted also works :P (I'll change the stdlib to cstdlib). The reason I've been using "stdio.h" because that is what is there in the Text book I have. (now that I think about it, there is no mention of "cstdio.h" in there; all the examples have include<stdio.h>)
Last edited on
stdio.h is the C header
cstdio (no .h) is the C++ equivalent

Here are described all the C++ standard containers: http://www.cplusplus.com/reference/stl/ ( they don't work with C )
Thanks Bazzy and all for your input.

The number of different characters you can read is limited so you can read the input of any size even with a fixed length array.
You can have variable-sized arrays in C++ but you don't need them for this.

After reading his post, it makes sense now. My fixed array size will be 26.


You may want another item to count non-alphabetical characters
Topic archived. No new replies allowed.