Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7853435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:41:31+00:00 2026-06-02T19:41:31+00:00

How can I pass an array, declared in main method by reference as a

  • 0

How can I pass an array, declared in main method by reference as a parameter to a function? Also the compiler wants dimentions, but when i give them with variables, the compiler gives errors of invalid integer dimensions of the array, here’s the code:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int scanV(int Row, int value, int M, int (&tableValues)[])
{
for (int i = 0; i <= M; ++i)
{
    if (tableValues[Row - 1][i] == 1)
    {
        if (i + value <= M)
        {
            tableValues[Row][i+value] == 1;
        }
        if (i - value >= 0)
        {
            tableValues[Row][i-value] = 1;
        }
    }
}
}

int main()
{
int C, B, M;
cin>>C;
int integers[C];

for (int i = 1; i < C; ++i)
{
    cin>>integers[i];
}
cin>>B;
cin>>M;
integers[0] = B;
int tableValues[C][M + 1];
tableValues[0][B] = 1;
for (int i = 1; i < C; ++i)
{
    scanV(i, integers[i], M, tableValues);
}

return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:41:33+00:00Added an answer on June 2, 2026 at 7:41 pm

    One simple solution is to use vectors. Consider this simple example:

    #include <iostream>
    #include <vector>
    
    void f (std::vector<std::vector<int> > &v)
    {
        for (int i = 0; i < 3; ++i)
            for (int j = 0; j < 3; ++j)
                v[i][j] = i * 3 + j;
    }
    
    int main()
    {
        std::vector<std::vector<int> > v (3, std::vector<int> (3, 0));
        f (v);
    
        for (int i = 0; i < 3; ++i)
            for (int j = 0; j < 3; ++j)
                std::cout << v[i][j] << ' ';
    }
    

    In main, a 2D vector of ints (3×3) is created. The constructor shows 3 elements, all initialized with a vector of ints, which are in turn created with 3 elements initialized to 0.

    Then, the vector is passed by reference to the function f, which assigns increasing values. When the vector is printed in main, it shows:

    0 1 2 3 4 5 6 7 8
    

    As you can see, their use is very similar to normal arrays, but they are actually contained, and provide easy access to a new level of programming using the STL.

    In C++11, their use becomes even more familiar. You can assign vectors as follows:

    std::vector<int> v0 = {2, 5};
    std::vector<std::vector<int> > v1 { {1,2,3} , {4,5,6} , {7,8,9} };
    

    Note that for vectors of multiple dimensions it’s a good idea to encapsulate it in a matrix class of some sort with an underlying 1D vector type instead.

    Edit:

    Here’s an example of initializing a 1D and 2D vector to specified elements. As seen above, this is easy in C++11, but if you have an array already, it’s still pretty quick.

    int a [5] = {1,2,3,4,5}; //normal
    std::vector<int> v1 (a, a +5); //create using beginning and ending addresses of a
    
    int b[3][3] = { {1,2,3} , {4,5,6} , {7,8,9} }; //normal
    std::vector<std::vector<int> > v2; //empty vector
    
    for (int i = 0; i < 3; ++i) //3 in first dimension
        v2.push_back (std::vector<int> (b [i], b [i] + 3)); //push a vector with the appropriate 3 elements of b onto the back of v2
    

    For going through one element at a time, you can do this:

    std::vector<std::vector<int> > v (3, std::vector<int> (3));
    
    for (int i = 0; i < v.size(); ++i) //v.size() == 3
        for (int j = 0; j < v [i].size(); ++j)
            adjustElement (v [i][j]); //replace with what you need
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I pass an array of structs by reference in C? As an
From a method, I can pass a struct which contains an array of integers,
Can i pass the entire POST array into a function and handle it within
How can I pass a parameter to a jquery iterator function (or whatever they're
Can I pass an array to printf directly: char text[1024] = text; printf(%s, text);
I have an array which contains around 50-200 hyperlinks. How can I pass this
In my Delphi development, I want to pass an array of const(that can contains
You can pass special strings into jQuery's Datepicker class setDate() method like +7 which
I know you can pass arguments through the RunWorkerAsync function call when you first
I know that I can pass a string as the second parameter to the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.