CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2023
    Posts
    1

    How to initialize an array whose size is initially unknown

    Hello all ,

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

    Code:
    #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

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,820

    Re: How to initialize an array whose size is initially unknown

    In c/c++ the size of an array needs to be known at compile time. However in C++ there is std::vector where the size can be set at run-time or enlarged as elements are added that will grow the container as needed. See https://cplusplus.com/reference/vector/vector/

    is it possible to add (Not + )variables next to each other
    Not directly, but if y < 10 you could do:

    Code:
    sum = x * 10 + y;
    A more general method is to use std:: ostringstream.

    Code:
    #include <iostream>
    #include <sstream>
    
    int main() {
    	const int x { 234 };
    	const int y { 5678 };
    	const auto sum { std::stoi(((std::ostringstream{}) << x << y).str()) };
    
    	std::cout << sum << '\n';
    }
    which displays:

    Code:
    2345678
    But why are you wanting to do this?
    Last edited by 2kaud; October 30th, 2023 at 06:37 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured