I have a running code of a dining philosophers problem but it has a logic problem where it loops right after it asks how many of the philosophers are hungry thus unable to calculate how exactly the philosophers are going to eat below is the code: #include #include #include #include #include     void one(void); void two(void); void exit(); int tph, philname[20], howhung, hu[20], cho;//{}[] int main(void) {     int i, status[10],philname[10],howhung;     printf("\n\nDINING PHILOSOPHER PROBLEM");     printf("\nEnter the total no. of philosophers: ");     scanf("%d",&i);     for(i=0;i=1&&abs(hu[i]-hu[j])!=tph-1)                 {                     printf("\n\n combination %d \n", (s+1));                     t=hu[i];                     r=hu[j];                     s++;                     if (r-t==1) continue;                     printf("\nP %d and P %d are granted to eat", t, r);                     for(x=0;x

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

I have a running code of a dining philosophers problem but it has a logic problem where it loops right after it asks how many of the philosophers are hungry thus unable to calculate how exactly the philosophers are going to eat

below is the code:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <string.h>

 

 


void one(void);
void two(void);
void exit();
int tph, philname[20], howhung, hu[20], cho;//{}[]

int main(void)
{
    int i, status[10],philname[10],howhung;
    printf("\n\nDINING PHILOSOPHER PROBLEM");
    printf("\nEnter the total no. of philosophers: ");
    scanf("%d",&i);
    for(i=0;i<tph;i++)
        {
            philname[i] = (i+1);
            status[i]=1;
        }
        printf("How many are hungry : ");
        scanf("%d", &howhung);
        if(howhung==tph)
            {
                printf("\nAll are hungry..\nDead lock stage will occur");
                printf("\nExiting..");
            }
    else
        {
            for(i=0;i<howhung;i++)
            {
                printf("Enter philosopher %d position: ",(i+1));
                scanf("%d", &hu[i]);
                status[hu[i]]=2;
            }
            do
            {
                printf("\n\n 1. One can eat at a time\t 2.Two can eat at a time\t 3.Exit\nEnter your choice:");
                scanf("%d", &cho);
                switch(cho)
                {
                    case 1:  one();
                    break;
                    case 2:  two();
                    break;
                    case 3: exit(0);
                    default: printf("\nInvalid option..");

                }
            }
            while(1);
        }
}

void one(void)
{
    int pos=0, x, i;
    printf("\nAllow one philosopher to eat at any time\n");
    for(i=0;i<howhung; i++, pos++)
        {
            printf("\nP %d is granted to eat", hu[pos]);
            for(x=pos+1;x<howhung;x++)
                printf("\nP %d is waiting", hu[x]);

        }
}

void two(void)
{
    int i, j, s=0, t, r, x;
    printf("\n Allow two philosophers to eat at the same time\n");
    for(i=0;i<howhung;j++)
        {
            for(j=i+1;j<howhung;j++)
            {
                if(abs(hu[i]-hu[j])>=1&&abs(hu[i]-hu[j])!=tph-1)
                {
                    printf("\n\n combination %d \n", (s+1));
                    t=hu[i];
                    r=hu[j];
                    s++;
                    if (r-t==1) continue;
                    printf("\nP %d and P %d are granted to eat", t, r);
                    for(x=0;x<howhung;x++)
                    {
                        if((hu[x]!=t)&&(hu[x]!=r))
                            printf("\nP%d is waiting", hu[x]);
                    }
                }
            }

        }
}

current ouptut:

 

DINING PHILOSOPHER PROBLEM
Enter the total no. of philosophers: 5
How many are hungry : 2
Enter philosopher 1 position: 1
Enter philosopher 2 position: 2
1. One can eat at a time
Enter your choice: 1
Allow one philosopher to eat at any time
2. Two can eat at a time
1. One can eat at a time
Enter your choice: 2
Allow two philosophers to eat at the same time
1. One can eat at a time
Enter your choice:
2. Two can eat at a time
2. Two can eat at a time
3. Exit
3. Exit
3. Exit
Transcribed Image Text:DINING PHILOSOPHER PROBLEM Enter the total no. of philosophers: 5 How many are hungry : 2 Enter philosopher 1 position: 1 Enter philosopher 2 position: 2 1. One can eat at a time Enter your choice: 1 Allow one philosopher to eat at any time 2. Two can eat at a time 1. One can eat at a time Enter your choice: 2 Allow two philosophers to eat at the same time 1. One can eat at a time Enter your choice: 2. Two can eat at a time 2. Two can eat at a time 3. Exit 3. Exit 3. Exit
Expert Solution
steps

Step by step

Solved in 2 steps with 4 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY