C++ Assignment

Dear Readers,

I am new to the whole concept of C++ and taking a class but the Teacher doesn't necessarily teach from the book (Sadly the assignments are from the book) and I have read over the chapter but still at loss as to how to start on my assignment.. I have attempted the first problem but can't seem to get it to work.

I have attached what I have done so far for Question #1

The others I have no clue how to start and therefore do not know how to go about it... Re-Read the chapter twice and still clueless.
PS: Any hints as to how to start the others would be well appreciated.

Any help would be appreciated, thank you.
«1345

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited February 2007
    If you're clueless, talk to your TA or your teacher. Seriously. I'll take a look later, but you'll get better help with one-on-one with a real person.
  • edited February 2007
    @Shwaip: I will when Monday as I don't have the time to really ask during/after class till then (Due to having other classes) and he doesn't seem to do Emails well as he always replies with just ask in class (Student got yelled at for asking during class so I won't risk that).

    So I appreciate you taking the time to help when you can.
  • airbornflghtairbornflght Houston, TX Icrontian
    edited February 2007
    Well, I'm a bit rusty in C++, so take it with the fact that there is going to need to be some debugging done to these. I didn't test any of them. I want you to understand the program more than anything, so I have commented them quite heavily.

    [php]
    #include "iostream.h"//You need this header file for input and output.


    int main()
    {
    float ounces,tons;//these are your two floating point variables that you will need.
    int boxes;//this in an integer value that will be the number of boxes per ton.
    const float divisor = 35273.92 //this is the number of ounces in a metric ton
    char yesno='a'; //this is how we will decide whether to go another round or not.

    do{//this starts the do while loop. This is how we will repeat until the user wants to stop
    cout << "Please enter weight in ounces: ";//output statement asking for ounces
    cin >> ounces;//input. assigning the value to ounces

    tons=ounces/divisor;//the number of tons is equal to the number of ounces divided by divisor. Assigning to tons
    boxes=divisor/ounces;//boxes is assigned the value of divisor over ounces

    cout << "\n\nTons: " << tons << "\nBoxes to the ton: " << boxes;//this is our main output statement, the newline character condense the number of lines needed.
    cout<<"\n\nAgain? (y or n): ";//now lets ask if the user wants to go another round
    cin>>yesno;//capture the answer
    }while(yesno!='y' || yesno!='Y');//decide if the user wants to indeed go another round
    return 0;
    }

    [/php]

    thats the first program. I think you should be able to do the others from there. Do you understand all of the iteration structures and if statements?
  • edited February 2007
    Thanks for the help but I must question (As I didn't read it in the book) but for the 'do' is that equivalent to 'while' as yeah I rereading the chapter and don't see the 'do' script, only 'while'.

    Same applies to the 'float'.

    ALso when testing the program.. when I input 'n' it still repeats itself.

    I am a slow learner when it comes to this but I do get where you got most of the stuff you used.

    Then because of the mistakes that I had, I then learned a few things by just messing around with the values a bit and came up with this:
    [php]#include
    #include

    int main()
    {
    float ounces,tons;
    int boxes;
    const float divisor = 35273.92;
    char ans;

    do{
    cout << "Please enter weight in ounces: ";
    cin >> ounces;

    tons = ounces/divisor;
    boxes = divisor/ounces;

    cout << "\n\nTons: " << tons << "\nBoxes to the ton: " << boxes;
    cout << "\n\nAgain? (y or n): ";
    cin >> ans;
    }while(ans == 'y' || ans == 'Y');

    system("PAUSE");
    return 0;
    }[/php]Thanks.

    I'll give the others a go tonight if I have the time.</stdlib.h></iostream.h>
  • airbornflghtairbornflght Houston, TX Icrontian
    edited February 2007
    yeh, they are both referred to as while loops. The difference is where it checks the condition. so the do while loop is a post check loop, and the while loop is a pre check loop. The do while loop runs the block of code at least once, where as a while loops body may not be ran at all.

    do you have iostream imported? its not showing up. As for the other two, it looked like the were just going to be if statements, and that one is going to be an if statement inside a for loop. Pretty simple programs. If you were doing these in Java I'd have them done in about 2 minutes and commented for you. But its been about a year since I've coded in C++. Just a good thing C++ is really similar to Java. I'm not sure why its not exiting the loop, I bet it has to do with the way that I'm referring to the char, you might try changing it to an int value and see if you can exit out by entering a number, say 0. and all you would do is change the conditional to is: }while(ans!=0);

    and to go further than that and do this:

    [PHP]int main()
    {
    float ounces,tons;
    int boxes;
    const float divisor = 35273.92;

    do{
    cout << "Please enter weight in ounces (0 to quit): ";
    cin >> ounces;

    if(ounces>0){
    tons = ounces/divisor;
    boxes = divisor/ounces;

    cout << "\n\nTons: " << tons << "\nBoxes to the ton: " << boxes;
    }
    else if(ounces<0)
    cout << "Please make sure your value is positive";
    }while(ounces!=0);

    system("PAUSE");
    return 0;
    }[/PHP]

    and this will also keep you from getting a division by 0 error thrown. It will also keep you from getting erratic results from someone putting in a bunk value (-1) since there is no negative weight that I am aware of. lol. Hope this is helping. If you have any questions feel free to ask. and im not sure if that second if statement needs curly braces or not. I know in java if there is only one line of execution you don't need them but I forget about C++. You might check that.

    As for you question on float, it is a floating point primitive data type. If I remember correctly there is float, double, and long. and for your purposes I doubt you need anything bigger than float. The only difference among them is their precision. Which affects how much memory the use. Not terribly important in your situation, but you should always try to build robust programs that use as little memory as possible. Becomes a whole lot more important when you start dealing with large programs.
  • a2jfreaka2jfreak Houston, TX Member
    edited February 2007
    You have an extra ";" in the while condition.
    Try
    [php]
    } while (ounces != 0);
    [/php]

    [PHP]
    }while(ounces!=0;);
    [/PHP]
  • airbornflghtairbornflght Houston, TX Icrontian
    edited February 2007
    oops. thats what I get for typing too fast in that tiny edit window. :vimp:

    Oh, sacred. Welcome to SM.
  • edited February 2007
    Thanks and I am not too sure if my iostream is imported or not as I just typed it as my Professor suggested:
    "#include <iostream.h>
    #include <stdlib.h>"
    As I am using Dev-C++ 4

    Also, with the one I posted up on my previous post that worked and I was able to quit out of it when I hit 'n'.

    With the updated code you gave, yeah can't seem to get it to work right as the formatting seems off.


    PS: Thanks for the welcome.
    </stdlib.h></iostream.h>
  • airbornflghtairbornflght Houston, TX Icrontian
    edited February 2007
    hmm. those include statements don't seem to be doing anything. just do this

    #include < iostream.h >

    without the spaces after and before the brackets
    is how its done I believe. That could be your problem. I'm not sure. Other than that I see nothing functionally wrong with the code
  • a2jfreaka2jfreak Houston, TX Member
    edited February 2007
    When you're inserting code, use the php tags so that formatting is preserved.

    [php]
    code goes here
    [/php]
  • edited March 2007
    Yeah I did use that tag.

    Also.. I just noticed that if I enter a number higher than the float divsor, the return value of box to ton = 0 and not greater, but if I enter the exact float divisor it returns as 1...

    EDIT: Re-Read the .doc for Question 6
    I gave it a go and having a bit of trouble when it comes to getting it to execute, error has to do with the 'else' part:
    [php]#include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    int people;
    float extra, add;
    const float Max_capacity = 500;
    char ans;

    do{
    cout << "Please enter the amount of people: ";
    cin >> people;

    if (people >= 500)
    cout << "Meeting cannot be held.\n";
    cout << "(Max_capacity - extra) need to be removed.\n";
    else
    cout << "Meeting can be held.\n";
    cout << "(Max_capacity - extra) more people can be added.\n";

    cout << "\n\nAgain? (y or n): ";
    cin >> ans;
    }while(ans == 'y' || ans == 'Y');

    system("PAUSE");
    return 0;
    }[/php]
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2007
    you need to use braces if you want more than one thing under a conditional:
    [php]
    if(blah blah){
    funct1();
    funct2();
    } else
    just1func();
    [/php]
  • airbornflghtairbornflght Houston, TX Icrontian
    edited March 2007
    [php]#include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    int people;
    const int MAX = 500;
    char ans;

    do{
    cout << "Please enter the number of people: ";
    cin >> people;

    if (people > MAX){
    cout << "Meeting cannot be held.\n";
    cout << people - MAX << " need to be removed.\n";
    }else{
    cout << "Meeting can be held.\n";
    cout << MAX - people << " more people can be added.\n";
    }
    cout << "\n\nAgain? (y or n): ";
    cin >> ans;
    }while(ans == 'y' || ans == 'Y');

    system("PAUSE");
    return 0;
    }[/php]

    Try that. You seem to be missing some key concepts. Are you sure that you have read the entire chapter? What does this chapter cover? I want to help you, but I can't just do your homework.
  • edited March 2007
    Thanks, I have read the chapter but I mean there are some parts that I don't full understand and therefore seems that I haven't read the chapter.

    The chapter covers Variables and Assignments, Input and output, Data Types and Expressions, Simple Flow of Control, and Program Style.
  • GHoosdumGHoosdum Icrontian
    edited March 2007
    yeh, they are both referred to as while loops. The difference is where it checks the condition. so the do while loop is a post check loop, and the while loop is a pre check loop. The do while loop runs the block of code at least once, where as a while loops body may not be ran at all.

    You're correct in your definition of a do vs. a while loop, but it's not strictly correct to refer to both as while loops. Although the do loop is post-checked with the while statement, if the assignment is specifically asking for a "while loop" I am pretty sure it is asking for the pre check while in the code...
  • airbornflghtairbornflght Houston, TX Icrontian
    edited March 2007
    The assignment was very broad and didn't ask him for any specific elements to be included. It just said what the program should ask for, output, and accomplish. I made the decision for a do loop as I figured the program needed to run at least once. So a post check loop seemed the most appropriate.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2007
    He's not going to learn anything if you just keep giving him code. He's either going to learn how to change the code you gave him, or how to turn in your code.

    Like you said, he's missing concepts, and reading code isn't going to help. Reading the book and getting help from resources available on campus is what he needs.
  • edited March 2007
    Actually I am learning slowly from seeing the code and then referencing it back to the book as to where it described about it...

    Cause sometimes for me.. Reading doesn't cut it as I will still be at loss.. But with a given code I can reference it back to the book and it makes more sense to me.. As the book gives examples but then they don't relate to the Problem Asked so I am at a loss there.
  • airbornflghtairbornflght Houston, TX Icrontian
    edited March 2007
    So did you get them all three coded?
  • edited March 2007
    Not yet, I am having trouble on the third I mean I don't want to post what I have up as I am too embarrassed by how little information I put on it.. But then again as I mentioned I read the chapter but the information just doesn't sink in.
  • airbornflghtairbornflght Houston, TX Icrontian
    edited March 2007
    post it and I'll help. But I'm not going to do it for you this time.

    I'll give you suggestions and things to read on and you should be able to get it.
  • edited March 2007
    Sorry didn't notice there was a reply...

    But here is what i have so far:

    [PHP]#include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    int x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, sum;
    const int LIMIT = 0;

    sum = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10;

    cout << "Enter 10 whole numbers: \n";
    cin >> x1 >> x2 >> x3 >> x4 >> x5 >> x6 >> x7 >> x8 >> x9 >> x10;

    if (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10 > LIMIT)
    {
    cout << "Sum of numbers greater than 0: " << (sum > LIMIT) << endl;

    }
    else
    {
    cout << "Sum of numbers less than 0: " << endl;
    }

    cout << "Sum of all whole numbers: " << sum << endl;

    system("PAUSE");
    return 0;
    }[/PHP]

    There is a lot I need to add in as I can't seem to get the concept even after reading the chapter over and over....
  • airbornflghtairbornflght Houston, TX Icrontian
    edited March 2007
    Ok, try this on for size.

    Make a for loop that goes something like for(int i=0;i<10;i++)

    and then output a message that says input integer number i+1 : then cin a variable named temp or something like that declared inside the loop.

    also, inside that loop wouldnt it be easier to have an if statement with two clauses. that says if temp is less than 0 x+=temp, else y+=temp. and z could be x+y.

    Seems a whole lot more elegant to me. then just output x,y,and z outside of the for loop. do you see how that takes considerably less code and is ten times more readable? now try to apply what I just told you and show me what you come up with.

    Is this a college class? I'm curious. because I'm going to college next year and if they stick me in something as rudimentary as this I'm going to be pissed.
  • edited March 2007
    Ehhh I came up with something like this.>..

    [PHP]#include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    int i, x, total, SumPos, SumNeg;

    cout << "Enter 10 whole numbers: \n";

    for (int i = 0; i < 10; i++);
    {
    cin >> x;
    }

    if (x > 0) SumPos+ = x;
    cout << "Sum of all numbers greater than 0: ";
    else if (x <= 0) SumNeg+ = x;
    cout << "Sum of all numbers less than 0: ";

    cout << "Sum of all numbers: " << total = x;


    system("PAUSE");
    return 0;
    }[/PHP]

    Also this is a College Course, BUT I went in with no prior knowledge so it is hard for me.. As you have had experience with it then it seems easy.
  • jaredjared College Station, TX Icrontian
    edited March 2007
    You might want to go to your local bookstore and pick up a copy of "C++ for Dummies".

    I have had several friends who struggled in programming classes and their text books were very poor for beginners. The "for Dummies" books are really repetitive but if you have no prior knowledge that is probably what you need. Also their examples are usually much easier to understand and relate too.

    If the text is over your head and the Prof/TA isn't around I would highly recommend getting a copy.

    cheers :jared:
  • edited March 2007
    I'll definitely look into that as yeah our textbook is hard to understand and doesn't really cover the material, and the examples given don't relate to the questions the book asks you.. No TA in class and Teacher does okay in lectures but outside help is a little harder as I don't have the time (Due to other classes) and when those are over he is gone.
  • airbornflghtairbornflght Houston, TX Icrontian
    edited March 2007
    Ehhh I came up with something like this.>..

    [PHP]#include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    int i, x, total, SumPos, SumNeg;

    cout << "Enter 10 whole numbers: \n";

    for (int i = 0; i < 10; i++);
    {
    cin >> x;
    }

    if (x > 0) SumPos += x;
    cout << "Sum of all numbers greater than 0: ";
    else if (x <= 0) SumNeg += x;
    cout << "Sum of all numbers less than 0: ";

    cout << "Sum of all numbers: " << total = x;


    system("PAUSE");
    return 0;
    }[/PHP]

    Also this is a College Course, BUT I went in with no prior knowledge so it is hard for me.. As you have had experience with it then it seems easy.

    ok, move your first output statement inside the for loop and change it to say something like "Enter integer#" << i+1<<": "; Also, you are doing your adding outside of the loop body, which isn't going to do much of what you want it to. So I might think about moving your if statement inside the loop body also. That way it will print:

    Enter integer #1:
    Enter integer #2:
    Enter integer #3:
    ...

    Do you see how this for loop is gonna work for us?
    There is no need for an else if when you are testing only those two conditions. if it isn't negative than we can say that it is without a doubt positive. so an 'else' is all that's needed. I can see that you are really missing a lot of concepts so I don't feel right writing the code but I would also second one of those dummies books. Or some time with your TA, they should have office hours that are at least available by appointment.

    also, can you tell me you declared variable 'i' twice? the only time that you need to declare it is in the loop. Thats called your loop control variable. You also probably want to shy away from the newline character on your first output statement, because now it won't look as pretty. And shouldn't total be equal to sumPos and sumNeg?

    See where that gets you. but come test time you are really gonna have to get this stuff down. Just wait till you get into recursion and polymorphism. FUN!
  • edited March 2007
    Bah...

    After a little editing, I still have a small problem...

    [php]#include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    int i, x, total, sumPos, sumNeg;

    for (int i = 0; i < 10; i++);
    {
    cout << "Enter 10 whole numbers";
    cin >> x;

    if (x > 0) sumPos = x;
    cout << "Sum of all numbers greater than 0: " << sumPos;

    else (x <= 0) sumNeg = x;
    cout << "Sum of all numbers less than 0: " << sumNeg;

    cout << "Sum of all numbers: " << total;
    total = sumPos + sumNeg;
    }

    system("PAUSE");
    return 0;
    } [/php]For the writing out of Inputted numbers I thought maybe the way I did it, it would be so the user can input there numbers in one line, separated by a space.

    Hmm only have one error, 'Parse Error before 'else''..
  • airbornflghtairbornflght Houston, TX Icrontian
    edited March 2007
    Bah...

    After a little editing, I still have a small problem...

    [php]#include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    int i, x, total, sumPos, sumNeg;

    for (int i = 0; i < 10; i++);
    {
    cout << "Enter 10 whole numbers";
    cin >> x;

    if (x > 0) sumPos = x;
    cout << "Sum of all numbers greater than 0: " << sumPos;

    else (x <= 0) sumNeg = x;
    cout << "Sum of all numbers less than 0: " << sumNeg;

    cout << "Sum of all numbers: " << total;
    total = sumPos + sumNeg;
    }

    system("PAUSE");
    return 0;
    } [/php]For the writing out of Inputted numbers I thought maybe the way I did it, it would be so the user can input there numbers in one line, separated by a space.

    Hmm only have one error, 'Parse Error before 'else''..

    Ok. Why not say if x<0, because right now you say if x>0, which is incorrect because while 0 is neither positive or negative its easier to throw it in with the positives. and why do you have a conditional after your else. you don't need it. because if it isn't positive it has to be negative. See my logic? Also, get those output statements out of the loop body. those will be the last thing that gets output. Why would you output the value of total before it is assigned? Doesn't seem to be logical.

    you need to put your assignment statements inside the if blocks. if<0 then sumNeg+=x else sumPos+=x. Its really that simple. You seem to be over complicating this. Remember, when you are programming follow the golden rule: KISS. (no, not the band).
  • edited March 2007
    Hmm I most likely am making this too complicated than needed...

    Alright I got a little confused about the whole put assignment statements inside the if block... are the assignment statements pretty much the one in front of the for block?

    Also taking the output statements outside of the loop... were you referring to the Total statement?

    [PHP]#include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    int i, x, total, sumPos, sumNeg;

    for (int i = 0; i < 10; i++);
    {
    cout << "Enter 10 whole numbers";
    cin >> x;

    if (x <= 0) sumNeg+=x;
    cout << "Sum of all numbers greater than 0: " << sumNeg << endl;

    else sumPos+=x;
    cout << "Sum of all numbers less than 0: " << sumPos << endl;
    }
    total = sumPos + sumNeg;
    cout << "Sum of all numbers: " << total;

    system("PAUSE");
    return 0;
    } [/PHP]

    Also.. does it make a different of which goes first x<=0 / x>0 by means that the first output is the sum of Positive numbers opposed to negative.
Sign In or Register to comment.