Hello,

I have this problem.

Use the function that you just wrote to read a sequence of numbers. Put them into an array declared in main, by repeatedly calling the function. Sort them into ascending numerical order, then print the sorted list.

So I thought this can work

/*
 * =====================================================================================
 *
 *       Filename:  test.c
 *
 *    Description:  test
 *
 *        Version:  1.0
 *        Created:  06-05-11 22:46:11
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Dr. Fritz Mehner (mn), mehner@fh-swf.de
 *        Company:  FH Südwestfalen, Iserlohn
 *
 * =====================================================================================
 */


#include        <stdio.h>
#include        <stdlib.h>

int uitkomst = 0 ;
int getallen[10];
int teller= 0;

int
verwerken (letter )
{

        if (teller <= 9)
        {
            getallen[teller] = letter;
            teller = teller +1 ;
            uitkomst = 0 ;
        }
        else
        {
                uitkomst = -1 ;
        }
        return uitkomst ;
}               /* ----  end of function verwerken  ----- */


int
main ( int argc, char *argv[] )
{
        int  ch;

                ch = getchar();
                uitkomst=0;
                      if (uitkomst <> -1)
                      {
                        while(ch != 'a')
 {
                          invoer=  verwerken(ch);
                          ch = getchar();
                        }
                      }
                printf ("De uitkomst is : %d", uitkomst);
                return (EXIT_SUCCESS);
}

So if the user enters more then 10 numbers and if the user enters "a" the sorting must begin.
But I can't figure out how to stop the entering of numbers without using a and statement.

Can anyone help me ?

Roelof

Recommended Answers

All 3 Replies

#1) You use a single IF to test if the first letter returns from your function good or bad. After that first test you never test the return from the function.
#2) Your instructions do not mention exiting on 'a'. I'm certain you should exit on an 'error' return from your function.

Rethink your problem.

Hello,

Correct that the if on a is never returned.
That's the problem I try to resolve.

I don't think it must exit on a error because after a few numbers the numbers have to be sorted. Or you must mean that I check if there are more then 10 numbers.
If so , stop entering numbers and sort the numbers. If not , the user has to enter another number.

Roelof

Look at your function again. What does it return? Why does it return those values? How can you use these values to control your loop?

There should (probably) be no IFs in main() because it's not needed.

Now, as I implied before, don't try to fix your other code. Just rewrite main() from scratch, putting more thought into how your function works.

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.