A couple of things stand out that can be a major stumbling block to helping you:
1. No comments. You know what the code you posted is supposed to do, I don't have much of a clue.
2. Using single character variable names. What are they for? Single character variable names by convention should only be used in simple loops, not everywhere as you've done.
Good variable names go a long way to self-documenting your code.
3. Your request for inputs. No text "markers" to inform the user what they should enter.
If you want help with your code, write it so it can be understood by others.
More than once I've looked at older code I worked on months and years ago and set aside. Now it looks like something others have written. I don't remember why I wrote something a certain way if I didn't document it.
I made some wild guesses as to what your code was doing, lack of documentation didn't help. The following for me works, but it may or may not be what you want it to do:
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#include <iostream>
#include <vector>
#include <cctype> // for toupper()
void Takeinput(std::vector<std::vector<int>>& arr)
{
for (size_t rows = 0; rows < arr.size(); rows++)
{
for (size_t cols = 0; cols < arr[0].size(); cols++)
{
std::cout << "[" << rows << "][" << cols << "]: ";
int input;
std::cin >> input;
arr[rows][cols] = input;
}
}
}
int Sum(std::vector<std::vector<int>>& arrA, std::vector<std::vector<int>>& arrB)
{
int sum = 0;
int sum1 = 0;
int sum2 = 0;
unsigned i = 0;
while (i < arrA.size())
{
for (unsigned 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()
{
std::cout << "Enter the dimension of the square vector: ";
int N;
std::cin >> N;
// create the vector and THEN pass it to your function
std::vector<std::vector<int>> arrA(N, std::vector<int>(N));
std::cout << "Enter the values for the first 2D vector:\n";
Takeinput(arrA);
std::vector<std::vector<int>> arrB(N, std::vector<int>(N));
std::cout << "Enter the values for the second 2D vector:\n";
Takeinput(arrB);
// what does this input do? Use a text "label" so the user knows what to enter and why.
int Q;
std::cin >> Q;
while (Q--)
{
// the following inputs are somewhat easier to understand without any documentation, but
// better variable names and/or some comments could be very helpful
char ch;
std::cin >> ch;
int a, b, c;
std::cin >> a >> b >> c;
// I used the toupper() function so it doesn't matter whether the user entered upper or lower case
// http://www.cplusplus.com/reference/cctype/toupper/
if (toupper(ch) == 'A')
{
arrA[a][b] = c;
}
else
{
arrB[a][b] = c;
}
std::cout << "The sum of the two 2D vectors is: ";
std::cout << Sum(arrA, arrB) << ".\n";
}
}
|
Enter the dimension of the square vector: 2
Enter the values for the first 2D vector:
[0][0]: 2
[0][1]: 4
[1][0]: 6
[1][1]: 8
Enter the values for the second 2D vector:
[0][0]: 4
[0][1]: 8
[1][0]: 12
[1][1]: 24
1
a 1
2
3
The sum of the two 2D vectors is: 640. |