Re: Writing a program to sort numbers - HELP!
- From: Barry Schwarz <schwarzb@xxxxxxxxx>
- Date: Sat, 30 Jul 2005 15:13:46 -0000
On Thu, 28 Jul 2005 06:50:40 -0000, "webhead"
<jeffreybrown2@xxxxxxxxxxxx> wrote:
>Hello,
>
>I'm working on a program for an assignment where we get practice using:
>
>pointers, atoi() function and gets() function
You may not have a choice but strtol is preferred to atoi and fgets is
preferred to gets
>
>Below is a program I'm trying to write that sorts two numbers (yes, two
>numbers) provided by the user. We have to scan the user's response
>into a string variable using gets() and convert the user's response to
>an integer using atoi(). Then we using a sort function to sort the
>integers from smallest to largest and then display them in that order.
>
>We're asked to return value 0 if the integers are already in sorted
>order and return value 1 if the integers had to be swapped. (not sure
>what is meant by that)
Why don't you ask your instructor what he meant. I would expect he
wants main to return these values to the OS.
>
>Below is a draft of the program so far. I ran it through the compiler,
>and, of course, I have many errors. Not sure what the error messages
>mean, so I was wondering if you can provide me some guidance with
>errors.
And unless you tell us what the error messages are, we won't be able
to help you much either.
>
>See below:
>
>Thanks
>Jeff
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
>// JB6Q3.c ... Get two numbers and sort them
>#include <stdio.h>
>
>int sort_two(int * u, int * v);
>int main(void)
>{
> char input1;
> char input2;
> int num1;
> int num2;
>
> printf("In the program, you will enter two numbers and they will be
>sorted\n");
> printf("Enter the first number\n");
> gets(input1);
gets will input a string, that is a nul-terminated sequence of char.
input1 is a single char. It does not have enough room to hold more
than one. It also has the wrong type since gets expects a pointer.
> num1 = atoi(input1);
> printf("Enter the second number\n");
> gets(input2);
> num2 = atoi(input2);
>
> sort_two(&num1, &num2);
sort_two will return an int. Don't you think you should save it so
you can return it to the OS?
> printf("The sorted order is %d, %d\n", num1, num2);
>
> fflush(stdin);
fflush is defined only for output files.
> printf("\nPress enter to continue\n\n");
> getchar();
>
> return 0;
Only if num1<num2.
>}
>
>int sort_two(int * u, int * v)
>
>{
> if (num2 < num1)
There are no variables with these names in scope at this time. You
probably meant *u and *v to dereference the pointers you received from
the calling function.
> {
> int temp;
> temp = *u;
> *u = *v;
> *v = temp;
> return 1;
> }
>
> else
> printf("The numbers were already in sorted order.\n");
As a matter of good design, sort_two should only sort the numbers and
return a status value. Let the caller print out the message if
desired. Then sort_two will have more general applicability.
> return 0;
>}
<<Remove the del for email>>
--
comp.lang.c.moderated - moderation address: clcm@xxxxxxxxxxxx -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
.
- Prev by Date: Re: Switch from C++ to C
- Next by Date: Re: how do I know how long my function call took?
- Previous by thread: Re: Writing a program to sort numbers - HELP!
- Next by thread: Re: Switch from C++ to C
- Index(es):
Relevant Pages
|