I'm trying to compare elements of the "coordinateList" vector with elements of "Buttons" vector and if four different if-statements are okay to pass them, increase 1 in "countList" vector. But, the problem is that whenever I input 2 numbers for "coordinateList" which can't pass the if-statement, it gets "vector subscript out of range" on the part;
if ((coordinateList[j][0] >= Buttons[i][0])
&& (coordinateList[j][0] <= Buttons[i][1])
&& (coordinateList[j][1] >= Buttons[i][2])
&& (coordinateList[j][1] <= Buttons[i][3]))
I don't know how to fix it.
#include <iostream>
#include <vector>
using namespace std;
int main() {
int numberOfButtons;
int numberOfClicks;
cin >> numberOfButtons;
cin >> numberOfClicks;
int buttonCoordinate;
vector <vector<int> > Buttons;
for (unsigned int i = 0; i < numberOfButtons; i++) {
vector<int> oneButtonCoordinate;
for (unsigned int j = 0; j < 4; j++) {
cin >> buttonCoordinate;
oneButtonCoordinate.push_back(buttonCoordinate);
}
Buttons.push_back(oneButtonCoordinate);
}
int XYCoordinate;
vector <vector<int> > coordinateList;
vector <int> clickCount;
for (unsigned int i = 0; i < numberOfClicks; i++) {
vector<int> oneClickCoordinate;
for (unsigned int j = 0; j < 2; j++) {
cin >> XYCoordinate;
oneClickCoordinate.push_back(XYCoordinate);
}
coordinateList.push_back(oneClickCoordinate);
}
for (unsigned int i = 0; i < numberOfButtons; i++) {
clickCount.push_back(0);
}
for (unsigned int j = 0; j < numberOfClicks; j++) {
for (unsigned int i = Buttons.size() - 1; i >= 0; i--) {
if ((coordinateList[j][0] >= Buttons[i][0]) && (coordinateList[j][0] <= Buttons[i][1]) && (coordinateList[j][1] >= Buttons[i][2]) && (coordinateList[j][1] <= Buttons[i][3])) {
clickCount.at(i) += 1;
break;
}
}
}
(skip)
return 0;
}
Example of correct Input and Output
Input
2 5
1 5 1 5
3 8 3 8
1 1
3 3
3 5
8 8
3 10
Output
Button: #1: 1
Button: #2: 3
so it is the line
for (unsigned int i = Buttons.size() - 1; i >= 0; i--)
which ends up in an invalid index (0xFFFFFFFF still >= 0!).
Use instead:
for (int i = (int) Buttons.size() - 1; i >= 0; i--)
Related
I'm trying to find the smallest of the biggest sum of each column of every possible permutations of a given 2D array NxN, where the values in each row can shift towards the left. For example, the array
4 6
3 7
would have 4 possibles permutations:
4 6 6 4 4 6 6 4
3 7 3 7 7 3 7 3
The biggest sum of each permutation is respectively, 13, 11, 11, 13. Thus the smallest of the biggest sums is 11. I have written a recursive function that should work, but for some reason, it only works for arrays that are smaller than 6x6... I'm new at programming, and just recently learned about recursion, any help or counsel on how to think recursively and to debug code would be greatly appreciated...
For the array 4x4
7410 1371 2665 3195
4775 4130 6499 3414
300 2092 4009 7638
5351 210 7225 7207
The answer is 18349, and my code gives me the correct answer.
However, for the array 6x6
5219 842 7793 2098 5109 2621
1372 3253 3804 5652 810 1620
4894 6792 1784 4335 4772 6656
3203 1070 4716 5335 1157 6855
5529 2767 2205 408 7516 7454
375 7036 2597 5288 937 2893
The answer should be 23733, but I've got 24176. How is this possible?
Here's my code:
#include <iostream>
using namespace std;
#define MAX_N 1000
int n, matrix[MAX_N][MAX_N], shift[MAX_N] = {0}, minSum = 100000000;
void possibTree(int position){
//Base case
if(position == n){
for (int i = 0; i < n; i++) {
// Temporary array to store the values in the row that just shifted towards the left
int temp[MAX_N] = {0};
for (int j = 0; j < n; j++) {
if(j - shift[i] < 0)
temp[n+(j-shift[i])] = matrix[i][j];
else
temp[j-shift[i]] = matrix[i][j];
}
for (int k = 0; k < n; k++)
matrix[i][k] = temp[k];
}
int max = 0;
for (int i = 0; i < n; i++) {
int temp = 0;
for (int j = 0; j < n; j++) {
temp += matrix[j][i];
}
if(temp > max)
max = temp;
}
if(minSum > max)
minSum = max;
return;
}
for (int i = 0; i < n; i++) {
shift[position] = i;
possibTree(position+1);
}
return;
}
int main() {
while(cin >> n){
memset(matrix, 0, sizeof(matrix));
memset(shift, 0, sizeof(shift));
if(n == -1) // The user enters "-1" to end the loop and terminate the program.
return 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
possibTree(0);
cout << minSum << endl;
minSum = 100000000;
}
return 0;
}
Ok I believe I understand my mistake, I have to reset the matrix to its original state at the end of each base case, when the matrices are small, the code is still capable of finding all the possible biggest sums, but when the matrices got bigger, some of the possibilities weren't generated. Here's my code:
#include <iostream>
using namespace std;
#define MAX_N 1000
int n, matrix[MAX_N][MAX_N], OrigMatrix[MAX_N][MAX_N], shift[MAX_N] = {0}, minSum = 100000000;
void possibTree(int position){
//Base case
if(position == n){
for (int i = 0; i < n; i++) {
// Temporary array to store the values in the row that just shifted towards the left
int temp[MAX_N] = {0};
for (int j = 0; j < n; j++) {
if(j - shift[i] < 0)
temp[n+(j-shift[i])] = matrix[i][j];
else
temp[j-shift[i]] = matrix[i][j];
}
for (int k = 0; k < n; k++)
matrix[i][k] = temp[k];
}
int max = 0;
for (int i = 0; i < n; i++) {
int temp = 0;
for (int j = 0; j < n; j++) {
temp += matrix[j][i];
}
if(temp > max)
max = temp;
}
if(minSum > max)
minSum = max;
//EDITS
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = OrigMatrix[i][j];
}
}
return;
}
for (int i = 0; i < n; i++) {
shift[position] = i;
possibTree(position+1);
}
return;
}
int main() {
while(cin >> n){
memset(matrix, 0, sizeof(matrix));
memset(shift, 0, sizeof(shift));
if(n == -1) // The user enters "-1" to end the loop and terminate the program.
return 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
//EDITS
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
OrigMatrix[i][j] = matrix[i][j];
}
}
possibTree(0);
cout << minSum << endl;
minSum = 100000000;
}
return 0;
}
Sample Input
3
11 2 4
4 5 6
10 8 -12
Sample Output
15
Explanation
The primary diagonal is:
11
5
-12
Sum across the primary diagonal: 11 + 5 - 12 = 4
The secondary diagonal is:
4
5
10
Sum across the secondary diagonal: 4 + 5 + 10 = 19
Difference: |4 - 19| = 15
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int i, j;
int arr[i][j];
int x1 = 0, x2 = 0;
for (i = 1; i <= n; ++i)
{
for (j = 1; j <= n; ++j)
{
cin >> arr[i][j];
}
}
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
if (i == j)
x1 = x1 + arr[i][j];
}
}
for (i = 1; i <= n; i++)
{
for (j = n; j <= 1; j--)
{
x2 = x2 + arr[i][j];
}
}
cout << abs(x1 - x2);
}
Here is your corrected code:
1) First, you've declared a 2D array of garbage size.
2) Second, array indexing starts from 0 in most of the programming languages except MATLAB.
3) Always use pre-increment until and unless post-increment isn't absolutely necessary.
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int i, j;
int arr[n][n];
int x1 = 0, x2 = 0;
for (i = 0; i < n; ++i)
{
for (j = 0; j < n; ++j)
{
cin >> arr[i][j];
}
}
for (i = 0; i < n; ++i)
{
for (j = 0; j < n; ++j)
{
if (i == j)
x1 += arr[i][j];
}
}
for (i = 0; i < n; ++i)
{
for (j = 0; j < n; ++j)
{
if(i + j == n - 1)
x2 += arr[i][j];
}
}
cout << abs(x1 - x2);
return 0;
}
PS: There is a lot of scope of improvement in your code formatting.
I need help in a C++ for a school task.
I don't really know where the error is.
It seems like it skips the first row.
I should compare the highest value with an other row's avarage value.
Task:
In the first row of the standard input there are the count of the cities (1≤N≤1000) and the count of days (1≤M≤1000). In the following N row there are the daily forecast M temperature values (-50≤Hi,j≤50).
In the standard output's first row, you have to write the city number, which maximal forecast has to be lower than some other city's avarage temperature! If there is none you should write -1!
Example:
Input
3 5
11 11 11 11 20
18 16 12 16 20
10 15 12 10 10
The code:
#include <iostream>
using namespace std;
int main() {
int N, M;
cin >> N;
cin >> M;
int homerseklet[N][M];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
cin >> homerseklet[i][j];
}
}
int maxh[N] = {0}, osszh[N] = {0};
for (int i = 0; i < N; i++)
{
maxh[i] = homerseklet[i][0];
for (int j = 0; j < M; j++)
{
osszh[i] = osszh[i] + homerseklet[i][j];
if (homerseklet[i][j] > maxh[i])
{
maxh[i] = homerseklet[i][j];
}
}
}
int atlag[N] = {0};
for (int i = 0; i < N; i++)
{
atlag[i] = osszh[i] / M;
}
bool van = false;
for (int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if (i != j && maxh[i] < atlag[j])
{
if (van = true)
{
cout << i + 1 << endl;
}
}
}
}
if (!van)
{
cout << -1 << endl;
}
return 0;
}
I'm beginner in c++, and I want to make a program that delete a row from a matrix.. Like, If I say 3 3 2
1 2 3 it need to show 1 2 3
4 5 6 7 8 9
7 8 9
The program works like this: 3 = rows, 3 = columns, 2 = the deleted row. "3 3 2" is not a row...
I wrote this :
#include <iostream>
using namespace std;
int main() {
int N, M, v[100][100];
cin>>N>>M;
int i,j,p;
cin>>p;
for (i = 1; i <= N; ++i)
for (j = 1; j <= M; ++j)
cin>>v[i][j];
for (i = 1; i <= N; ++i) {
for (j = 1; j <= M; ++j)
cout<<v[i][j]<<" ";
cout<<"\n";
}
for (i = p; i < N; ++i)
v[i][j]=v[i+1][j];
--N;
for (i = 1; i <= N; ++i){
for (j = 1; j <= N; ++j)
cout<<v[i][j]<<' ';
cout<<"\n";
}
return 0;
}
But it doesn't work.... Can someone help me?
modified code (it will work fine ):
#include <iostream>
using namespace std;
int main()
{
int N, M, v[100][100];
cin>>N>>M;
int i,j,p;
cin>>p;
for (i = 1; i <= N; ++i)
for (j = 1; j <= M; ++j)
cin>>v[i][j];
for (i = 1; i <= N; ++i)
{
for (j = 1; j <= M; ++j)
cout<<v[i][j]<<" ";
cout<<"\n";
}
for (i = p; i < N; ++i)
for(j=1; j<=M; ++j)
v[i][j]=v[i+1][j];
--N;
for (i = 1; i <= N; ++i)
{
for (j = 1; j <= M; ++j)
cout<<v[i][j]<<' ';
cout<<"\n";
}
return 0;
}
This doesn't work, what do you think j is doing in this code?
for (i = p; i < N; ++i)
v[i][j]=v[i+1][j];
--N;
You need to loop over rows and columns. Copy each column in every row greater that the row you want to delete. In other words you need nested loops here
for (i = p; i < N; ++i)
for (j = 1; j <= M; ++j)
v[i][j] = v[i+1][j];
--N;
You should use vector
#include <vector>
#include <iostream>
using namespace std;
int main()
{
//matrix
vector< vector<int> > V;
//To Add
for(int i=0; i<100; i++)
{
vector<int> R;
for(int j=0; j<100; j++)
{
int x;
cin>>x;
R.push_back(x);
}
V.push_back(R);
}
//To delete a row
int row_to_delete = 2;
V.erase(V.begin() + row_to_delete);
//To access
for(int i=0; i<V.size(); i++)
{
for(int j=0; j<V[i].size(); j++)
{
cout<<V[i][j];
}
}
}
vector<T> is class that use a template to create a dynamic array.
vector<int> is a var array int. V.push_back(T) you can add a element to array with type T, and with V.erase(V.begin() + int ) you can delete a element to array in this case a row. With V.size() you can take a elements count inside the array.
I have following program. with Input 3 5
3 rows
5 growth of numbers
The output should be:
1 2 4 7 10
3 5 8 11 13
6 9 12 14 15
But my program gives:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
Here is what I have tried so far
int main() {
int n, m, c = 0;
cin >> n >> m;
int a[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i][j] = ++c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << setw(4) << a[i][j];
cout << endl;
}
}
What I am doing wrong or missing?
About the spaces: Can't find reason for such behavior(first spaces are ignored), displayed on screenshot. Tried to run in different IDE's with different compilers and had such problem only in testing system.
Hi try to use tab instead.
#include <iostream>
using namespace std;
int main() {
int n, m, c = 0;
cin >> n >> m;
int *a = new int[n * m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i * n + j] = ++c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << "\t" << a[i * n + j];
cout << endl;
}
delete[] a;
return 0;
}
Can't remember how I solved this problem in secondary school, but with n less than m, the following code works:
#include <iostream>
using namespace std;
void nextij(long n,long m,long& i,long& j) {
if (i==n-1) { //bottom row
if (j<n-1) { //in the left square
j = i+j+1;
i = 0;
}
else { //out of the left square
i = j-(n-1)+1;
j = m-1;
}
}
else { //other rows
if (j==0) { //left most column
j = i+1;
i = 0;
}
else { //other columns
i++;
j--;
}
}
}
int main() {
long n = 3;
long m = 5;
long a[3][5];
long i = 0;
long j = 0;
long c = 1;
while (c<=n*m) {
a[i][j] = c;
nextij(n,m,i,j);
c++;
}
for (i=0; i<n; i++) {
for (j=0; j<m; j++)
cout <<a[i][j] <<" ";
cout <<endl;
}
}
/*
output:
1 2 4 7 10
3 5 8 11 13
6 9 12 14 15
*/