To arrange n numbers in descending order ?
-
i tries but the rest lies here. :sigh:
#include
#includeint main(){
int num\[50\], i=0, largest, count =0; int k, j; printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n"); do{ scanf("%d", &num\[i\]); i++; count++; }while(num\[i-1\]!=0); while(count>=0){ largest = 0; for(j=0; j<=count-1; j++){ if(num\[j\] > largest){ largest = num\[j\]; j=k; } } printf("%d", largest); for(j=k; j<=count-1; j++){ num\[j\] = num\[j+1\]; } count = count - 2; } return 0;
}
-
i tries but the rest lies here. :sigh:
#include
#includeint main(){
int num\[50\], i=0, largest, count =0; int k, j; printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n"); do{ scanf("%d", &num\[i\]); i++; count++; }while(num\[i-1\]!=0); while(count>=0){ largest = 0; for(j=0; j<=count-1; j++){ if(num\[j\] > largest){ largest = num\[j\]; j=k; } } printf("%d", largest); for(j=k; j<=count-1; j++){ num\[j\] = num\[j+1\]; } count = count - 2; } return 0;
}
Maybe take a look at this[^] thread.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
i tries but the rest lies here. :sigh:
#include
#includeint main(){
int num\[50\], i=0, largest, count =0; int k, j; printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n"); do{ scanf("%d", &num\[i\]); i++; count++; }while(num\[i-1\]!=0); while(count>=0){ largest = 0; for(j=0; j<=count-1; j++){ if(num\[j\] > largest){ largest = num\[j\]; j=k; } } printf("%d", largest); for(j=k; j<=count-1; j++){ num\[j\] = num\[j+1\]; } count = count - 2; } return 0;
}
So what's the problem? Can we assume you've stepped through the code, line by line, using the debugger (to see the value of each variable along the way)?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
i tries but the rest lies here. :sigh:
#include
#includeint main(){
int num\[50\], i=0, largest, count =0; int k, j; printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n"); do{ scanf("%d", &num\[i\]); i++; count++; }while(num\[i-1\]!=0); while(count>=0){ largest = 0; for(j=0; j<=count-1; j++){ if(num\[j\] > largest){ largest = num\[j\]; j=k; } } printf("%d", largest); for(j=k; j<=count-1; j++){ num\[j\] = num\[j+1\]; } count = count - 2; } return 0;
}
You need to stop writing code for a while and spend more time thinking about the problem. What steps are needed to accomplish your goal? Write them down on paper, line by line to understand what needs to be done at each step. I already showed you how to find the largest number in a sequence and you are still making it more complicated. As you read in the integers in the first loop you can check for the largest as you store them, you do not need a second loop. You also need to look at the statement in the second loop:
largest = num\[j\]; j=k;
What is the value of k at this point? And in the final loop you are just moving numbers, with no regard to whether they are in any order.
-
i tries but the rest lies here. :sigh:
#include
#includeint main(){
int num\[50\], i=0, largest, count =0; int k, j; printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n"); do{ scanf("%d", &num\[i\]); i++; count++; }while(num\[i-1\]!=0); while(count>=0){ largest = 0; for(j=0; j<=count-1; j++){ if(num\[j\] > largest){ largest = num\[j\]; j=k; } } printf("%d", largest); for(j=k; j<=count-1; j++){ num\[j\] = num\[j+1\]; } count = count - 2; } return 0;
}
What you basically need to do is to sort the array in descending order. There are several sorting algorithms. This the first google search link I found for sorting - Sorting Arrays[^] Go through them, try them and understand them.
«_Superman_» _I love work. It gives me something to do between weekends.
_Microsoft MVP (Visual C++) (October 2009 - September 2013)
-
i tries but the rest lies here. :sigh:
#include
#includeint main(){
int num\[50\], i=0, largest, count =0; int k, j; printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n"); do{ scanf("%d", &num\[i\]); i++; count++; }while(num\[i-1\]!=0); while(count>=0){ largest = 0; for(j=0; j<=count-1; j++){ if(num\[j\] > largest){ largest = num\[j\]; j=k; } } printf("%d", largest); for(j=k; j<=count-1; j++){ num\[j\] = num\[j+1\]; } count = count - 2; } return 0;
}
#include
#define N 50
int main()
{
int num[N], i=0, count =0;printf("Enter 0 to exit entering integers.\n\nEnter your integers:\n");
do
{
if( scanf("%d", &num[i]) != 1) break;
++i;
count++;} while ( i
As an alternative, tou might use
qsort
:#include
#include#define N 50
int comp(const void * a, const void *b)
{
return *(int *)b - *(int*)a;
}int main()
{
int num[N], i=0, count =0;printf("Enter 0 to exit entering integers.\n\nEnter your integers:\n");
do
{
if( scanf("%d", &num[i]) != 1) break;
++i;
count++;} while ( i