Reputation:
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%10;
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.
Upvotes: 0
Views: 873
Reputation: 38
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("%99[^\n]", digits);
/*Simply using scanf("%99s", digits) should work too.
No need to protect from the new line since we aren't doing any other calls to scanf().*/
Note on scanf(): As hinted in the comments to this answer, you may get a string overflow if you do not specify the maximum width of your string; instead of scanf("%[^\n]", digits)
, consider scanf("%99[^\n], digits)
. The width specifier should be the size of the length of your buffer minus one. (We need to make it one byte shorter so scanf()
can add the string terminator (null character) at the end of our buffer, had we used all the space in it). You may also want to check if scanf()
was successful by checking the return value:
/* We only have one string in our arguments, so scanf() should return 1.
If anything goes wrong, terminate the program*/
int numOfAssignments = scanf("%99[^\n]", digits);
if (numOfAssignments != 1) return 1;
// return numOfAssignments; may also be a good idea.
Consider using fgets() instead, except you would have to manually remove the new line character.
========= End of Note =========
Let's make a define for the max number of digits, since we will be using it on most of the code: #define MAX_SIZE 100
Then, since you need to print the initial number at the end of the program, you were trying to copy it to a variable:
original = num;
Problem here is, that int num[100]
(now char digits[100])
is an array, and you were just copying it's address to original
. To copy the whole number the user had given the program you would need to copy each element of the array, one by one (or we could use memcpy()
instead):
for (i = 0; i < MAX_SIZE && i != '\0'; ++i){
original[i] = digits[i];
}
I will actually use the digits
array as my "original number" and will declare two extra arrays maxDigits[MAX_SIZE]
and minDigits[MAX_SIZE]
to hold the digits in ascending (min) and descending (max) order. Next, I will initialize either of the arrays:
minDigits[MAX_SIZE] = { '\0' };
maxDigits[MAX_SIZE] = { '\0' };
//Stop copying values when the null character is found, also count the number of digits.
while (digits[count] != '\0') {
maxDigits[count] = digits[count++];
}
Now, since this is supposed to hold the digits in descending order, we have to sort the array (this could probably have been done in the previous step, but this is about all the brainpower I have at 1:00 AM).
//Look up Insertion Sort to learn about this sorting algorithm
int buffer;
for (i = 1; i < count; ++i) {
buffer = maxDigits[i];
j = i - 1;
while (j >= 0 && maxDigits[j] < buffer) {
maxDigits[j + 1] = maxDigits[j];
j = j - 1;
}
maxDigits[j + 1] = buffer;
}
Now that maxDigits
is sorted we need to sort minDigits
.
This one should be easy, since all we need is to flip maxDigits
:
for (i = 0; i < count; ++i) {
minDigits[i] = maxDigits[count - i - 1];
}
Of course, we need to calculate the difference between the maximum and minimum value of reordered digits, so we should convert maxDigits
and minDigits
to integers in order to do math with them:
/*This will multiply each digit with it's position value and add it together
(hundreds + tens + ones...)*/
int multiplier = 1;
for (i = count - 1; i >= 0; --i, multiplier *= 10) {
max += (maxDigits[i] - '0') * multiplier;
min += (minDigits[i] - '0') * multiplier;
}
/*Since the digits we stored are actually characters from '0' to '9' and not
integers 0 to 9, we need to substract the value of '0' or else our min and max values will be off.*/
Finally, we just have to output the results:
printf("The difference between largest(%d) and smallest(%d) of the integer %s is %d.\n", max, min, digits, max - min);
Upvotes: 1
Reputation: 12322
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.
Upvotes: 1