Is it possible for a for loop with 2 variables to stop incrementing only one of the variable when a condition is met ? For example
for(int i = 0, j = 0; i < 5 && j < 10; i++, j++)
{
cout << i << " " << j << endl;
}
and the output would look something like
0 0
1 1
2 2
3 3
4 4
4 5
4 6
4 7
4 8
4 9
This is my actual code. I wanted the condition for both variables
cout << sp.dets.size() << " " << gt.groundtruth.size() << endl;
for (int i = 0, j = 0; i < sp.dets.size() && j < gt.groundtruth.size(); j < gt.groundtruth.size() ? j++ : j, i < sp.dets.size() ? i++ : i)
{
cout << i << " " << j << endl;
}
sp.dets.size = 0
gt.groundtruth.size() = 8
It would be nice if the solution works for any number i.e. i > j or i < j or i = 0 or j = 0
You can use ternary statement to increment variable i value. Like below:
for(int i = 0, j = 0; i < 5 && j < 10; j++, i<4? i++: i)
{
cout << i << " " << j << endl;
}
This will output the expected result.
Something like this may work,
int k = 0;
int val = 4;
for(int i = 0, j = 0; i < 10 && j < 10; i++, j++){
if (i>=val){
k=val;
cout << k << " "<< j << endl;
}
else
cout << i << " " << j << endl;
}
Prints this,
0 0
1 1
2 2
3 3
4 4
4 5
4 6
4 7
4 8
4 9
Don't try to be overly clever by trying to fit all the logic in one line. It't more important that your code can be read by others (and yourself in two weeks' time) than saving a single line. If the incrementing logic of the two variables is more complex than just increasing by one for each loop, put them in seperate lines, like this:
while (i<5 && j<10) {
...
// complicated expression calculating new i
// complicated expression calculating new j
}
In your case, you might also flip the logic around:
for (int i=0; i<max(sp.dets.size(), gt.groundtruth.size(); i++) {
cout << min(i, sp.dets.size()-1)
<< ","
<< min(i, gt.groundtruth.size()-1)
<< endl;
}
You can use ternary operator on both variables. Just be sure to change your condition to break only when both reach their destined value
for (int i = 0, j = 0;
i != 5 || j != 10;)
{
i += i < 5 ? 1 : 0;
j += j < 10 ? 1 : 0;
}
You'll note I moved the increment into the loop body, this improves readability, in my opinion.
Related
I wrote a program in c++ that is made up of two for loops like this:
for(int i=1; i<3; i++)
{
for(int j=1; j<3; j++)
{
cout<<j<<"\t"<<2*j*i<<endl;
}
}
with output:
1 2
2 4
1 4
2 8
But I’m not interested in output with format like this, what I’m looking for is that after the first for loop over j with i=1 is finished, the output of the for loop over j with i=2 prints in a new column like below.
1 2 1 4
2 4 2 8
Invert the loops
for(int j = 1; j < 3; ++j)
{
for(int i = 1; i < 3; ++i)
{
std::cout << j << "\t" << (2 * j * i) << "\t"; // No std::endl here
}
std::cout << std::endl;
}
Moreover, some suggestions about your code:
Use pre-incrementation when it's possible (++i instead of i++). Check the difference here.
Keep the namespace std on your types (std::cout, std::endl). You will avoid a lot of beginner mistakes.
Add spaces in your code, it's moooooore clear (std::cout << j << "\t" << (2 * i * j) << std::endl is easier to read than std::cout<<j<<"\t"<<(2*i*j)<<std::endl )
The algorithm is supposed to find the minimum cost path in NxN matrix given as an input. The starting cell is always left bottom and the destination is right top.
Each cell of the matrix represents a cost to traverse through that cell.
You can only move up and right.
I have managed to find the cost, however, I still struggle to backtrack the path.
I tried to start from top right cell and use the greedy algorithm to find my "way back", but the output was either completely wrong or skipping random columns/rows. I also tried to keep track of decisions I was making by creating an additional matrix, but I always end up stuck in the loop.
So how do I find the path?
Here's the code that works well (counts the cost and that's it):
#include <iostream>
using namespace std;
int main()
{
int tab[101][101], N, cost[101][101], backtrack[101][101];
cout << "N (size of NxN matrix) :" << endl;
cin >> N;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
cin >> tab[i][j];
cost[i][j] = 0;
backtrack[i][j] = 0;
}
}
cost[N-1][0] = tab[N-1][0];
int a = N-1;
for(int i = N-2; i >= 0; i--) // column 0 can be chosen only in 1 way
{
cost[i][0] = cost[i+1][0] + tab[i][0];
backtrack[i][0] = 4; // came from down
}
for(int j = 1; j < N; j++) // row N-1 can be chosen only in 1 way
{
cost[a][j] = cost[a][j-1] + tab[a][j];
backtrack[a][j] = 3; // came from right
}
for(int i = N-2; i >= 0; i--)
{
for(int j = 1; j < N; j++)
{
if(cost[i][j-1] <= cost[i+1][j])
{
cost[i][j] = tab[i][j] + cost[i][j-1];
backtrack[i][j] = 3;
}
else
{
cost[i][j] = tab[i][j]+cost[i+1][j];
backtrack[i][j] = 4;
}
}
}
cout << "Cost: " << cost[0][a] << endl;
return 0;
}
Now, here's the function with flawed additional matrix that's supposed to give me the path, but ends up in an infinite loop:
(matrix backtrack from previous code was given as track here)
void path(int track[101][101], int N)
{
int help[101][101];
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
help[i][j] = 0;
}
int w = 0, k = N-1;
help[w][k] = 1; // top right argument is included in the output
while(w < N || k >= 0)
{
if(track[w][k] == 3)
{
help[w][k-1] = 1; // 3 means I came from the previous column k-1
k--;
}
else if(track[w][k] == 4)
{
help[w+1][k] = 1; //4 means I came from the previous row w+1
w++;
}
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if(help[i][j] != 0)
cout << i << " " << j << endl;
}
}
}
Example input:
5
2 3 4 2 5
5 2 1 2 2
2 4 2 2 3
1 2 2 4 3
3 2 1 2 3
Expected output:
Cost: 20
4 0
4 1
4 2
3 2
2 2
1 2
1 3
0 3
0 4
Actual output
Cost: 20
And no path at all since it ends up in an infinite loop.
You have written the while loop in path() incorrectly:
while(w < N || k >= 0)
...
You intend this loop to continue until w = N-1 and k=0, which it does, but the loop doesn't terminate there, it just runs in place. (You could see this yourself by adding cout << w << " " << k << endl; to the loop.) The conditional I think you want is:
while(w < N-1 || k > 0)
The idea I have is pretty simple. What I'd like to do is just have the user input a series of integers and plug those integers specified into an array. So for example, here's a segment of the program I'm writing.
const int size = 1000;
int array_one[size], array_two[size];
for (int i = 0; i < size; i++)
{
std::cin >> array_one[i];
if (array_one[i] == -1) break;
}
for (int i = 0; i < size; i++)
{
std::cin >> array_two[i];
if (array_two[i] == -1) break;
}
The cut off point is -1. Should the user enter this, one index of the array will be all integers greater than -1 with the following indexes housing 0's in this 1000 sized array.
What I'm doing next is simply replicating how you would write out basic addition on paper. So, lets say for example I plugged in 1 0 0 6 -1 as inputs for the first array and then 2 3 4 for the second array. The whole entire output should look something akin to this
1006
+ 234
------
1240
Here's what I've typed out to get this sort of output
int array_size_one = 0;
int array_size_two = 0;
int space_size = 3;
int cut_off_space = 0;
int count = 0;
for (int i = 0; i < size; ++i)
{
if (array_one[i] < 0) break;
array_size_one += 1;
}
for (int i = 0; i < size; ++i)
{
if (array_two[i] < 0) break;
array_size_two += 1;
}
if (array_size_one > array_size_two) space_size += array_size_one;
else if (array_size_two > array_size_one) space_size += array_size_two;
if (array_size_one < array_size_two) cut_off_space += array_size_one;
else if (array_size_two < array_size_one) cut_off_space += array_size_two;
std::cout << std::setw(space_size - array_size_one);
for (int j = 0; j < size; ++j)
{
if (array_one[j] < 0) break;
std::cout << array_one[j];
}
std::cout << "\n+";
std::cout << std::setw((space_size - 1) - array_size_two);
for (int j = 0; j < size; ++j)
{
if (array_two[j] < 0) break;
std::cout << array_two[j];
}
std::cout << "\n";
std::cout << std::setfill('-') << std::setw(space_size) << " " << std::endl;
for (int i = 0; i < (space_size - 3); ++i)
{
count = array_one[i] + array_two[i];
}
std::cout << count << std::endl;
return 0;
This is the part where I think I'm sure I'm having trouble with.
std::cout << "\n";
std::cout << std::setfill('-') << std::setw(space_size) << " " << std::endl;
for (int i = 0; i < (space_size - 3); ++i)
{
count = array_one[i] + array_two[i];
}
std::cout << count << std::endl;
return 0;
The idea was that I wanted to add up everything inside each index of the arrays to perform standard integer expressions. The for loop above isn't really cutting it. I'm getting outputs like 5 or 15. If going by the example above that has inputs of 1 0 0 6 -1 for the first array and then 2 3 4 -1 for the second array. What could I actually do inside the very last for loop iteration to make sure I get the correct outputs replicating standard integer expressions?
I need to print an int array that has 50 values the array must contain multiple rows and each row can't have any more than 15 variables on it. Can any one point me in the right direction or explain where I'm going wrong?
#include <iostream>
using namespace std;
int main()
{
int alhpa[51];
for(int i = 0; i < 51; i++)
{
alpha[i] = -1; // initializes all elements to -1
}
for(int i = 0; i < 51; i++)
{
for(int j = 0; j < 15; j++)
{
cout << alpha[j] << "\t";
}
cout << endl;
}
return 0;
}
So the above code works it just doesn't do what I intended it to, if you run this you'll see that the array is printed in rows and each row does have 15 variables on it. How ever the issue is that it only prints the first 15 elements in the array and each time the main loop executes the process is reset and as I said above the only output is the first 15 variables over and over again.
I need to figure out how to print this array out in the same way it's printing now but I need all the elements in the array to be processed and printed.
I know that the last line wouldn't have 15 on it because there should only be room for 5 because 15 * 3 = 45
Any help would be awesome!
How about modifying this code
for(int i = 0; i < 51; i++)
{
for(int j = 0; j < 15; j++)
{
cout << alpha[j] << "\t";
}
cout << endl;
}
to this
for(int i = 0; i < 51; i += 15)
{
for(int j = i; j < 15+i && j < 51; j++)
{
cout << alpha[j] << "\t";
}
cout << endl;
}
This prints them in sets of 15 each line
The output of your program in this case will be
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1- 1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1- 1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1- 1
-1 -1 -1 -1 -1 -1
Change
cout << alpha[j] << "\t";
to
if(15*i+j < 51)
cout << alpha[15*i+j] << "\t";
If the element you access is only j, which loops from 0 to 14, it is normal that you only print the first 15 values.
With this change you can also change the outer loop to for(int i = 0; i < 4; i++)
As an alternative, you can use a counter for elements per line:
int count = 0;
for (int i = 0; i < 51; i++)
{
cout << alpha[i] << "\t";
if (++count == 15)
{
cout << endl;
count = 0;
}
}
if (count > 0) // EOL after any residual values
cout << endl;
Your
cout << alpha[j] << "\t";
Only references the j var, meaning it will just repeat the same first 15 items in the array over and over. You will need to refactor this nested loop to achieve the results you want.
for ( j = 0; j < d1; j++ ){
m += j;
for ( i = 0; i < d1*d2; i +=d2){
cout << *(m+i);
}
cout << endl;
}
d1,d2 are array dimensions
and
int* m = new int [d1*d2];
I want to traverse over my array and simply group and print the columns.Can't figure what's wrong with this code.Seems to be working fine untill the 3rd iteration in the following example:
Let's say my input values are 1 2 3 4 5 6 7 8 9
I get:
1 4 7
2 5 8
4 7 (something random)
In
m += j;
you are first incrementing m by 0, then by one, then by 2. If we originally took a copy
int *start = m;
then in the first iteration of the outer loop, we'd have
m == start
in the second,
m == start + 1
in the third
m == start + 3
You'd want m == start + 2 there. Except that you want to keep m in order to delete it at the end, so you shouldn't change m at all but use something like
for ( j = 0; j < d2; j++ ){
for ( i = j; i < d1*d2; i +=d2){
cout << *(m+i);
}
cout << endl;
}
m = &a[0];
for ( j = 0; j < d1; j++ )
{
for ( i = 0; i < d2; i++)
cout << *m++;
cout << endl;
}