Program seems to skip 'if' statement - c++

I'm attempting to create a program that will multiply 3 not equal values from vector 1 ('V1'), and write in vector 2 ('V2'). I'm using 3 'for' loops and an integer that counts vector 2's value's number. But program skips the 'if' statement.
#include <vector>
#include<fstream>
#include<iostream>
#include <algorithm>
using namespace std;
void earase(vector<int> V2, unsigned int z){
V2.erase(V2.begin() + z);
}
void main(){
unsigned int N;
unsigned int z = 0;
vector<int> V1;
vector<int> V2;
int input;
ifstream file1;
file1.open("input.txt");
file1 >> N;
for (unsigned int i = 0; i < N; i++){
file1 >> input;
V1.push_back(input);
}
file1.close();
for (unsigned int i = 0; i < V1.size(); i++){
for (unsigned int j = 0; j < V1.size(); j++){
for (unsigned int k = 0; k < V1.size(); k++){
V2.push_back(V1[i] * V1[j] * V1[k]);
if (i == j || i == k || j == k || (i == k ) && k==j)
{
void earase(vector<int> V2, unsigned int z);
}
z++;
}
}
}
sort(V2.begin(), V2.end());
ofstream file2;
file2.open("output.txt");
file2 << V2.back();
file2.close();
}
Here is the input.txt file
4
1 2 3 4
After finishing the program sorts the vector 2 and writes the last number in vector 2 in 'output.txt' but it writes values that will come only if multiply the same value from vector 1, 3 times.
I think the problem is in 'if' statement, and 'z' integer, is there any more efficient way to do this?

Non equal positions is trivial, no test required (assuming you want each combination only once):
for (unsigned int i = 0; i < V1.size(); i++){
for (unsigned int j = 0; j < i; j++){
for (unsigned int k = 0; k < j; k++){
V2.push_back(V1[i] * V1[j] * V1[k]); } } }
If for some reason, you wanted all each combination multiple times (as if you had gone through all N cubed possibilities skipping only duplicate positions) just go through the above simple way and push each multiple times.

The body of your if is actually something quite different than you expect:
if (i == j || i == k || j == k || (i == k ) && k==j)
{
void earase(vector<int> V2, unsigned int z); // <<<<<<<
}
You're not calling earase. Instead, you're declaring a function called earase. If you want to call the function, call it:
if (i == j || i == k || j == k || (i == k ) && k==j)
{
earase(V2, z);
}
By the way you could have easily checked that with some debugging output (like std::cout << "if entered"; after/before earase). However, keep in mind that this won't actually change V2.

Related

Floating Point Error With This Code Can someone help me

i am facing problem with this chinese remainder theoram
i am taking an input.txt file as input
and trying to generate an output.txt file but it says floating point error. when i am running with some specific input at that time it is working but for many test cases it is not working
#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
using namespace std;
int ModInverse(int a, int m)
{
a = a % m;
for(int x = 1; x < m; x++)
if(((a * x) % m) == 1) return x;
}
int findMinX(int num[], int rem[], int k) {
int prod = 1;
for(int i = 1; i <= k; i++) prod *= num[i];
int result = 0;
for(int j = 1; j <= k; j++) {
int y = prod / num[j];
result = result + rem[j] * ModInverse(y, num[j]) * y;
}
return result % prod;
}
int main() {
ifstream infile;
infile.open("input.txt");
int n;
int num[100];
int rem[100];
infile >> n;
for(int i = 0; i < n; i++) infile >> num[i];
for(int i = 0; i < n; i++) infile >> rem[i];
infile.close();
int k = sizeof(num) / sizeof(num[0]);
ofstream myfile;
myfile.open("output.txt");
myfile << findMinX(num, rem, k);
myfile.close();
return 0;
}
These lines:
for(int i = 1; i <= k; i++)
for(int j = 1; j <= k; j++)
will cause i and j to go out of bounds.
Array indexing starts at 0 in C++, so you should use indexes 0 to k-1.
Do this instead:
for(int i = 0; i < k; i++)
for(int j = 0; j < k; j++)
Another thing worth checking up is this function:
int ModInverse(int a, int m)
{
a = a % m;
for(int x = 1; x < m; x++)
if(((a * x) % m) == 1) return x;
}
Given the wrong input, it'll exit the loop and return nothing, which causes undefined behaviour. Validate the input and print an error message if the file contains data you can't handle.
Here's an example of an input.txt that makes it crash for me:
5
1 2 3 4 5
2 3 4 5 6
Another cause for concern is that you use k instead of n in your call to the function:
myfile << findMinX(num, rem, k);
This means the function will always work on 100 values. Some of them may be uninitialized, and again, undefined behaviour.

How to fix vector subscript out of range in c++

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--)

Memory Limit Exceeded while reading integers into the vector

I am trying to read integer values from text file into a vector.
Input file ip1.txt has the following content:
4
-1000 -2000 -3000 -4000
int maxsub(vector<int> a, int size)
{
a.erase(a.begin());
vector<int> sum;
for(vector<int>::iterator w=a.begin(); w <= a.begin()+size-1; ++w)
{
int j;
int s=*w;
for(int t=0; t <= size-1; t++)
{
j = s + a[t];
sum.push_back(j);
}
a.pop_back();
}
std::sort(sum.begin(),sum.end());
int u = sum.size()-1;
int m = sum.at(u);
return m;
}
int main()
{
std::vector<int> nums( (std::istream_iterator<int>(std::cin)),
std::istream_iterator<int>() );
int k = nums[0];
int u = maxsub(nums,k);
cout << u <<endl;
}
I am getting Warning message as 'Memory Limit Exceeded'
How can i resrict vector to read only till -4000 in the input file,I am using file redirection
*./123 < ip1.txt *
Bug at the source code.
All loops in maxsub()
for(vector<int>::iterator w=a.begin(); w <= a.begin()+size-1; ++w)
for(int t=0; t <= size-1; t++)
has iterations from 0 to size-1 elements (=size all in all elements), but after code
a.erase(a.begin());
vector 'a' has only (size-1) elements.
Therefore, all for-operators is 'outside-the-boundary'.
I think, it's cause of warning message.
If you know that the first number will be the length of your vector, why not take advantage of it?:
int length = 0;
std::cin >> length;
std::vector<int> numbers;
numbers.reserve(length);
then you can just use simple for loop that checks whether the number has been successfully extracted from the stream and also prevents more than specific amount of numbers being read:
int number;
for (int i = 0; (i < length) && (std::cin >> number); ++i)
{
numbers.push_back(number);
}
Don't forget to #include <iostream>, #include <sstream> and #include <vector>. Also note that std::cin can be easily replaced with file stream :)
Write a for loop
std::vector<int> nums;
int size;
std::cin >> size;
for (int i = 0; i < size; ++i)
{
int val;
std::cin >> val;
nums.push_back(val);
}
'a.begin()+size-1' is before begin():
Having
for(vector<int>::iterator w=a.begin(); w <= a.begin()+size-1; ++w)
and
int k = nums[0];
int u = maxsub(nums,k);
and
nums[0] = -1000;
size is -1000
If you want to exclude the first and last element:
if( ! a.empty()) {
// Not erasing the first element: a.erase(a.begin());
for(vector<int>::size_type i = 1; i < a.size() - 1; ++i);
}

Usage of multidimensional vectors

I was trying to use multidimensional vector and change the values of row and column.
#include<iostream>
#include<vector>
using namespace std;
void changerow(vector<vector<int> > A, int row, int M, int P){
for(int j = 0; j < M; j++){
(A[row - 1])[j] = ((A[row - 1])[j] + P) % 10;
}
}
void changecolumn(vector<vector<int> > A, int column, int N, int P){
for(int i = 0; i < N; i++){
(A[i])[column - 1] = ((A[i])[column - 1] + P) % 10;
}
}
int main(int argc, char* argv[])
{
int T, N, M;
cin >> T >> N >> M;
if((T >= 1 && T <= 10) && (M >= 1 && M <= 100) && (N >= 1 && N <= 100)){
// Logic of the program
vector<vector<int> > A(N, vector<int>(M));
for(int i = 0; i < N ; i++){
for(int j = 0; j < M; j++){
cin >> (A[i])[j];
}
}
changerow(A,2,M,3);
for(int i = 0; i < N ; i++){
for(int j = 0; j < M; j++){
cout << A[i][j];
}
}
}
return 0;
}
I don't know how would pass the address of the vector in order to change the element, since only the local copy of the vector gets passed. I am currently reading Thinking in C++ Volume 1 but its not elaborate. Kindly let me know a good source for learning the use of vectors in C++.
Currently, you are passing the vector by value, which means that the callee gets a copy of the vector.
If you wish the changes that the callee makes to be visible to the caller, you need to pass the vector by reference. This is done like so:
void changecolumn(vector<vector<int> >& A, int column, int N, int P){
^ THIS
For a discussion, see Pass by Reference / Value in C++

How can I properly add and access items to a multidimensional vector using loops?

I have this program that is trying to determine how many unique items are within some intersecting sets. The amount of input entirely depends on the the first value n, and then the amount of sets entered afterward. For example, if I start with entering n = 2, I am expected to enter 2 integers. The program then determines how many intersections there are between n items (this is like choosing 2 items from n items). This goes on as k increments. But that's kind of beyond the point. Just some background info.
My program adapts correctly and accepts the proper amount of input, but it stops working properly before the first for loop that is outside of the while loop. What I have tried to do is make a vector of integer vectors and then add every other row (when index starts at 0 AND index starts at 1). But I am guessing I have constructed my vectors incorrectly. Does anybody see an error in my vector logic?
#include <iostream>
#include <vector>
using namespace std;
int fact (int m) {
if (m <= 1)
return 1;
return m * fact(m - 1);
}
int comb (int n, int k) {
return fact(n)/(fact(n-k)*fact(k));
}
int main() {
int n = 0;
int k = 2;
int sum = 0;
int diff = 0;
int final = 0;
vector <vector <int> > arr;
cin >> n;
while (n > 0) {
vector <int> row;
int u;
for (int i = 0; i < n ; ++i) {
cin >> u;
row.push_back(u);
}
arr.push_back(row);
n = comb(row.size(), k);
k++;
}
for (int i = 0; i < arr.size(); i+2)
for (int j = 0; j < arr[i].size(); ++j)
sum += arr[i][j];
for (int i = 1; i < arr.size(); i+2)
for (int j = 0; j < arr[i].size(); ++j)
diff += arr[i][j];
final = sum - diff;
cout << final;
return 0;
}
for (int i = 0; i < arr.size(); i+=2)
^
You want to do i+=2 or i=i+2, else the value of i is never changed, leading to an infinite loop.