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.
Assignment:
1. A metric ton is 35,273.92 ounces. Write a program that will read the weight of a package of breakfast cereal in ounces and output the weight in metric tons as well as the number of boxes needed to yield one metric ton of cereal. Your program should allow the user to repeat this calculation as often as the user wishes.
6. Write a program that determines whether a meeting room is in violation of fire law regulations regarding the maximum room capacity. The program will read in the maximum room capacity and the number of people to attend the meeting. If the number of people is less than or equal to the maximum room capacity, the program announces that it is legal to hold the meeting and tells how many additional people may legally attend. If the number of people exceeds the maximum room capacity, the program announces that the meeting cannot be held as planned due to fire regulations and tells how many people must be excluded in order to meet the fire regulations. For a harder version, write your program so that it allows the calculation to be repeated as often as the user wish.
10. Write a program that reads in ten whole numbers and that outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the ten numbers just once each and the user can enter them in any order. Your program should not ask the user to enter the positive numbers and the negative numbers separately.
/*
___________________________________________________________________
Programmer : Christopher La
Project : Ch. 2.1
Class : CS2
Due : 3/8/07
Description : Write a program that will read the weight of a
package of breakfast ceral in ounces and output
the weight in metric tons as well as the number
of boxes needed to yield one metric ton of cereal.
___________________________________________________________________
*/
#include <iostream.h>
#include <stdlib.h>
int main()
{
const double ounces_per_metric_tons = 35273.92;
double metric_tons, ounces;
cout << "Enter the number of ounces:\n";
cin >> ounces;
metric_tons = ounces*ounces_per_metric_tons;
cout << "There are " << metric_tons << " in "
<< ounces << " ounces.\n";
system("PAUSE");
return 0;
}
# 1 Re: C++ Assignment
Check your units on the "metric_tons(per cereal package)" calculation: your calculation returns "ounces squared per metric ton". If you divide your package weight in ounces by your ounces_per_metric_tons you'll get the right number of metric tons per package. Then divide one metric ton by metric_tons per cereal package and you'll get the number of cereal packages.
Any idea about a "menu" or other process to ask your user to choose another run or to quit?
nspils at 2007-11-11 20:58:38 >

# 2 Re: C++ Assignment
well I came up with this after a little help:
#include <iostream.h>
#include <stdlib.h>
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;
}
# 3 Re: C++ Assignment
does this compile? does this give you the answer you expect? it seems to me that your second calculation should be boxes = tons/ounces, not divisor/ounces (your calculation would give you 1/tons).
Yes, the do-while loop is effective for this task.
nspils at 2007-11-11 21:00:48 >

# 4 Re: C++ Assignment
Uh oh.. it works but I just noticed if you pick a number over 35273.92 the Boxes to the ton is still 0..
I think I am missing a line or something that limits it or well adjusts it to work accordingly.
Tried it by changing the boxes = tons/ounces without luck.
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:
#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;
}
# 5 Re: C++ Assignment
I went back and looked at my previous post which gives the right formula for the number of boxes as boxes = 1 / tons (using your variables) ... which, I realized, is the same as the ounces / divisor that you wrote -- I apologize for not seeing that.
As to the room capacity: Instead of hard coding your comparison with 500, try using your constant as the comparison. Then, if you go to a room which is 1000, all you need to do is reassign the value held in that constant and it is changed wherever it is used throughout your program. [by convention, constants are written in all caps - so your constant will be more quickly recognized as that in your code if you were to write it MAX_CAPACITY]
As to the comparison at the start of your if-else structure, the call of the question says that if the number of people is less than or equal to that capacity, the meeting can be held. So you should change that test to
if ( people > MAX_CAPACITY ) ...
A writing hint: your conditional will work for only one statement unless you enclose multiple statements within brackets.
To display the output of your "how many more people can the room hold" calculation, you cannot put it as a part of the string which is being fed to the output stream "cout" - you need to perform the calculation and feed its result to the output stream. Therefore, your output to the console should be:
cout << MAX_CAPACITY - people << " more people can be added." << endl;
or
int extra = MAX_CAPACITY - people;
cout << extra << " more people can be added." << endl;
nspils at 2007-11-11 21:02:47 >

# 6 Re: C++ Assignment
Thanks, I shall try that and read more into that when the time gives as I have class now ><
# 7 Re: C++ Assignment
It's been a while since i programmed in c++ but i gave #10 a try for fun. Here's what i came up with...
/********************************************/
#include <iostream.h>
#include <stdlib.h>
void main()
{
int array[] = new int[10];
int sum = 0;
for(int i =0; i < 10; i++)
cin >> array[i];
for(int i = 0; i < 10; i++)
{
if(array[i] > 0)
sum += array[i];
}
cout << "Sum of numbers greater than 0: " << sum << "\n";
sum = 0;
for(int i = 0; i < 10; i++)
{
if(array[i] < 0)
sum += array[i];
}
cout << "Sum of numbers less than 0: " << sum << "\n";
sum = 0;
for(int i = 0; i < 10; i++)
sum += array[i];
cout << "Sum of all numbers entered: " << sum << "\n";
return;
}
I don't have a compiler on this machine so i can't test it. You'll probably have to hit enter after every number that is entered. Let me know how this works. Also, is there a shareware or freeware compiler out there or should i just go buy the .net programmers suite?
-Brant
# 8 Re: C++ Assignment
@bdwellons: Sorry, as I appreciate your help but we haven't really learned about arrays at all yet... and I just use Dev-C++ 4 which is free. Same applies to 'for' statements.
PS: What is the 'i' stand for/mean?
EDIT: Oh nvm I assume the 'i' is just a variable to stand of the inputted number right?
Will read more from the chapter to see if there is something I am missing for #10 as yeah teacher hasn't gone into Arrays yet.
EDIT: Okay this is very simplistic as I really am at lost but this is what I have came up with so far for #10, doesn't work too greatly as I am missing a lot of statements ><:
#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;
}
