Please review my code here in C... there are errors occurring and I'm not sure why. Input will begin with a line containing 2 integers, n and e (1 ≤ n ≤ 500,000; 1 ≤ e ≤ 500,000), representing the number of orangutans and the number of events. The following e lines each contain a single event description. An event description will be one of the following three, ●1 n a - which represents a feeding, where n is the name of the orangutan at the time of the feeding and a represents the

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

Please review my code here in C... there are errors occurring and I'm not sure why.

Input will begin with a line containing 2 integers, n and e (1 ≤ n ≤ 500,000; 1 ≤ e ≤ 500,000), representing
the number of orangutans and the number of events. The following e lines each contain a single event
description. An event description will be one of the following three,
●1 n a - which represents a feeding, where n is the name of the orangutan at the time of the feeding
and a represents the amount of mangos given to the orangutan. (1 ≤ a ≤ 100)
●2 o n - which represents a name change, where o represents the old name of the orangutan and n
represents the new name.
●3 p - which represents an inquiry as to the number of mangos eaten by orangutans whose name
starts with the sequence of characters p.
Each name and character sequence will contain at most 20 characters. All names will be strictly
uppercase Latin characters (‘A’ through ‘Z’). No name will contain whitespace. No orangutan will
change their name to an already existing name.
Assume that no orangutan has eaten prior to the given events

Output: For each input event that represents an inquiry print the number of mangos eaten by orangutans whose
name starts with the given input character sequence.

Sample input:

10 6
1 BOB 5
1 BETTY 3
3 B
3 ALICE
2 BETTY ALICE
3 B

Sample output:

8

0

5

Sample input:

4 8
1 WILLIAM 4
1 WILL 6
3 WILLI
1 WILLIAN 9
1 WILLY 10
2 WILL MATT
1 WILLIAN 2
3 WILL

Sample output:

4

25

 

Code below:

#include<stdio.h>

#include<limits.h>

// ans variable will store the minimum noise generated

int ans=INT_MAX;

// swap function

void swap(int *a, int *b){

int tmp=*a;

*a=*b;

*b=tmp;

}

// min and max function

int min(int a,int b){

if(a<=b) return a;

else return b;

}

int max(int a,int b){

if(a<=b) return b;

else return a;

}

// recursive function generates all possible arrangement of the animals

void recursive(int ch[],int pos,int n,int noise[n][n]){

// for each possible arrangement

if(pos==n){

// calculate noise generated for this arrangement

int tmp=0;
for(int i=0;i<n;i++){

for(int j=max(0,i-2);j<=min(n-1,i+2);j++){

tmp+=noise[ch[i]-1][ch[j]-1];

}

}

// recheck if ans variable stores minimum possible value of noise generation

ans=min(ans,tmp);

return;

}

else{

// Using backtracking for generated all permutation

for(int j=pos;j<n;j++){

swap(&ch[j],&ch[pos]);

recursive(ch,pos+1,n,noise);

// backtracking

swap(&ch[j],&ch[pos]);

}

}

}

int main(){

// taking input from the user

int n;

scanf("%d",&n);

int noise[n][n];

for(int i=0;i<n;i++){
for(int j=0;j<n;j++){

scanf("%d",&noise[i][j]);

}

}

// ch array will store the position of the animals

int ch[n];

int pos=0;

for(int i=1;i<=n;i++){

ch[i-1]=i;

}

// calling our recursive function

recursive(ch,0,n,noise);

// output the minimum possible answer

printf("The minimum noise generated is : %d",ans);

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Hiring Problem
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
  • SEE MORE 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