|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
Tell me the right way to do it!!!!! I'm having problems with this questionsThe world population reached 6 billion people in 1999 and was growing at the rate of 1.4 percent each year. Assuming that the population will continue to grow at the asme rate, write a program to determine when the population will exceed 10 billion. What am I doing wrong?? Private Sub cmdbutton_Click() Dim population As Integer Dim years As Integer years = 0 population = 6000000 Do While population > 10000000 population = population * 0.014 years = years + 1 Loop picWhen.Print "In"; years; "Population will be"; population; End Sub Last edited by Dodot : October 17th, 2004 at 12:26 PM. Reason: I need answers |
|
#2
|
|||
|
|||
|
Your first problem is using an integer datatype, which doesn't count to the billions. Review the available VB datatypes in the documentation.
Your next problem is not posting the error you got when you tried to run your code.
__________________
====== Doug G ====== I didn't attend the funeral, but I sent a nice letter saying I approved of it. --Mark Twain |
|
#3
|
|||
|
|||
|
Quote:
Try changing Code:
Private Sub cmdbutton_Click() Dim population As long Dim years As Integer years = 0 population = 6000000 Do While population > 10000000 population = population * 0.014 years = years + 1 Loop picWhen.Print "In"; years; "Population will be"; population; End Sub |
|
#4
|
||||
|
||||
|
The code inside the while will never be executed. You are saying:
population = 6 million Then saying: do while population > 10 million The do loop will NEVER execute, and your code will just say "in 0 years, the population will be 6 million" (Note, Im using the word "million" for clarity) What you WANT the do loop to say, is: do while population < 10 million... Thats the SERIOUS logic flaw I see... |
|
#5
|
||||
|
||||
|
You are multiplying by ZERO .014. your have created an ever-shrinking population count, at this rate we will be extinct in no time and your loop will NEVER END. That statement makes population = 1.4 percent of it's original population. 1 % of 100 is 1 so that is a pretty small number.
Change to this: population = population * 1.014 like it said in the problem "rate of 1.4 percent" . Rule: move the decimal two places to the left. If you are DECREASING to the value leave the zeros. If you are INCREASING by the value you have to represent the EXISTING value with a 1 to the left of the decimal because we all know that 1 x 1 is 1. You have to have at LEAST that if you are going to increase a value. It would do you some good in solving these to understand how to set a b... reakpoint and step through your code one line at a time and WATCH the values. If you don't understand it, you need to get help from your teacher because this is fundamental to any programming. In VB You can wave your mouse over a variable after you execute a step and see for yourself what is happening to your values. YOu can watch it Do ...Loop and see why it isn't looping. Open the Immediate window and add debug.print to your program like this: Do While population < 10000000 population = population * 0.014 years = years + 1 Debug.Print population Loop run it and look at your immediate window. Again, get a demonstration from someone who knows. It can be hard to grasp the meaning of "debug" and "immediate" and "step the code" when you are first starting but you have to understand it if you want to be able to succeed at this so if you don't get it, keep asking "what do you mean?" and don't be afraid to say "show me" ... good luck and hang in there. |
|
#6
|
||||
|
||||
|
Clarification on dealing with percents
population grows by 1.4 percent: population = population * 1.014 population shrinks by 1.4 percent: population = population - (population * 0.014) population shrints TO 1.4 percent: population = population * 0.014 |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Help!!Check my VB program and tell me what I'm doing wrong!! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|