Thread: Global variables, how can I add washerB to this program?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    25

    Global variables, how can I add washerB to this program?

    Code:
    #include <stdio.h>
    #include <math.h>
    
    double	quantity1, thickness1, density1, inner1, outer1;
    double product, area;
    double washerA;
    
    /*          Prototypes               */
    
    void ReadData (void);
    double FindProd (void);
    double CircleArea (void);
    void PrintResult (void);
    
    int main(void)
    {
    	/*            Body of Control          */
    	ReadData ();
    	product=FindProd();
    	area=CircleArea();
    	washerA=product*area;
    	PrintResult();
    	return  0;
    }
    
    void ReadData (void)
    {
    	scanf ("%lf %lf %lf %lf %lf", &quantity1, &thickness1, &density1, &inner1, &outer1);
    }
    double FindProd (void) 
    {
    	double product;   /* local variable  */
    	product = quantity1*thickness1*density1;
    	return (product);
    }
    double CircleArea (void) 
    {
    	double area;   /* local variable  */
    	area = ((3.142*(pow(outer1/2,2)))-(3.142*(pow(inner1/2,2))));
    	return (area);
    }
    void PrintResult(void) 
    {
    	printf ("%0.2lf, %0.2lf\n",product,washerA);
    }
    Hello, I want this program to process 2 washers, in this case washerA and washerB, I must not pass any parameters in function calls.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    74
    why can't you pass parameters?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    25
    I cannot pass any parameters in function calls.

    It was a required instruction for this program, I am a little stuck, I got it running for one Washer, but adding another one, I have been working on this for awhile!

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    74
    I wonder what is the idea of that sort of rule, but anyway, why not just copy past the code and change the variable names?
    Kinda stupid, but works

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    74
    thought of another way, you could make a loop on main, but you would loose the first value(since you would be changing the same variable)

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dsured
    It was a required instruction for this program
    Time to find a new teacher. I'm sorry, but your teacher is teaching you bad habits. In some cases, global variables are fine, but this is the kind of assignment that should be used to teach proper parameter passing, not cultivate the bad habit of using global variables unnecessarily.

    EDIT:
    Yes, looking more at this program it is obvious to me that parameter passing makes much more sense. You define a bunch of functions to operate on a washer, then you call them for washer A, and then for washer B. But since you cannot do that, an obvious solution is to just use code duplication: rename your functions to ReadDataA, FindProdA, etc, and have them operate on quantityA, thicknessA, etc, and then create new functions ReadDataB, FindProdB, etc, to operate on quantityB, thicknessB, etc. This is stupid because functions can normally be used to reduce code duplication, but that's what happens when you have a stupid teacher.

    Actually, it would be easier to only have a main function. This might even improve readability, which is ironic.
    Last edited by laserlight; 11-02-2010 at 03:10 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Time to find a new teacher. I'm sorry, but your teacher is teaching you bad habits.
    I'll say....

    While I don't shy away from using globals when absolutely necessary (things like status flags or settings) they are generally not that great an idea as routine. As laserlight says, this guy is teaching you some very bad habits here...

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    int main(void)
    {
         for(int x = 0; x < 5; x++)
            {
    	ReadData ();
    	product=FindProd();
    	area=CircleArea();
    	washerA=product*area;
    	PrintResult();
           }
         return  0;
    }
    There you go, now it will do 5 washers...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Setting environment variables for use outside program
    By guesst in forum Windows Programming
    Replies: 12
    Last Post: 09-09-2008, 12:53 PM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. Envir. variables into C program
    By ojschubert in forum C Programming
    Replies: 3
    Last Post: 02-07-2005, 10:06 AM