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.
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.
0
Comments
So I appreciate you taking the time to help when you can.
[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?
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>
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.
Try
[php]
} while (ounces != 0);
[/php]
Oh, sacred. Welcome to SM.
"#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>
#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
[php]
code goes here
[/php]
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]
[php]
if(blah blah){
funct1();
funct2();
} else
just1func();
[/php]
#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.
The chapter covers Variables and Assignments, Input and output, Data Types and Expressions, Simple Flow of Control, and Program Style.
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...
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.
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.
I'll give you suggestions and things to read on and you should be able to get it.
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....
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.
[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.
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
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!
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).
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.