Turning several loops into one recursion - c++

I have this piece of code:
for (int i=0; i<N; i++)
{
int j;
if (i%2==1) j=1;
else j=0;
for (; j<N; j+=2)
{
for(int k=0; k<N; k++)
{
int n;
if (k%2==1) n=1;
else n=0;
for (; n<N; n+=2)
{
for (int l=0; l<N; l++)
{
int o;
if (l%2==1) o=1;
else o=0;
for (; o<N; o+=2)
{
for(int m=0; m<N; m++)
{
int p;
if (m%2==1) p=1;
else p=0;
for (; p<N; p+=2)
{
if (check_full(lenta,i,j,k,n,l,o,m,p))
{
count++;
cout<<"Lenta uzsipilde: ("<<i<<","<<j<<"), "<<"("<<k<<","<<n<<"), "<<"("<<l<<","<<o<<"), "<<"("<<m<<","<<p<<"), "<<endl;
}
}
}
}
}
}
}
}
}
Is there any way it could be turned into recursion? Basically these loops find all possible coordinates for given problem. And if it can be converted into one small recursion, will I need to use array instead of 8 variables?
Here's what I tried to do, but it doesn't work:
void findBishops(){
for (int i=0; i<N; i++){
int j;
if (i%2==1) j=1;
for (; j<N; j+=2){
putIntoArray(array, i, j);
if (isFull(board, array)){
PrintAnswer(array);
}else{
arrayCount = arrayCount-2;
findBishops();
}
}
}
}
void putIntoArray(array[], i, j){
array[arrayCount++] = i;
array[arrayCount++] = j;
}

I would probably recurse over the bishops rather than loop over the board:
First place one bishop, then recursively place the second, and so on until you reach the last.
The backtracking happens when you return from the recursion, and that's when you try the next alternative, for a single bishop, and recurse again.
Until you've run out of options - that's when you're done.
Here's a rough outline:
place_bishop(this_bishop)
if this_bishop is the final bishop:
for every possible position of this_bishop:
see if it's a solution and handle that
else:
for every possible position of this_bishop:
place_bishop(next_bishop)
Position-picking requires some thought in order to not locate the same solution more than once.

Related

(C++) How to imagine working of nested loops?

I have no idea what's going on how to imagine that, one more thing like in 3rd for loop there's a condition that k<j but its upper loop j is set to 0 and i is also 0 then as I think k=0 if this is right than 0<0 how's that can be valid????
void printing_subarrays(int *arr,int n){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
for(int k=i; k<j; k++){
cout<<arr[k]<<", ";
}cout<<endl;
}cout<<endl;
}
}
I don't think it makes much sense for us to explain what's happening, you need to see it for yourself.
Therefore I've added some output lines, which will show you how the values of the variables i, j and k evolve through the loops:
for(int i=0; i<n; i++){
cout<<"i=["<<i<<"]"<<endl;
for(int j=0; j<n; j++){
cout<<"i=["<<i<<"], j=["<<j<<"]"<<endl;
for(int k=i; k<j; k++){
cout<<"i=["<<i<<"], j=["<<j<<"], k=["<<k<<"]"<<endl;
cout<<arr[k]<<", ";
}
cout<<endl;
}
cout<<endl;
}
Did you try maybe running it? Then it should be apparent...
#include <iostream>
#include <vector>
void printing_subarrays(int *arr, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = i; k < j; k++) {
std::cout << arr[k] << ", ";
}
std::cout << "\n";
}
std::cout << std::endl;
}
}
int main() {
int n = 10;
std::vector<int> vec(n);
for (size_t i = 0; i < n; ++i) {
vec[i] = i;
}
printing_subarrays(vec.data(), n);
return 0;
}

Output not getting print after matrix multiplication

In the code below, my aim is to multiply two matrices reflect[3][3] and mat[3][s] where s can be any value 0-10. Here the statement (A) and (B) is not getting printed, please tell me why??
#include<iostream>
# include<math.h>
#include<conio.h>
using namespace std;
int mat[10][10];
int result[3][10];
int reflect[3][3]= {1,0,5,0,1,5,0,0,1};
int i , j,k,s;
void multiply_matrix(int A[3][3], int B[3][10])
{
for(i=0; i<3; i++)
for( j=0; j<10; j++)
result[i][j] = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < s; j++)
{
result[i][j] = 0;
for (k = 0; k < 3; k++)
{
result[i][j]=result[i][j]+(A[i][k]*B[k][j]) ;
}
cout<<result[i][j]<<" ";//------(1)
}
cout<<endl;
}
cout<<"Multiplication after matrix: "<<endl;
for(i=0; i<3; i++)
{
for(j=0; j<s; j++)
{
cout<<result[i][j]<<" ";//------(B)
}
cout<<endl;;
}
}
int main()
{
int i, j,s;
cout<<"Enter the sides of polygon :\n";
cin>>s;
cout<<"Enter the coordinates of polygon :\n";
cout<<"Enter x coordinates ";
for(i=0; i<s; i++)
cin>>mat[0][i];
cout<<"Enter y coordinates ";
for(i=0; i<s; i++)
cin>>mat[1][i];
cout<<"\n\n";
for(i=0; i<s; i++)
mat[2][i] = 1;
cout<<"MAt: "<<endl;
for(i=0; i<3; i++)
{
for(j=0; j<s; j++)
{
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
multiply_matrix(reflect, mat);
cout<<"End"<<endl;
return 0;
}
I have attached a sample output image for reference:
Output of code
I am a newbie and have tried various things but I am just not able to figure my mistake. Thanks in advance for your help.
You have two s in your code. Globally:
int i , j,k,s;
And in main:
int i, j,s;
The one in main you assign a value read from user input, but the global one is 0 always. multiply_matrix uses the global one, hence this loops have zero iterations:
for (j = 0; j < s; j++)
{
result[i][j] = 0;
for (k = 0; k < 3; k++)
{
result[i][j]=result[i][j]+(A[i][k]*B[k][j]) ;
}
cout<<result[i][j]<<" ";//------(1)
}
and
for(j=0; j<s; j++)
{
cout<<result[i][j]<<" ";//------(B)
}

Selection Sort in c++

i'm trying to understand selection sort from this video:
https://www.youtube.com/watch?v=79AB11J5BqU
this is my current code :
#include <iostream>
int main() {
int numbers[5]={5,3,4,1,2};
int temp;
std::cout<<"BEFORE SORT : \n";
for(int x=0;x<5;x++){
std::cout<<numbers[x]<<" ";
}
for (int i = 0; i < 5; ++i) {
for (int j = i+1; j < 5; ++j) {
if(numbers[j]<numbers[i]){
temp = numbers[j];
numbers[j] = numbers[i];
numbers[i] = temp;
}
}
}
std::cout<<"\n\nAFTER SORT : \n";
for(int x=0;x<5;x++){
std::cout<<numbers[x]<<" ";
}
}
Am i doing the selection sort just like the video?
or am i instead doing buble sort ?
Thanks
In selection sort you find a minimal (or maximal) element and put it to top (bottom), then repeat it again for the rest of list.
It would be a selection sort, but you don't need to do swap every number you compare to find the smallest one. Store smallest number index in each internal loop and do one swap at the end of it.
unsigned minIndex;
for (int i = 0; i < 5; ++i) {
minIndex = i;
for (int j = i + 1; j < 5; ++j) {
if(numbers[j] < numbers[minIndex]){
minIndex = j;
}
}
if (minIndex != i) { // Do swapping
temp = numbers[i];
numbers[i] = numbers[minIndex];
numbers[minIndex] = temp;
}
}
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning.
#include<iostream>
using namespace std;
// Selection Sort//
void Selection_Sort(int a[],int n)
{
for(int i=0;i<n-1;i++)
{
int min_index=i;
for(int j=i;j<n-1;j++)
{
if(a[j]<a[min_index]){
min_index=j;
}
}
swap(a[i],a[min_index]);
}
}
int main()
{
int n,key;
cin>>n;
int a[1000];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
Selection_Sort(a,n);
for(int i=0;i<n;i++)
{
cout<<a[i];
}
}

Find the number of common factors for n numbers excluding "1" as common factor

Here is my code that i have written in gcc. Don't know if my logic is incorrect or i'm making some other mistake.The output is coming as 0 every time.
int main()
{
int n,a[20],count=0;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int k=0;k<n;k++)
{
int c=0;
for(int j=2;j<n;j++)
{
if(a[k]%j==0)
{
c++;
}
else
{
continue;
}
}
if(c==n)
{
count++;
}
}
cout<<count;
}
You got the loops in the wrong order. You are checking if there are n dividers of any of the numbers.
Try swapping the loops. Then the count will be of the number of numbers that divide the input.
You also have the wrong upper bound. You need the highest possible divider.
int max_divider = 2;
for (i = 0; i < n; i++) {
if (a[i] > max_divider)
{
// Naive approach
max_divider = a[i];
}
}
// For each number 2..max_divider
for(int j=2;j <= max_divider;j++)
{
int c=0;
for(int k=0;k<n;k++)
{
if(a[k]%j==0)
{
c++;
}
else
{
continue;
}
}
if(c==n)
{
count++;
}
}
Your approach isn't correct from what I gather.
You should take every number from 2 to max(a)[1] and check if it is a factor/divisor of every number in a. If it is, you can increment count.
[1] or even better max(sqrt(a[i]) for a[i] in a) in pseudo-code-ish syntax.
Replace this loop
for(int j=2; j<n; j++)
with
for(int j=2; (j < a[k]); j++)
Note that you can get rid of else part, it is having no effect.

Bad Access C++ Error

Can you help me fix a bad access error please?
Here is the code:
#include <iostream>
using namespace std;
int main() {
int t,tr=0;
cin>>t;
while (tr<t) {
int n;
cin>>n;
int distance=n;
int number;
number=n*n;
int spiral[n][n];
for (int i=0;i<n;i++) {
for (int j=0; j<n; j++) {
spiral[i][j]=0;
}
}
for (int i=0; i<n;) {
for (int j=0; j<n;) {
spiral[i][j]=number;
number=number-1;
//cout<<"ij"<<endl;
for (int k=0; k<distance; k++) {
i++;
spiral[i][j]=number;
number--;
//cout<<"k"<<endl;
}
}
}
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
cout<<spiral[i][j];
}
cout<<endl;
}
tr++;
}
return 0;
}
Bad access is on
spiral[i][j]=number;
Here is the link for the problem but this is not important at the moment. I tried nszmobies but it didn't work so I'm asking you.
This is c++.
Here is the problem.
It seems that you have errors in your loops.
Loop
for (int j=0; j<n;)
looks as it is infinite because j variable isn't changing. Moreover variable i in
spiral[i][j]=number;
in your program can be greater or equal to n.