Originally Posted by
BurleMD
Okay, this is my best shot at writing it. I have no clue how those modular functions or whatever work, so I pretty much just wrote some crap. Im turning it in now cause frankly, Im tired of trying to dick with it and get it to work. If someone could tell my how I should have incorporated those into the program, i think it would help for next time. Thanks.
/* Matt
COMP-1200
Fall '06
Assignment 5
---------------------------------------------------------------
---------------------------------------------------------------
*/
#include <stdio.h>
#define e 2.71828
int main()
{
/*Declare all variables */
void printDashes();
int getEndYear();
void displayHeader( int year );
double calPop( int t );
void displayPop( int year, int t );
void displayStars( int numStars );
void displayPercChange( int begYear, int endYear,
int beg_t, int end_t );
double findPercChange( int beg_t, int end_t );
float p, pop;
int year;
/*Get year */
printf("Enter a year inbetween 1990 and 2100: \n");
scanf("%d", &year);
/*Keep year in valid range */
if (year < 1990) || (year > 2100)
printf("Invalid year, must be between 1990 and 2100 \n");
else
/*Make t = 0 */
t = t - 1990;
/*Calculate the population */
p = 11.14 / ( 1 + 1.101 * e^(-0.051*t)
pop = p * 100
/*Print the inforumation */
printf(The year is : %i \n",year);
printf(The population is : %5.2f \n",pop);
/*Exit */
return 0;
The modules or functions that are mentioned are just logical boxes that we can take parts of a big problem and chip off, and put them into these boxes. Once a box (module or function) is right, we can seal it up with tape, and say "OK, that's done, now the problem is smaller than it used to be".
In addition to making the whole problem easier to deal with, it also helps insulate any problems of logic or syntax. Say you have a function like "DisplayStars", and all it does is just show the right number of '*''s for the populations percentage change. Once we get this DisplayStars function working right - then we know if there's a problem with the stars being displayed - it will be because (most likely), that function is getting the wrong input. Because we KNOW it will indeed display the stars correctly with the right input.
So DisplayStars is one of the functions or modules we'd use in this program. If we can group these functions into groups that are doing similar requirements, and still keep them small enough to easily debug, it's a big help in writing good code.
Here on the board, please use code tags around any code you submit. It keeps the right spacing, which is a HUGE help for people to understand your code. Code tags are just square bracks around the word code, to start the code, and then square bracket around the word code again BUT THIS TIME WITH A SLASH IN FRONT OF THE WORD CODE, to end the code. It looks like this, when it's done right:
Code:
/* example of code tag use */
for (i = 0; i < 10; i++)
printf("\n I is now: %d", i);
It's really what you want to add to any code you post up on the forum.
Adak