473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C++ Help with nested loops

Alright, I'm having a lot of trouble grasping what's going on in my Computer Science class. I spent probably 5 hours+ on this simple project so far, and I can't get it to even come close to working. I think I'm going to have to take tutoring, or something, to make it through the semester.





Trust me when I say that you guys don't wanna see what I have so far...
Oct 26 '06 #1
1 1738
Ganon11
3,652 Expert 2GB
Alright, I'm having a lot of trouble grasping what's going on in my Computer Science class...

Trust me when I say that you guys don't wanna see what I have so far...
The first step is to break this problem down. What is it asking? Right off the bat, the problem asks for a loop - one for each floor, for the number of floors the user inputs. You'll need to define an integer variable to store the number of floors - call it numFloors. Then output a message asking the user for the input (i.e. "How many floors will your hotel have?") and prompt the user to input numFloors. If the user enters less than 1 floor, prompt them for different input (using a while(...) loop will do this).

You will also need two additional integer variables - one to store the total rooms, and one to store the total occupied rooms (Call them numRooms and numOccRooms, respectively). Initialize both variables to 0.

Next is the first loop. You'll want to loop an index (i) from 1 to numFloors, inclusive. A for loop will work marvelously. But here's our first problem:

Hotels skip the 13th floor, according to your problem specification. I can think of 2 ways to interpret this:

Suppose the user wants less than 13 floors. No problem.

Suppose the user wants exactly 13 floors. There are 2 possibilities. (1) Your teacher wants you to have a total of 13 floors, and treat the 13th floor as the 14th (a.k.a. after 12 iterations, start outputting i+1 for the floor number instead of i). Or, (2) Your teacher wants to completely ignore the 13th iteration - thus, if the user specifies 13 floors, there will actually be only 12 floors. You should ask for clarification on this issue.

Now that you have the loop header, you should begin executions for floor analysis (a.k.a. find out how many rooms there are and how many are occupied). I'll leave these simple calculations to you - most of this is simply prompting the user for input and storing user input into a temporary value. You'll also have to check if the rooms for this floor are greater than 10; if not, prompt the user for new input. Finally, you'll have to check if the number of occupied rooms exceeds the number of rooms; if not, prompt the user for new input.

After the loop has finished, you'll need to output relevant data; number of rooms, number of occupied rooms, number of unoccupied rooms, and the percentage of occupied rooms. All of this is straightforward except for the percentage. If you simply divide numOccRooms by numRooms, you'll get an integer - but you need a decimal number, such as 93.2%. In order to do this, you will need to cast one or both of the integer values to a double using a static_cast<double>(...) call.

Hopefully, this will give you a basic shell so that you can start programming. Good luck!
Oct 26 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
6
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote:...
0
by: Palle Girgensohn | last post by:
Hi! A performance question: I have some tables: Tabell "public.person" Kolumn | Typ | Modifierare ------------------+--------------------------+---------------...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
17
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html Why is C# 500% slower than C++ on Nested Loops ??? Will this problem be solved in...
3
by: Sean Shanny | last post by:
To all, We are running postgresql 7.4.1 on an G5 with dual procs, OSX 10.3.3 server, 8GB mem, attached to a fully configured 3.5TB XRaid box via fibre channel. I think we have run into this...
2
by: elein | last post by:
Yes, I vacuumed. Reproduced on both 7.3.2 and 7.5. Brain dead java beans want order by clauses in views that they use. my view is: select .... from bigtable b left join lookuptable l order...
9
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
4
by: toddlahman | last post by:
I am using two while loops that are nested. The first loop (post name) returns the full column of results, but the second (post modified) only returns the first row of the column. Is there another...
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.