I am trying to find the largest and smallest possible value from an integer which is taken as an input from the user. For example - If the user enters the number 3859428, I want to find 9885432 and 2345889.
Here is my code -:
#include <stdio.h>
int main()
{
int num[100], original, remainder, min=0, max=0, count=0, i, j;
printf("Enter a number : ");
scanf("%[^\n]", num);
original = num;
i=0;
while(num!=0)
{
num/=10;
num[i]=num;
count ;
i ;
}
for(i=0; i<count, num!=0; i )
{
for(j=i 1; j<count, num!=0; j )
{
if(num[i]<num[j])
{
min=min*10 num[i];
num/=10;
}
}
}
while(min!=0)
{
remainder=min;
max=max*10 remainder;
min/=10;
}
printf("The difference between largest(%d) and smallest(%d) of the integer %d is %d.\n", max, min, original, max-min);
return 0;
}
I am getting 2 errors -
"assignment to expression with array type"
on line 16 and 29, and 2 warnings -
"assignment to 'int' from 'int*' makes integer from pointer without a cast"
on line 10 and 17.
I don't know what these mean and how to accomplish my task. Any help would be great.
Thank You.
CodePudding user response:
What you are trying to do can be done much simpler. There is no need to parse the input as a number at all, no need to convert it into binary form and then extract decimal digits from it to reorder them.
The same result can be achieved simply by reading a string and sorting it for the minimal number (potentially drop leading '0') and reversing it for the maximum.
CodePudding user response:
I believe what you are trying to achieve is to input some number, and then reorder it's digits to achieve a maximum and minimum value:
int num[100];
scanf("%[^\n]", num);
First of all I would recommend either setting scanf() to parse the input to an integer:
int number;
scanf("%d", &number);
Or using an string (array of chars) to hold your digits one by one. (I'm going to continue this way).
//Initialize all elements to '\0', in order to know where the user input ended.
char digits[100] = {'\0'};
scanf("