A program where u have a 10 input number and enbles u to choose wether the output is in asecnding or descending order. Thanks in advance!!

Recommended Answers

All 7 Replies

Hmm..

What have you done so far?

Member Avatar for jencas

Use std::vector<int> and std::sort() with the appropriate predicate depending on the desired order

You have given no details at all, you didn't even ask a question or describe a problem :icon_confused: , exactly what do you need help with?

You have given no details at all, you didn't even ask a question or describe a problem :icon_confused: , exactly what do you need help with?

Deduction: He wants us to do it for him.

Conclusion: Wait for him to show some effort, or if he's lucky some chump will do it for him =P

Ok! Now i have done it. The prblem is that i dont know how to do a switch.

I mean after the program is executed and finish, How do I enable it to ask the user if he wants another try. If yes, the prog starts again and if no, it exits.

O and 1 more thing, the prog (window) closes after i had made the choice (ascending or descending).

Heres my Code:

#include <stdio.h>

int main(void)
{
int a[10], i=0, j=0, n, t;

printf ("\n Enter the no. of elements: ");
scanf ("%d", &n);
printf ("\n");

for (i = 0; i <n; i++)
{
printf ("\n Enter the %dth element: ", (i+1));
scanf ("%d", &a[i]);
}

for (j=0 ; j<(n-1) ; j++)
{
for (i=0 ; i<(n-1) ; i++)
{
if (a[i+1] < a[i])
{
t = a[i];
a[i] = a[i + 1];
a[i + 1] = t;
}
}
}

printf ("\n Ascending order: ");
for (i=0 ; i<n ; i++)
{
printf (" %d", a[i]);
}

printf ("\n Descending order: ");
for (i=n ; i>0 ; i--)
{
printf (" %d", a[i-1]);
}

return 0;
}

I'm very new to programming myself, but that is a pure C and not really a C++ application, right?

Ok! Now i have done it. The prblem is that i dont know how to do a switch.

I mean after the program is executed and finish, How do I enable it to ask the user if he wants another try. If yes, the prog starts again and if no, it exits.

O and 1 more thing, the prog (window) closes after i had made the choice (ascending or descending).

Heres my Code:

#include <stdio.h>

int main(void)
{
int a[10], i=0, j=0, n, t;

printf ("\n Enter the no. of elements: ");
scanf ("%d", &n);
printf ("\n");

for (i = 0; i <n; i++)
{
printf ("\n Enter the %dth element: ", (i+1));
scanf ("%d", &a);
}

for (j=0 ; j<(n-1) ; j++)
{
for (i=0 ; i<(n-1) ; i++)
{
if (a[i+1] < a)
{
t = a;
a = a;
a = t;
}
}
}

printf ("\n Ascending order: ");
for (i=0 ; i<n ; i++)
{
printf (" %d", a);
}

printf ("\n Descending order: ");
for (i=n ; i>0 ; i--)
{
printf (" %d", a[i-1]);
}

return 0;
}

put the whole code in a do-while loop and have one input that asks the user if he wants to continue.check in the while condition if the choice is 'y'

#include <stdio.h>

int main(void)
{
int a[10], i=0, j=0, n, t;
char a,choice;

do{

printf ("\n Enter the no. of elements: ");
scanf ("%d", &n);
printf ("\n");

for (i = 0; i <n; i++)
{
printf ("\n Enter the %dth element: ", (i+1));
scanf ("%d", &a[i]);
}

for (j=0 ; j<(n-1) ; j++)
{
for (i=0 ; i<(n-1) ; i++)
{
if (a[i+1] < a[i])
{
t = a[i];
a[i] = a[i + 1];
a[i + 1] = t;
}
}
}
//Check for this part for switching
cout<<"Enter A for ascending and D for descending";
cin>>choice;
switch(choice)
{case A:
printf ("\n Ascending order: ");
for (i=0 ; i<n ; i++)
{
printf (" %d", a[i]);
}
break;
case D:
printf ("\n Descending order: ");
for (i=n ; i>0 ; i--)
{
printf (" %d", a[i-1]);
}
break;
}
//check for this part for continuing
cout<<"Continue";
cin>>a;
}while(a=='y'||a=='Y');
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.