#include <stdlib.h> // to get NULL main() { short m[]={3,5,7,2,5,1,2,2, 6,5,7,2,
ID: 3726705 • Letter: #
Question
#include <stdlib.h> // to get NULL
main()
{
short m[]={3,5,7,2,5,1,2,2,
6,5,7,2,4,1,3,3,
7,7,3,2,5,7,1,9};
//unsigned char temp, i, j;
unsigned char temp;
unsigned char numElements = sizeof(m)/sizeof(m[0])-1;
//
// array identifier "m" is a pointer to the first
// location of the array m -> m[0]
// this solution is to have two pointers managing the numbers in array m
// create a pointer to "temp", silly, but it is practice
//
// keep pointer types same as to what they point to, notice the "*" and "&"
short *outer_prt = NULL; //represents the outer loop or the number under evaluation
short *inner_prt = NULL; //represents the inner loop or the number under swap evaluation
unsigned char *temp_ptr = &temp; //initalize temp_ptr to idenitifer temp
//for (i=0; i<=numElements-1; i++)
for (outer_prt=m; outer_prt<=&m[numElements-1]; outer_prt++) //incromenting the pointers is same as m[i]
{
//for(j=i+1; j<=numElements; j++)
for(inner_prt = outer_prt+1 ; inner_prt<=&m[numElements]; inner_prt++) //addresses, so compare current address to last
{
//if ( m[i] <= m[j])
if ( *outer_prt <= *inner_prt ) //each pointer moves down the array
{
//temp = m[i];
*temp_ptr = *outer_prt; //
//m[i] = m[j];
*outer_prt = *inner_prt;
//m[j] = temp;
*inner_prt = *temp_ptr; //
}
}
}
}
Q. From the code you have abve, please
Develop a function “Swap()” which will replace the three lines of code, in the scope of the if-statement. The function “Swap()” does not return a value. You define the arguments and function proto-type. Do not use any global defined identifiers. The resulting values in the array “m” will be in descending order.
Explanation / Answer
#include <stdlib.h> // to get NULL
void swap(short *in_prt, short *out_prt) ;
int main()
{
short m[]={3,5,7,2,5,1,2,2,
6,5,7,2,4,1,3,3,
7,7,3,2,5,7,1,9};
unsigned char i;
unsigned char temp;
unsigned char numElements = sizeof(m)/sizeof(m[0])-1;
// array identifier "m" is a pointer to the first
// location of the array m -> m[0]
// this solution is to have two pointers managing the numbers in array m
// create a pointer to "temp", silly, but it is practice
// keep pointer types same as to what they point to, notice the "*" and "&"
short *outer_prt = NULL; //represents the outer loop or the number under evaluation
short *inner_prt = NULL; //represents the inner loop or the number under swap evaluation
unsigned char *temp_ptr = &temp; //initalize temp_ptr to idenitifer temp
for (outer_prt=m; outer_prt<=&m[numElements-1]; outer_prt++) //incromenting the pointers is same as m[i]
{
for(inner_prt = outer_prt+1 ; inner_prt<=&m[numElements]; inner_prt++) //addresses, so compare current address to last
{
if ( *outer_prt <= *inner_prt ) //each pointer moves down the array
{
// calling swap function
swap( inner_prt,outer_prt);
}
}
}
for (i=0; i<=numElements-1; i++)
{
// Printing the data after swap by fuction
printf("%d ",m[i]);
}
}
/*
swap function:
Input: short pointer to in_prt , short pointer to out_prt
return: None
*/
void swap(short *in_prt, short *out_prt) {
short emp;
if(*out_prt <= *in_prt)
emp = *out_prt;
*out_prt = *in_prt;
*in_prt = emp;
}
/*
OUTPUT:
9 7 7 7 7 7 6 5 5 5 5 4 3 3 3 3 2 2 2 2 2 1 1
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.