Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with my C code! I have no clear understanding of pointers.My code is for using user input 3-4 digits to subtract the

Please help me with my C code! I have no clear understanding of pointers.My code is for using user input 3-4 digits to subtract the lowest possible number(ascending) from the highest possible number (descending). This loops until the numbers match the prev

Example:

Input:694

964-469=495

Then use that total as new number:

954-459=495

Since that matches the prev then the loop will stop.

Here is my code:

#include

#include

#define BUFFER_SIZE 10

int main()

{

int n,num,i,j;

int placeholder[4];//ones, tenths, hundreths, thousandths

int small,large,dif;

int size=4;

int bplaceholder[4];

printf("Enter a number ");

scanf("%d",&num);

while(num > 0) //do till num greater than 0

{

for ( i = 0; i

int mod = num % 10; //split last digit from number

placeholder[ i ] = mod; /* set element at location i to i + 100 */

bplaceholder[i]=placeholder[i];

num = num / 10; //divide num by 10. num /= 10 also a valid one

if(i>=3 && placeholder[i]==0)

break;

}

/* output each array element's value */

for (j = 0; j

printf("Element[%d] = %d ", j, placeholder[j] );

}

while(1){

small=ascending(placeholder);

large=descending(bplaceholder);

printf("%d %d",small,large);

/*

dif=large-small;

printf("n= %d, Descending= %d, Ascending= %d, Difference= %d ",num,ascending(placeholder),small,dif);

*/

// if the new number is equal to the current number break the loop

if(dif==n)break;

// make n the new number as per given method

n=dif;

}

return 0;

}

}

int ascending(int arr[4])

{

int size=4;

int i, j, temp;

int num;

for(i=0; i

{

/*

* Place currently selected element array[i]

* to its correct place.

*/

for(j=i+1; j

{

/*

* Swap if currently selected array element

* is not at its correct position.

*/

if(arr[i] > arr[j])

{

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

}

/* Print the sorted array */

printf(" Elements of array in ascending order: ");

for(i=0; i

{

printf("%d", arr[i]);

num *= 10;

num += arr[i];

}

printf(" %d", num);

return num;

}

int descending(int barr[4])

{

int size=4;

int i, j, temp;

int number;

for(i=0; i

{

/*

* Place currently selected element array[i]

* to its correct place.

*/

for(j=i+1; j

{

/*

* Swap if currently selected array element

* is not at its correct position.

*/

if(barr[i]

{

temp = barr[i];

barr[i] = barr[j];

barr[j] = temp;

}

}

}

/* Print the sorted array */

printf(" Elements of array in descending order: ");

for(i=0; i

{

printf("%d", barr[i]);

number *= 10;

number += barr[i];

}

printf(" %d ", number);

return number;

}

And here is the output

image text in transcribed

> clang-7 -pthread -lm -o main main > clang-7 -pthread -lm -o main main.c main.c:32:12: warning: implicit declaration of function 'ascending' is invalid in 099 [-Wimplicit-function-declaration] small=ascending(placeholder); A main.c:33:11: warning: implicit declaration of function 'descending 'is invalid in 099 [-Wimplicit-function-declaration] large=descending(bplaceholder); A 2 warnings generated. } ./main Enter a number 495 Element[0] 5 Element[1] = 9 Element[2] 4 Element[3] = 0 Elements of array in ascending order: 0459 459 Elements of array in descending order: 9540 4599540 459 4599540

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Databases With PostgreSQL From Novice To Professional

Authors: Richard Stones, Neil Matthew

2nd Edition

1590594789, 978-1590594780

More Books

Students also viewed these Databases questions

Question

What is a voucher?

Answered: 1 week ago

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago