In need of the ALGORITHM for the following code: void dec_sort (int * p, string
ID: 3706781 • Letter: I
Question
In need of the ALGORITHM for the following code:
void dec_sort (int * p, string * q, int n)
{
for (int i = 0; i < n; ++i)
for (int j = 0; j < n - i - 1; ++j)
if (p[j] < p[j + 1])
{
int temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
string s = q[j];
q[j] = q[j+1];
q[j+1] = s;
}
}
void ac_sort(int *p,string *q,int n)
{
int loc,min;
for(int i = 0; i < n-1; i++)
{
min=p[i];
loc=i;
for(int j = i+1; j < n; j++)
{
if(min > p[j])
{
min = p[j];
loc = j;
}
}
int temp = p[i];
p[i] = p[loc];
p[loc] = temp;
string s = q[i];
q[i] = q[loc];
q[loc] = s;
}
}
int elevator(int *p,string *q,int n)
{
int sum = 0;
int pcount = 0;
for(int i = 0; i < n; i++)
{
if(sum+p[i] <= 1100)
{
sum = sum+p[i];
pcount++;
}
else
break;
}
cout << "Total number of people that can get on the elevator is: " << pcount << endl;
cout << "Total weight: " << sum << endl;
return pcount;
}
void print(int *p,string *q,int n)
{
cout << "Their names and weight: ";
for(int i = 0; i < n; i++)
{
cout<<q[i] << " " << p[i]<<endl;
}
}
int main()
{
{
int ar1[]={30,150,305,225,135,160,80,200,165,90,100,120};
int size=sizeof(ar1)/sizeof(ar1[0]);
string ar2[]={"Anne","Bob","Ralph","Tim","Barbara","Jane","Steve","Tom","Mike",
"Shirley","Pam","Frank"};
int n;
int perf[3];
print (ar1, ar2, size);
n = elevator(ar1, ar2, size);
print (ar1, ar2, n);
perf[0]=n;
dec_sort(ar1, ar2, size);
cout << " After sorting in decending order ";
print (ar1, ar2, size);
n = elevator(ar1, ar2, size);
print (ar1, ar2, n);
perf[1] = n;
ac_sort (ar1, ar2, size);
cout << " After sorting in acsending order ";
print (ar1, ar2, size);
n = elevator(ar1, ar2, size);
print (ar1, ar2, n);
perf[2] = n;
if(perf[0] >= perf[1] && perf[0] >= perf[2])
cout << " First method is best ";
else if(perf[1] >= perf[0] && perf[1] >= perf[2])
cout << " Decsending sort method is best ";
else
cout << " Acsending sort method is best ";
}
return 0;
}
Explanation / Answer
Algorithm:
start
Declare array with elements as ar1[]={30,150,305,225,135,160,80,200,165,90,100,120};
FInd size of ar1
Declare array with strings as string ar2[]={"Anne","Bob","Ralph","Tim","Barbara","Jane","Steve","Tom","Mike",
"Shirley","Pam","Frank"};
print(ar1, ar2, size);
call the function elevator(ar1, ar2, size);
called function elevator(int *p,string *q,int n)
Initialise sum = 0,pcount = 0;
for loop 0 to n:
if(sum+p[i] <= 1100)
sum = sum+p[i];
pcount++;
end if
else
break;
end else
end for
print Total number of people that can get on the elevator.
print Total weight.
return pcount;
end function
print (ar1, ar2, n);
perf[2] = n;
if(perf[0] >= perf[1] && perf[0] >= perf[2])
print First method is best.
end if
else if(perf[1] >= perf[0] && perf[1] >= perf[2])
print Decsending sort method is best.
end else if
else
print Acsending sort method is best
end else
print (ar1, ar2, n);
perf[0]=n;
calling function dec_sort(ar1, ar2, size);
called function void dec_sort (int * p, string * q, int n)
for (int i = 0; i < n; ++i)
for (int j = 0; j < n - i - 1; ++j)
if (p[j] < p[j + 1])
int temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
string s = q[j];
q[j] = q[j+1];
q[j+1] = s;
end if
end function
print After sorting in decending order.
print (ar1, ar2, size);
n = elevator(ar1, ar2, size);
called function elevator(int *p,string *q,int n)
Initialise sum = 0,pcount = 0;
for loop 0 to n:
if(sum+p[i] <= 1100)
sum = sum+p[i];
pcount++;
end if
else
break;
end else
end for
print Total number of people that can get on the elevator.
print Total weight.
return pcount;
end function
print (ar1, ar2, n);
perf[1] = n;
calling function ac_sort (ar1, ar2, size);
called function ac_sort(int *p,string *q,int n)
for(int i = 0; i < n-1; i++)
min=p[i];
loc=i;
for(int j = i+1; j < n; j++)
if(min > p[j])
min = p[j];
loc = j;
end if
end for
int temp = p[i];
p[i] = p[loc];
p[loc] = temp;
string s = q[i];
q[i] = q[loc];
q[loc] = s;
end for
end function
print After sorting in acsending order.
print (ar1, ar2, size);
n = elevator(ar1, ar2, size);
called function elevator(int *p,string *q,int n)
Initialise sum = 0,pcount = 0;
for loop 0 to n:
if(sum+p[i] <= 1100)
sum = sum+p[i];
pcount++;
end if
else
break;
end else
end for
print Total number of people that can get on the elevator.
print Total weight.
return pcount;
end function
print (ar1, ar2, n);
perf[2] = n;
if(perf[0] >= perf[1] && perf[0] >= perf[2])
print First method is best.
end if
else if(perf[1] >= perf[0] && perf[1] >= perf[2])
print Decsending sort method is best
end else if
else
print Acsending sort method is best
end else
end program
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.