HELP PLEASE! C++ EXERCISE



  • Fisrt of all,sorry I don't speak german.

    I habe an exam tomorrow on c++ and I am looking for an answer to an exercise that is the following:

    Write a program that asks the user 10 (or less) numbers. (So program asks: how many numbers u want to enter?) The numbers that are entered, ar read into an Array. After that the doubles are removed.

    Example: After reading numbers 5 7 13 6 7 13 4 6 5 and 8 the arra is as followed:

    [5,7,13,6,7,13,4,6,5,8]

    After "deleting" the doubles the array is as followed:

    [5,7,13,6,4,8,?,?,?,?]

    It doesnt matter what the content of ? is.

    Can somenone give me some tips or a c++ code (not to complex).
    The exercise should be done with a FOR-loop.



  • I think the standard input and output is not the problem,

    to delete the doubles you can do something like this:

    for (int i=0; i<Count; i++)
        {
        for (int j=i+1; j<Count; j++)
            {
            if (Array[i] == Array[j])
                {
                for (int k=j; k<Count-1; k++)
                    {
                    Array[k] = Array[k+1];
                    }
                Array[Count-1] = -1000;
                }
            }
        }
    

    Count = number of entered numbers
    Array = Array of entered numbers

    maybe there is a better way, but I think for the moment its ok



  • Yes! It is working!!

    thank you sooo much for the script!!!!!

    You are a very nice person 😉

    Keep it up 👍 👍 👍



  • I think the algorithm will fail if there are more than 2 equal numbers in the array. (just as side note)



  • should be easily fixed with decrementing the Count, which may also save a bit of unnecessary work.
    I'd introduce something like

    const int MY_MAGIC_QM = -1000;
    

    though 😉



  • Hmm, I'm not sure which tomorrow is meant 😮 😉

    [EDIT]<OT> WTF? I wrote meant without a. I think it's time for learning...</OT>[/EDIT]



  • Exam was today, but it went okay.

    An exercise with 2dimensional array, so it was okay.

    Thanks again for your answers.


Anmelden zum Antworten