Re: Writing a program to sort numbers - HELP!



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.
.



Relevant Pages

  • Re: Using a link list over an array.
    ... compile (p->data is a void *) so you have not shown us some key ... You can't use it to sort a list of ints where the int is ... You can use it to sort a list of pointers to any type. ...
    (comp.lang.c)
  • Sorting a CList
    ... I need to sort a CList ... I have this code but it there is something wrong with the pointers.. ... int n=vector.GetCount; ... Please correct me so I may improve my English! ...
    (microsoft.public.vc.mfc)
  • Writing a program to sort numbers - HELP!
    ... an integer using atoi(). ... Then we using a sort function to sort the ... int main ... int temp; ...
    (comp.lang.c.moderated)
  • Re: resize JTable
    ... >able to easily sort the data, from a user point of view. ... public static final int DESCENDING = -1; ... private Comparator comparator; ... * @param model OrderedTableModel to be kept deduped/sorted. ...
    (comp.lang.java.programmer)
  • Re: A taxonomy of types
    ... I am describing how to represent the representation (and, ... if the system follows static typing rules (such as in a compiler), ... so, the hardware sees pointers and pointer arithmetic, but the compiler ... "Besides the char types, up to three sizes of integer, declared short int, ...
    (comp.lang.misc)