Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hello! I'm doing my homework and cannot figure this out so please help. ok I'm suppose to create a windows application that will allow users to input any year that is greater then 2005 and beyond. once the user enter the year, like 2010, then the program will calculate the year population base on a 2% increase yearly. anyway I create a function and to solve this problem and the result is great but at the same time the requirement of the project is also to calculate the life expectancy too. I don't know how to return two value from the function as a function return only a single value. How would I solve this problem. thank in advance!

In C, you could use a struct or a string if you need to use a multi value returning function. You could also just pass the pop and life vars by reference and save yourself the grief. :P
#include <stdio.h> #include <stdlib.h> typedef struct { float var_a; float var_b; } mystruct; mystruct *by_struct(int year) { mystruct *tmp = (mystruct *)malloc(sizeof(*tmp)); // calc pop and life tmp->var_a = pop_value; tmp->var_b = life_value; return (tmp); } char *by_string(int year) { char *tstr = malloc(BUFSIZ); // calc pop and life values sprintf(tstr, "%0.2f#%0.2f", pop_value, life_value); return (tstr); } int main(int argc, char *argv[]) { mystruct *s = NULL; char *p = NULL; float a = 0.0f, b = 0.0f; s = by_struct(some_year); printf("by_struct = %.2f, %.2f\n", s->var_a, s->var_b); p = by_string(some_year); sscanf(p, "%f#%f", &a, &b); printf("by_str = %.2f, %.2f\n", a, b); free(s); free(p); return (0); }I'm still a bit rusty with C, so whether or not that compiles, runs, and terminates normally is anyone's guess. Nonetheless, HTH.

Well this what I have but in VB. for some reason the population return correctly but the life expectancy value remains the same.
Select Case intSelectedCounty Case 0 'this is the calling function dblFuturePopulation = China(decDifferenceOfYear) dblFutureLifeExpectancy = ChinaLife(decDifferenceOfYear) 'this sub-function will calculate china life expectancy Private Function ChinaLife(ByVal decYearInput As Decimal) As Decimal Dim dblChinaCurrentLife As Double = 72.3R Dim dblAnnualLifeIncrease = 0.0000023R Dim decYearStart As Decimal = 1D Dim decYearEnd As Decimal = decYearInput Dim decYearLoop As Decimal Dim decIncreaseLife As Decimal Dim dblFutureLife As Decimal For decYearLoop = decYearStart To decYearEnd Step 1 decIncreaseLife = dblChinaCurrentLife * dblAnnualLifeIncrease dblFutureLife = decIncreaseLife + dblChinaCurrentLife dblChinaCurrentLife += decIncreaseLife Next Return dblFutureLife End Function 'this below function will calculate china Population Private Function China(ByVal decYearDifference As Decimal) As Double Dim dblChinaCurrentPop As Double = 1306314000.0R Dim decAnnualRateOfPopChange As Decimal = 0.006D Dim decYearStart As Decimal = 1D Dim decYearEnd As Decimal = decYearDifference Dim decYearLoop As Decimal Dim dblIncreasePop As Double Dim dblFuturePop As Double For decYearLoop = decYearStart To decYearEnd Step 1 dblIncreasePop = dblChinaCurrentPop * decAnnualRateOfPopChange dblFuturePop = dblIncreasePop + dblChinaCurrentPop dblChinaCurrentPop += dblIncreasePop Next Return dblFuturePop End Function

![]() |
Search DOS files for text...
|
Extracting Text from a te...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |