Please help review my code, not sure its working as intended.... its in C! Objective Give practice with Recursion in C. Give practice with Backtracking in C. Give practice with Stacks in C. Input will begin with a line containing 2 integers, n and c (1 ≤ n ≤ 20; 0 ≤ c ≤ n choose 2), representing the number of animal exhibits and the number of constraints. The following c lines will each contain a constraint description in the form of 3 space separated integers, f, s, and d

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

Please help review my code, not sure its working as intended.... its in C!

Objective

Give practice with Recursion in C.
Give practice with Backtracking in C.
Give practice with Stacks in C.

Input will begin with a line containing 2 integers, n and c (1 ≤ n ≤ 20; 0 ≤ c ≤ n choose 2),
representing the number of animal exhibits and the number of constraints. The following c lines
will each contain a constraint description in the form of 3 space separated integers, f, s, and d
(1 ≤ f, s ≤ n; f ≠ s; 0 ≤ c ≤ n - 2), representing the index of the first animal, the index of the
second animal, and the exact number of cages that should be in between those 2 animals.
For 7 of the 10 cases each animal will be in at most 1 constraint.

Output should contain 1 line. In the event that a solution exists, you should print out n space
separated integers representing the animal index given in order from the first cage to the last. If
there is no solution the output should be only the phrase “No Solution.” (quotes for clarity).

Sample Input Sample output

4 2

1 2 1

3 4 0

No Solution
Sample Input  Sample Output
5 4
1 2 1
1 3 1
1 5 0
2 5 2
2 4 1 5 3

Code below:

#include<stdio.h>
 
#include<limits.h>
 
int result=INT_MAX;
 
void swap(int *l, int *r){
 
int temp=*l;
 
*l=*r;
 
*r=temp;
 
 
 
}
 
 
 
int min(int l,int r){
 
if(l<=r) return l;
 
else return r;
 
 
 
} int max(int l,int r){
 
if(l<=r) return r;
 
else return l; } // recursive function
 
void recursive(int ch[],int position,int N,int noise[N][N]){
 
if(position==N){
 
int temp=0;
 
for(int i=0;i<N;i++){
 
for(int j=max(0,i-2);j<=min(N-1,i+2);j++){
 
temp+=noise[ch[i]-1][ch[j]-1];
 
}
 
 
 
}
 
result=min(result,temp);
 
return; }
 
else{ // Using backtracking
 
for(int j=position;j<N;j++){
 
swap(&ch[j],&ch[position]);
 
recursive(ch,position+1,N,noise);
 
swap(&ch[j],&ch[position]);
 
 
 
}
 
 
 
} }
 
int main(){
 
 
 
int N;
 
printf("Enter the two numbers : ");
 
scanf("%d",&N);
 
printf(" Enter the numbers : ");
 
int noise[N][N];
 
for(int i=0;i<N;i++){
 
for(int j=0;j<N;j++){
 
scanf("%d",&noise[i][j]);
 
 
 
}
 
 
 
}
 
int ch[N];
 
int position=0;
 
for(int i=1;i<=N;i++){
 
ch[i-1]=i;
 
 
 
} // call the recursive function
 
recursive(ch,0,N,noise);
 
printf("Minimum noise generated is : %d",result);
 
}
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Computational Systems
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education