How do I clean up these nested for loops? [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
NOTE: Bad code ahead. You've been warned.
This code iterates through all elements in all 3x3 squares in a Sudoku puzzle. The way shown below is that it gets the coordinate of the upper-left-hand element for each square then iterates through each element in that square. This means that there will be a total of 4 "for" loops needed to just access the correct index, and then another "for" loop to do the correct action.
This code will work (if the retracted code were put in), but it looks very messy and is very hard to read. Is there a better way to do it that eliminates these nested "for" loops?
Thanks in advance.
void Sudoku::updateSquares(int grid[9][9], int possibleSolutions[9][9][10])
{
for (int i = 0; i < 9; i += 3)
{
for (int j = 0; j < 9; j += 3) //for every square:
{
//Other code
//...
//Other code
//updates the possibleSolutions array
for (int k = 0; k < 3; k++)
{
for (int l = 0; l < 3; l++) //for every element in every square:
{
if(grid[i+k][j+l] != 0)
continue;
for (int n = 0; n < 10; n++)
{
if(possibleSolutions[i+k][j+l][n] != 0 && numbers[n] == 0)
{
possibleSolutions[i+k][j+l][n] = 0;
possibleSolutions[i+k][j+l][0] -= 1; //reduce the size, which is held in [][][0]
}
}
}
}
}
}
}

You have implemented something called as "Exhaustive Searching" which is essentially trying out every possible combination of squares.
Have you heard about something called loop unwinding ?
--> Instead of 5 nested For loops use fewer nested loops multiple times; something like 2 nested loops .
Use a dynamic programming approach which is probably O(n^2)
Top Coder DP example.

Related

Primitive operations and running time [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
For the given pseudo code I have to determine primitive operations and running time:
for( i=0; i<n; i++) ->1+n+n=2n+1
a[i] = 0; ->?
for( i=0; i<n; i++ ) ->2n+1
for( j=0; j<n; j++ ) ->n(2n+1)
a[i] += a[j] + i + j; ->?
Anyone to help me if it's correct and how to do the rows which I have put ?. please
As far as I understand, the for loop instruction going from 0 to n times would be executed n+1 times (once extra when loop index equals n itself, after which it breaks), so it would be n+1 and not 2n+1. Its contents would run for n times. Assigning values doesn't add additional complexity in terms of input size (n):
for(i = 0; i < n; i++) // 1+n
a[i] = 0; // runs for n times
Likewise, for the nested loops, the inner-loop statement will run for n+1 times, multiplied by n, as it in itself is the content of the outer loop, making the product of n(n+1). The contents would run for n*n times.
for(i = 0; i < n; i++) // n+1
for(j = 0; j < n; j++) // n(n+1) => n*n+n
a[i] += a[j] + i + j; // runs for n*n, for the dominant factor
With the innermost statements running for n*n times, the overall asymptotic time complexity would be O(n2).

Is there an alternative to declaring a variable inside a for loop? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to count the number of odd values in the elements of the array. Is there a way to perform the below operation without declaring a variable inside the for loop? I am aware that the variable declared inside the loop cannot be accessed outside the loop and I want to know if there is a way that the following loop is performed and the value of oddValueCountKS could be accessed outside of the loop.
int arr[3] = {1004, -237890, 30022};
for (int i = 0; i < 3; i++) {
int oddValueCountKS = 0;
while (arr[i] != 0) {
if (arr[i] % 2) {
oddValueCountKS++;
}
arr[i] /= 10;
}
}
int arr[3] = {1004,-237890,30022};
int oddValueCountKS[3] = {0};
for (int i = 0; i < 3; i++) {
while (arr[i] != 0) {
if (arr[i] % 2) {
oddValueCountKS[i]++;
}
arr[i] /= 10;
}
}
Declare it outside the loop
int oddValueCountKS;
for (int i = 0; i < 3; i++) {
oddValueCountKS = 0;
//the rest of your code
}
this way you will be able to access it outside

How can I clear the variable after while loop is done [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm writing a program that multiplies matrices. And here I have got two variables "i" and "q" which at the beginning are both 0. While the loops proceed variables ("i" and "q") change their values. However after the loops are done I need "i" and "q" to come back to the value 0, so the loops can repeat themselves for different "w" and "k" . How can I do so??
int wynik[x][z]; //table that holds the result of the multiplication
int i=0;
int q=0;
int wyn=0;
for(int w=0; w<x; w++)
{
for(int k=0; k<z; k++)
{
while((i<y) && (q<v) )
{
wyn = (tab1[w][i] * tab2[q][k]) + wyn;
i++;
q++;
}
wynik[w][k] = wyn;
}
}
set those to 0 after the outer loop is done:
int wynik[x][z]; //table that holds the result of the multiplication
int i=0;
int q=0;
int wyn=0;
for(int w=0; w<x; w++)
{
for(int k=0; k<z; k++)
{
while((i<y) && (q<v) )
{
wyn = (tab1[w][i] * tab2[q][k]) + wyn;
i++;
q++;
}
wynik[w][k] = wyn;
}
//HERE
i = 0;
q = 0;
}
I feel like the most natural way according to your current design would be to change the most inner while loop into for (int i = 0, q = 0; (i < y) && (q < v); i++, q++). This lets for loop manage modification of i and q and both are inside the scope of the inner for loop since they are not needed anywhere else.
I would rethink the design. It sounds like a great example to study some algorithms.
Just dropping this here ;)
https://en.wikipedia.org/wiki/Matrix_multiplication_algorithm

How to break inner loop but the loop still increment [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
for (int j=1; j<path.size(); j++) {
//do something
for (int z=0; z<path.size(); z++) {
//do something
}
break;
}
The inner loop can loop from 0 until it hits the end of the path size, but the outer loop just keeps giving me j=1 instead of doing the increment like inter loop does.
How can both loop do the increment at same time? I was trying to delete the break; but z loop through path.size()'s times.
The inner loop executes completely for every iteration of the outer loop, that's why you see z going looping path.size() times. After this your loop stops due to break so it won't iterate a second time over outer loop. So even if condition of outer loop is still true it won't even check it since you're breaking out of your loop.
Try running this test program with and without the break
for (int j=1; j<5; j++) {
printf("j=%d\n",j);
for (int z=0; z<5; z++) {
printf("z=%d\n",z);
}
break; // next time try to remove this break
}
and you might get enlightened.
If you want j and z to be counted up in parallel the you might want something like this
for (int j=1, z= 1; j<5; j++, z++) {
printf("j=%d\n",j);
printf("z=%d\n",z);
}
for(int j = 1 ; j < path.size() ; j++){
do something
for (int z = 0 ; z < j ; z++){
do something
}
I think that's what you're looking for

C++ For Loop: invalid operands to binary expression [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to copy data from one vector to another but am getting an error , "Invalid operands to binary expression 'int' and 'Card' " when I try to compile the following for loop:
for (int i = 0; i <= vectorOne[vecCapacity]; i++) { //step 3
vectorTwo[i] = vectorOne[i];
}
Would anyone have any suggestions?
I believe what you meant is
for (int i = 0; i <= vecCapacity; i++)
or even more likely
for (int i = 0; i < vecCapacity; i++)
The error message is clear enough: in this loop
for (int i = 0; i <= vectorOne[vecCapacity]; i++) { //step 3
vectorTwo[i] = vectorOne[i];
}
i has type int while vectorOne[vecCapacity] has type Card and there is no defined operator <= for these types.
So this loop makes no sense.
Maybe you mean
for (int i = 0; i < vecCapacity; i++) { //step 3
vectorTwo[i] = vectorOne[i];
}
Also take into account that you have to guarantee that the size of vectorTwo is not less than the size of vectorOne or at least vecCapacity.
You could use standard algorithm std::copy declared in header <algorithm>
For example
#include <algorithm>
//...
std::copy( vectorOne, vectorOne + vecCapacity, vectorTwo );
You should be looping from 0 to vectorOne's size.
for (int i = 0; i < vectorOne.size(); i++) { //step 3
vectorTwo[i] = vectorOne[i];
}`
`
Also, if you're doing it this way, make sure vectorTwo is big enough before the loop.
vectorTwo.resize(vectorOne.size());