Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to prioritize, and output to the screen, an array of medical eme

ID: 3849323 • Letter: W

Question

Write a program to prioritize, and output to the screen, an array of medical emergencies at a hospital. As individual emergencies arrive, each is placed into the array, with those of higher priority being put in front of others. Three levels of priority exist, specifically 1 (highest), 2 (medium) and 3 (lowest). When an emergency is inserted into the array, all those of lower priority must be moved back, using a function, to create a space for the higher priority emergency. For example, if the array contains, from front to back, the emergencies {2, 2, 3}, and a priority 1 emergency arrives, then the array, after the insertion, would contain the emergencies {1, 2, 2, 3}. A second function is needed to print, from front to back, the current contents of the array. Your submission should include a screenshot of the execution of the program using the following sequence of operations, which can be directly coded into the program. insert emergency (priority 2) insert emergency (priority 1) print array insert emergency (priority 3) print array insert emergency (priority 2) insert emergency (priority 1) insert emergency (priority 1) print array insert emergency (priority 2) insert emergency (priority 2) print array

Explanation / Answer

Update: C++ version

#include <iostream>

using namespace std;

void print_emergency(int emergencies[], int count)

{

int i;

cout << " The list of emergencies from front to back is " ;

for ( i = 0; i < count; i++)

cout << emergencies[i] ;

cout << " ----------------- " ;

}

int insert_emergency(int emergencies[], int max, int count, int current_emergency)

{

int i, k;

cout << " Inserting emergency "<< current_emergency;

if(count < max)

{

for(i = count - 1; i >= 0; i--)

{

if(current_emergency < emergencies[i]) //keep moving towards front of array as long as current emergency is of higher priority i.e of lower value

continue;

else

break; //break when same priority or lower priority

}

  

if(i == count - 1) // to be insertd at end ?

emergencies[count++] = current_emergency;

else

{

i++;

//move all elements to make room for the newly arrived emergency

for(k = count; k > i; k--)

emergencies[k] = emergencies[k-1];

  

//store the emergency in appropriate place

emergencies[i] = current_emergency;

count++;

}

  

}

else

cout << " Array full.";

return count;

  

}

int main()

{

int emergencies[100]; /* maximum 100 entries*/

int count = 0;

  

count = insert_emergency(emergencies, 100, count, 2);

print_emergency(emergencies, count);

count = insert_emergency(emergencies, 100, count, 2);

count = insert_emergency(emergencies, 100, count, 1);

print_emergency(emergencies, count);

count = insert_emergency(emergencies, 100, count, 3);

print_emergency(emergencies, count);

count = insert_emergency(emergencies, 100, count, 2);

count = insert_emergency(emergencies, 100, count, 1);

count = insert_emergency(emergencies, 100, count, 1);

print_emergency(emergencies, count);

count = insert_emergency(emergencies, 100, count, 2);

count = insert_emergency(emergencies, 100, count, 3);

print_emergency(emergencies, count);

}

==============

Here is the code for the question. Since the programming language is not specified , I have coded in C. In case you need it in c++, post a comment and I shall respond. Output is shown below.

Please don't forget to rate the answe if it helped. Thank you very much.

#include <stdio.h>
void print_emergency(int emergencies[], int count)
{
int i;
printf(" The list of emergencies from front to back is ");
for ( i = 0; i < count; i++)
printf("%d ", emergencies[i]);
printf(" ----------------- ");
}
int insert_emergency(int emergencies[], int max, int count, int current_emergency)
{
int i, k;
printf(" Inserting emergency %d", current_emergency);
if(count < max)
{
for(i = count - 1; i >= 0; i--)
{
if(current_emergency < emergencies[i]) //keep moving towards front of array as long as current emergency is of higher priority i.e of lower value
continue;
else
break; //break when same priority or lower priority
}

if(i == count - 1) // to be insertd at end ?
emergencies[count++] = current_emergency;
else
{
i++;
//move all elements to make room for the newly arrived emergency
for(k = count; k > i; k--)
emergencies[k] = emergencies[k-1];

//store the emergency in appropriate place
emergencies[i] = current_emergency;
count++;
}

}
else
printf(" Array full.");
return count;

}
int main()
{
int emergencies[100]; /* maximum 100 entries*/
int count = 0;

count = insert_emergency(emergencies, 100, count, 2);
print_emergency(emergencies, count);
count = insert_emergency(emergencies, 100, count, 2);
count = insert_emergency(emergencies, 100, count, 1);
print_emergency(emergencies, count);
count = insert_emergency(emergencies, 100, count, 3);
print_emergency(emergencies, count);
count = insert_emergency(emergencies, 100, count, 2);
count = insert_emergency(emergencies, 100, count, 1);
count = insert_emergency(emergencies, 100, count, 1);
print_emergency(emergencies, count);
count = insert_emergency(emergencies, 100, count, 2);
count = insert_emergency(emergencies, 100, count, 3);
print_emergency(emergencies, count);
}

output


Inserting emergency 2
The list of emergencies from front to back is
2
-----------------

Inserting emergency 2
Inserting emergency 1
The list of emergencies from front to back is
1 2 2
-----------------

Inserting emergency 3
The list of emergencies from front to back is
1 2 2 3
-----------------

Inserting emergency 2
Inserting emergency 1
Inserting emergency 1
The list of emergencies from front to back is
1 1 1 2 2 2 3
-----------------

Inserting emergency 2
Inserting emergency 3
The list of emergencies from front to back is
1 1 1 2 2 2 2 3 3
-----------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote