How to initialize an array whose size is initially unknown

Hello all ,

Note: These values[xup -yup -xright - etc ..] are constantly changing
My example ..

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
#include <iostream>



int main() {

int xup = 721 ;
int yup = 575;
int xright = 726;
int yright = 570;
int xleftdown = 731;
int yleftdown = 586;
int xlenth = xright - xup;
int ylenth = yup - yright;
int hight = xleftdown - xup ;

std::cout << "xlenth " << xlenth << " ylenth " << ylenth << " hight " << hight<< "\n";

int x,y,x1,y1;
for (int i = 1; i <= xlenth; ++i) {
x = xup + i;
y = yup -i;

std::cout << x << "," << y << "\n";


for (int j = 1; j <= hight; ++j) {

x1 = x + j;
y1 = y + j;
int add = y1+ 1;
std::cout << x1 << "," << y1 << "\n";
std::cout << x1 << "," << add << "\n";
}

}
return 0;
}



output :
722,574
723,575
723,576
724,576
etc ...

Another question, is it possible to add (Not + )variables next to each other?
like this :
int x = 5;
int y = 6;
int sum;
sum = x&y; // I know it's wrong but I want something like this
output:
56

Then I will add output to array .. Thanks
Last edited on
I'm really confused. The title of this thread is about initializing an array with unknown size, and then there is not a single array in your code example.

Next you make a statement about variables that are "constantly changing", but you do not ask a question. Then you say "Another question...". I am so confused.

Finally, you want to concatenate 2 numbers to make another number.

Do you need these as integers, or can they be strings? If strings works, then you can simply do
1
2
3
std::string x = "5";
std::string y = "6";
std::string sum = x + y;


Why you want a concatenation of 2 ints stored in a variable named "sum" is beyond me.

1
2
3
4
int x = 5;
int y = 6;
int final;  // I am not going to call it sum any more
final = (x * 10) + y;


You could also figure out how many digits are in y by using a log function and then multiply x by 10 to that power to make it more general.

Oh, there's the reference to an array! Buried at the end. I really don't know what you are trying to do.
With regular arrays use dynamic memory management. new[] and delete[].

With C++ code you'd be better off using a C++ container such as a vector. Let C++ manage the memory for you so you don't have to worry about it.

https://en.cppreference.com/w/cpp/container/vector
Thanks so much doug4 , Thanks so much George P
I'm really confused. The title of this thread is about initializing an array with unknown size, and then there is not a single array in your code example.


I want to make an array without size because I don't know the size
output :
722,574
723,575
723,576
724,576
etc ...

Finally, you want to concatenate 2 numbers to make another number.

Do you need these as integers, or can they be strings

as integers ..

Finally, you want to concatenate 2 numbers to make another number.

becuse i don't need 2nd array ..

Oh, there's the reference to an array! Buried at the end. I really don't know what you are trying to do.


All I want after adding the two numbers in a variable is add the result to a single array !

And sorry about that ..

I'm relying on you @doug4 to explain @Hawlong's last post!
@lastchance,

Why are you leaving this to me? I still have no idea what @Hawlong wants.

@Hawlong refers to "sum" and "add" while trying to concatenate digits (I think - it's very vague at this point). Somehow, that's not the most confusing part of his posts.

@Hawlong,

Wait, are you trying to create an array of pairs of numbers? Use this:

std::vector<std::pair<int, int>>

If that's not what you are trying to do, then I remain completely confused.

Last edited on
I want to make an array without size because I don't know the size


Well from the OP, you do know it's size. It's xlength * hight cout iterations displaying 4 values for each iteration. If for L7 - L15 these variables are made const (better still constexpr), then compute xlength * hight as another const int and use this value as the size of the array. If you have a struct of 4 ints (called say Data), then the type of this array will be Data.

But why not use a std::vector - either of type Data from above or std::pair<int, int> and have 2 entries for each iteration?

sum = x&y; // I know it's wrong but I want something like this


Yes - but presumably at some point either the code or someone has to interpret this 'number' as somethings. So if sum is say 1234 is x 123 and y 4 or is x 12 and y 34 or... Why do you want to do this - as without further info it doesn't seem a good idea??

You really need to provide more detailed info as to what you're trying to achieve.

PS
1
2
3
int add = y1+ 1;
std::cout << x1 << "," << y1 << "\n";
std::cout << x1 << "," << add << "\n";


This is really:

1
2
std::cout << x1 << "," << y1 << "\n";
std::cout << x1 << "," << y1 + 1 << "\n";


As the second cout is trivially obtainable from the first cout, why do you need 2 lines of output and 4 values - why not just 1 line with 2 values of output?

Again, what are you trying to achieve - for what purpose are these numbers to be used??
Last edited on
C style arrays, or similar constructs, have a fixed size at compile time, and nothing you can do will 'fix' that.
One choice seen in old code would be to allocate the array to its largest possible size (with some extra for good measure) and keep track of how many elements you used in another variable.

another way is to use pointers, which you can size at run time to fit.

but in c++ we have a dozen containers that resize for you. The c++ answer is to use a vector or similar container that sizes to fit the data.

both arrays and vectors can be initialized at run time; you can supply an array initialization for fewer elements than it has.

Its still unclear what you want, but this is the answer to the question posed in the title.
I don't know how to explain more than this anyway
this is example output :
722,574
723,575
723,576
724,576
724,577
725,577
725,578
726,578
726,579
727,579
727,580
728,580
728,581
729,581
729,582
730,582
730,583
731,583
731,584
732,584
732,585
723,573
724,574
724,575
725,575
725,576
726,576
726,577
727,577
727,578
728,578
728,579
729,579
729,580
730,580
730,581
731,581
731,582
732,582
732,583
733,583
733,584
724,572
725,573
725,574
726,574
726,575
727,575
727,576
728,576
728,577
729,577
729,578
730,578
730,579
731,579
731,580
732,580
732,581
733,581
733,582
734,582
734,583
725,571
726,572
726,573
727,573
727,574
728,574
728,575
729,575
729,576
730,576
730,577
731,577
731,578
732,578
732,579
733,579
733,580
734,580
734,581
735,581
735,582
726,570
727,571
727,572
728,572
728,573
729,573
729,574
730,574
730,575
731,575
731,576
732,576
732,577
733,577
733,578
734,578
734,579
735,579
735,580
736,580
736,581

I want to add each line inside the array

I want to add each line inside the array


That's what you needed to say in your first post. You only mentioned "output" (and I still don't know what "output" means), with vague references to an array without saying why you wanted one. We aren't mind readers (at least I'm not).

After reading your second post a few times and after I started responding to it, I was able to guess that this is what you were trying to do (maybe).

Please be more specific about your questions in the future. If you are asking about an array, don't say "here is the output". Tell us how the output relates to your question. Adding 100 extra lines of output doesn't clarify anything. Saying that you want to take the (contents of) each line of output and place it into an array does.

But to answer your question, read the previous 3 responses from me, @seeplus and @jonnin. We all suggest using a std::vector (or other standard container) containing data structure of some sort. It seems to me like std::pair<int, int> would be an appropriate data structure, but if not, you can create your own class. So instead of an array, you would have

std::vector<std::pair<int, int>>
or
std::vector<MyClass>

This will give you a whole lot of safety and capabilities, including the ability to grow the container as needed.
Last edited on
Thanks all , seeplus , jonnin , doug4
Thank you very much for your great effort.




Last edited on
Topic archived. No new replies allowed.