problem passing 2-D vector to a function

Pages: 12
Jul 20, 2018 at 9:32am
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 Jul 20, 2018 at 10:19am
Jul 20, 2018 at 10:01am
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.
Jul 20, 2018 at 10:06am
still after removing N from arrA and arrB as parameter it's giving error
Jul 20, 2018 at 10:07am
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.
Jul 20, 2018 at 10:11am
@MikeyBoy Before pushing back elements into the vector .their size would be 0 in case of Takeinput function.
Jul 20, 2018 at 10:12am
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 Jul 20, 2018 at 10:12am
Jul 20, 2018 at 10:15am
i am just compiling the program. not giving it any input still it gives error
Jul 20, 2018 at 10:15am
What are the error messages?
Jul 20, 2018 at 10:17am
Time limit exceeded
Jul 20, 2018 at 10:22am
That's it? What compiler do you use?
Jul 20, 2018 at 10:23am
i m using codechef IDE
Jul 20, 2018 at 10:26am
even when i m trying to debug it on codeblocks IDE.. it is giving error on line number 37
Jul 20, 2018 at 10:30am
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;
Jul 20, 2018 at 10:33am
THanks sir... problem got solved on geeksforgeeks ide
I have c++11 on my codeblocks ide stills its not working
Last edited on Jul 20, 2018 at 10:34am
Jul 20, 2018 at 10:37am
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.
Jul 20, 2018 at 10:40am
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 Jul 20, 2018 at 10:41am
Jul 20, 2018 at 10:41am
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 Jul 20, 2018 at 10:42am
Jul 20, 2018 at 10:43am
What you mean by "debugs"?
Jul 20, 2018 at 10:55am
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 Jul 20, 2018 at 10:59am
Jul 20, 2018 at 11:32am
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