#beginner nood needs help c++ pls

42 messages · Page 1 of 1 (latest)

limpid needle
#

hy everyone
i have here an exercises:
ask me to initialize 4 int variable, the first 3 variables cout with the user imput cin ; the 4th=2th; (imput real numbers ex: 0, 1, 2, 3..)
the exercise ask me to sort the first 3 int entered values ​​in ascending order.
don't know where i get mistakes...spnt some hours to try resolve it but no way.

if anyone wants to explain what I'm doing wrong, thank you in advance

lapis anvilBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

autumn turret
#

What do you want to achiebe exactly? Its hard to understand.

limpid needle
#

want to sort my variables in ascending order

#

have 3 variable in imput user (real numberses 47, 89, 93)

#

the forth int variable = 89;

dapper storm
#

do you know how to use for() loop yet

limpid needle
#

not yet

dapper storm
#

and I don't understand you either

#

so the 4th must equals to 2th

#

and you must use only "if else"

#

am I correct

limpid needle
#

yes

sage terrace
#

This can be done with three if() statements.

if (b < a)
  swap(a, b);

if (c < a)
  swap(a, c);

if (c < b)
  swap(c, b);

I have no clue what the purpose of your 4th number is.

limpid needle
#

can't swap it

autumn turret
#
#include <iostream>

int main()
{
    int a, b, c;
    a = 3;
    b = 2;
    c = 1;
    int s[3] = {a, b, c};

    if (s[0] > s[1])
    {
        int tmp = s[0];
        s[0] = s[1];
        s[1] = tmp;
    }
    if (s[1] > s[2])
    {
        int tmp = s[1];
        s[1] = s[2];
        s[2] = tmp;
    }
    if (s[0] > s[1])
    {
        int tmp = s[0];
        s[0] = s[1];
        s[1] = tmp;
    }

    std::cout << s[0] << std::endl;
    std::cout << s[1] << std::endl;
    std::cout << s[2] << std::endl;

    return 0;
}
#

This works

limpid needle
#

must use =

sage terrace
#

So, do it the hard way.

autumn turret
limpid needle
#

unisersity exercise 😦

sage terrace
#

I will leave that as an exercise for the reader.

autumn turret
#

@limpid needle is my solution fine?

#

can you use it or do you need another one? ^^

limpid needle
#

I think it works ... but it doesn't meet the exercise terms

autumn turret
#

why not? because of the array? we can fix that quite easily

#

And yes it does work. I tested it (with all possible situations).

limpid needle
#

yes ... we haven't talked about arrays yet

autumn turret
#
    if (a > b)
    {
        int tmp = a;
        a = b;
        b = tmp;
    }
    if (b > c)
    {
        int tmp = b;
        b = c;
        c = tmp;
    }
    if (a > b)
    {
        int tmp = a;
        a = b;
        b = tmp;
    }```
then here you go
limpid needle
#

i think this one work's fine.

#

I'll let you know shortly

#

thank you for the support

autumn turret
#

alr. no problem ^^

limpid needle
#

i appreciate it

autumn turret
#

If you need help with the next exercises or smth related to C++ you can ask me directly (via discord private messages).. if you want of course ^^

limpid needle
#

I will definitely do it ... you do not know how much I am grateful to hear you say it ... I am a beginner, and the manuals that the university provide us do not give sufficient examples and explanations

autumn turret
#

Where do you study and which language do you speak?

limpid needle
#

i study in genova... italy

#

but i'm romanian

autumn turret
#

ahh okay

lapis anvilBOT
#

@limpid needle Has your question been resolved? If so, run !solved :)

limpid needle
#

!solved