CrazyEngineers
  • Help on getting started with C++

    Black_Rose

    Member

    Updated: Oct 25, 2024
    Views: 3.0K
    We were given a very basic introduction to C programming.

    Today we were set a task in labs to use Microsoft Visual Studio 2008 and use C++.

    The question on the task was :

    Test the following expressions, where a, b and c are all integers and a and b have been assigned the values 8 and 9, respectively. You need to explain in the report what the value of each of the following expressions is and if the value of a or b changes, you need to give their new value.
    · c = b/a
    · a = b
    · a = b = 5
    · c = ++a + b--

    I'm a completely beginner so have ABSOLUTELY no idea where to start. Does anyone have any notes on the basic commands on C++, because I'm completely stuck. Please note I'm not looking for the answer, rather help so I may write the programme myself.

    I think it should look something like the following please correct me if I'm wrong!

    #include stdio.h

    void main (void)

    int a, b, c;

    a = 8;
    b = 9;
    c = b/a;

    printf (c=b/a);

    Whilst on the topic, what does the printf command do, the scanf command?

    Any and all help much appreciated!!
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • MaRo

    MemberOct 13, 2009

    Black_Rose
    · c = b/a
    · a = b
    · a = b = 5
    · c = ++a + b--
    a, b & c are integers..

    a = 8, b = 9;

    c = b/a;

    c = 9 / 8 = 1.125

    Since c is an integer, its floating point precision will be deducted, means int datatype doesn't take the digits after the decimal point.

    If you want the floating point precision use another datatype for c like float or double.

    & the flow goes as following:

    a = b; // the value of b replaces the value of a, now a = 9;

    a = b = 5; // now 5 replaces the values of a & b now both equal 5

    c = ++a + b--;

    ++ means incrementing the value of the variable by 1
    -- means decrementing the value of the variable by 1

    but their place in the next to the value differ the time the operation occur, if the they're on the left side the operation occur then do the instruction (line).

    means a & b now equal 5, ++a = 6, b-- = 4, but b won't decrement till the line is done.

    So, c = (++a) 6 + (b--) 5;
    c = 11;
    b = 4; //after the operation is done.


    Black_Rose
    #include stdio.h

    void main (void)

    int a, b, c;

    a = 8;
    b = 9;
    c = b/a;

    printf (c=b/a);
    Whilst on the topic, what does the printf command do, the scanf command?
    this suppose to show the value of c.

    but the printf() function isn't declared right.

    printf("%d",c) OR printf("%d",b/a)

    printf() shows the value of strings, now you want to show an integer so we use "%d" to show the value of c & you can't assign a value "c=b/a" in printf().

    scanf() takes input from user, like:
    int n;
    scanf("%d",&n);
    this waits for user input & press "Enter" button then put the value in "&n"

    &n : is the address of variable n in the memory, the bits value of assigned to its address, that's why we dont just put scanf("%d",n).

    & the "%d", means read it as an integer.


    I hope this is helpful for your query, feel free to ask and you can know more #-Link-Snipped-# & there are plenty of tutorials on a web search for "C++ tutorials"
    Are you sure? This action cannot be undone.
    Cancel
  • Black_Rose

    MemberOct 13, 2009

    Maro, thank you! You've been extremely helpful. I got the first program running, I just can't get the console to remain open, which command will keep the window open after execution and how do I output my result, would I be correct in assuming I should use printf (%d, c) ?
    Are you sure? This action cannot be undone.
    Cancel
  • MaRo

    MemberOct 13, 2009

    - You can add function getch(); or scanf(); to keep control for your application

    - printf() prints strings, strings in programming always surrounded with double quotations " string " .. so, it's printf("%d",c);
    Are you sure? This action cannot be undone.
    Cancel
  • Black_Rose

    MemberOct 19, 2009

    Maro Thanks alot! I handed in an ace report and I learned plenty.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register