#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
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.
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 !
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.
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.
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??
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.
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.