problem passing 2-D vector to a function

Pages: 12
why am i getting runtime error ,i spent a lot of time learning taking user input in 2-d vector ,still i am failing to do that. Somebody,please help
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
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;

void Takeinput(vector<vector<int>> &arr,int N){
     for(int i=0;i<N;i++){
         vector<int> row;
         for(int j=0 ;j<N;j++){
                int  n;
                cin>>n;
                row.push_back(n);
            }
         arr.push_back(row);
     }
    return;}
    
int Sum(vector<vector<int>> &arrA,vector<vector<int>> &arrB)
{   int sum =0;
    int sum1=0;
    int sum2=0;
    int i=0;
    while(i<arrA.size()){
        for(int j=0;j<arrA.size();j++){
            sum = sum + arrA[i][j];
            sum1= sum + arrB[j][i];
          }
         i++;
        sum2 = sum2 + sum*sum1;
        sum=0;
        sum1=0;
   }
    return sum2;
 }
 
int main(){
    int N;
    cin>>N;
    vector<vector<int>> arrA;
     vector<vector<int>> arrB;
    Takeinput(arrA,N);
    Takeinput(arrB,N);
    int Q;
    cin>>Q;
    while(Q--){
        char ch;
        int a,b,c;
        cin>>ch;
        cin>>a>>b>>c;
        if(ch=='A') arrA[a][b] = c;
        else arrB[a][b]= c;
        cout<<Sum(arrA,arrB)<<endl;
        }
        return 0;
    }
Last edited on
Line 37-38 creates two vectors containing N empty vectors.
Takeinput adds another N vectors that contains N element.
So what you end up with is two vectors containing N+N rows, where the first N rows have no columns, and the last N rows have N columns.
still after removing N from arrA and arrB as parameter it's giving error
The very best thing you can do is learn how to use your debugger, so that you can step through your code and find exactly where the runtime error is occurring.

By the way, there's no need to pass N into your Sum and Takeinput functions. Vectors know their own size, and you can query it with the size() method.
@MikeyBoy Before pushing back elements into the vector .their size would be 0 in case of Takeinput function.
still after removing N from arrA and arrB as parameter it's giving error

Are you sure that the values that you input for i and j are less than N?
Last edited on
i am just compiling the program. not giving it any input still it gives error
What are the error messages?
Time limit exceeded
That's it? What compiler do you use?
i m using codechef IDE
even when i m trying to debug it on codeblocks IDE.. it is giving error on line number 37
You need to use at least C++11 for line 37 to work, otherwise you'll have to add a space between the right angle brackets.

 
vector<vector<int> > arrA;
THanks sir... problem got solved on geeksforgeeks ide
I have c++11 on my codeblocks ide stills its not working
Last edited on
I'm guessing that Time limit exceeded is actually a runtime error (like you said in your first post) that is telling you that the code takes too long to run. This might happen if codechef gives you less input than what you expect so that your program just stands there waiting for input until codechef eventually gets tired of waiting and gives up.
I have c++11 on my codeblocks ide stills its not working

You've enabled it? It's much easier to help if you show us the error messages that you get.
Last edited on
Thanks a lot sir...but how to correct my codeblocks compiler it nevers debugs when i use data structures like vector map linked list etc.
Last edited on
What you mean by "debugs"?
when i try debugging programs on codeblocks IDE, it opens header file of that particular data structure ,i used in that program and error message starts popping on the screen
Last edited on
Are these errors preventing you from debugging your program, or are they part of the debugging in which case they might help you fix the error. Please, if you want us to help you you need to start posting more error messages. Just saying that you get errors is not helpful.
Pages: 12