input

M

mitchellpal

guys i need help here, what do i do to my code inorder to prompt the
user to enter any 10 integer values then display them in ascending
order?

#include<stdio.h>
#define SIZE 10

main()
{
int i;
int pass;//pass counter
int hold;//
long a[SIZE]={12,451,56,205,450,78,90,67,45,100};


//printf("Enter 10 integer values\n\n");

printf("\nElements in original array\n\n");

for(i=0;i<SIZE;i++){
printf("%4d",a);
}

printf("\n\n");

//pass counter
for(pass=0;pass<SIZE;pass++){

for(i=0;i<SIZE-1;i++){

if(a>a[i+1]){
hold=a;
a=a[i+1];
a[i+1]=hold;
}//endif
}//end inner for
}//end outer for

printf("Elements in ascending order\n\n");

for(i=0;i<SIZE;i++){
printf("%4d",a);
}//endfor
printf("\n\n");
}
the program is working as it is...... my email address is real too.
 
V

Vladimir S. Oka

guys i need help here, what do i do to my code inorder to prompt the
user to enter any 10 integer values then display them in ascending
order?

#include<stdio.h>
#define SIZE 10

main()
{
int i;
int pass;//pass counter
int hold;//
long a[SIZE]={12,451,56,205,450,78,90,67,45,100};


//printf("Enter 10 integer values\n\n");

printf("\nElements in original array\n\n");

for(i=0;i<SIZE;i++){
printf("%4d",a);
}

printf("\n\n");

//pass counter
for(pass=0;pass<SIZE;pass++){

for(i=0;i<SIZE-1;i++){

if(a>a[i+1]){
hold=a;
a=a[i+1];
a[i+1]=hold;
}//endif
}//end inner for
}//end outer for

printf("Elements in ascending order\n\n");

for(i=0;i<SIZE;i++){
printf("%4d",a);
}//endfor
printf("\n\n");
}
the program is working as it is...... my email address is real too.


Your program is working by sheer luck! You use an int as a temporary
storage to swap two longs. This may not give you the results you expect
(think what happens if long is wider than int, and your numbers exceed
MAXINT).

To prompt user, use printf() to output the prompt, fgets() (NOT gets())
to collect the line entered, and sscanf() to parse it for number
entered. Wrap this in a nice loop, and you're all set...

Cheers

Vladimir

PS
My e-mail address is real as well, but that's highly off-topic in
c.l.c. ;-)
 
V

Vladimir S. Oka

let me try this.......thanks

Try what?

Please quote what you're replying to. There's going to be people
who can't see the original post. If using Google:

"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>

Cheers

Vladimir
 
E

Emmanuel Delahaye

(e-mail address removed) a écrit :
guys i need help here, what do i do to my code inorder to prompt the
user to enter any 10 integer values then display them in ascending
order?

In some loop, use a fgets() to accept a line of text and strtol() to
convert the text to a number. Store the values in some array.

Then, sort the array and display it.

Do your best an post your code if you are stuck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top