Two C functions sortInDescend_1() and sortInDescend_2() are given. sortInDescend_1() is used for call-by-value and sortI

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Two C functions sortInDescend_1() and sortInDescend_2() are given. sortInDescend_1() is used for call-by-value and sortI

Post by answerhappygod »

Two C
functions sortInDescend_1() and sortInDescend_2() are
given. sortInDescend_1() is used for
call-by-value and sortInDescend_2() is
used for call-by-reference.

Please write a C program which does the following:
Given that variables a1 =
3, a2 =
1, a3 = 2, one example output is shown
below.
For example:
code
#include<stdio.h>
#include<string.h>
void swap(int *i,int*j)
{
int temp;
temp=*i;
*i=*j;
*j=temp;
}
void sortInDescend_1(int var1, int var2, int var3)
{
if(var1<var2){
swap(&var1,&var2);
}
if(var1<var3){
swap(&var1,&var3);
}
if(var2<var3){
swap(&var2,&var3);
}
}
void sortInDescend_2(int *var1, int *var2, int *var3)
{
if(*var1<*var2){
swap(var1,var2);
}
if(*var1<*var3){
swap(var1,var3);
}
if(*var2<*var3){
swap(var2,var3);
}
}
int main()
{
int a1,a2,a3;
scanf("%d",&a1);
scanf("%d",&a2);
scanf("%d",&a3);
sortInDescend_1(a1,a2,a3);//passing the values as it is
printf("a1: %d a2: %d a3: %d\n",a1,a2,a3);
sortInDescend_2(&a1,&a2,&a3);//passing the references
of the values, & symbol passes the address of the
variable
printf("a1: %d a2: %d a3: %d\n",a1,a2,a3);
return 0;
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply