Results 1 to 3 of 3

Thread: Loop counter/average question

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Loop counter/average question

    Ok, I have to write this program for school. I'm almost done, but I need help on one part. The program runs a loop 20 times and collects A, P, C, P, P. I have that part, but I also need to find the average number of times it took. This is what I have so far:
    Code:
    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    using namespace std;
    
    int prem();
    int average(int, int);
    
    int main()
    {
    	srand(time(NULL));
    	int a = 0, c = 0, p = 0, sum = 0, counter = 0;
    	
    	for (int i = 1; i < 20; i++)
    	{
    		while (a < 1 || c < 1 || p < 3)
    		{
    			switch (prem())
    			{
    			case 1:
    				a++;
    				break;
    			case 2:
    				c++;
    				break;
    			case 3:
    			case 4:
    				p++;
    				break;
    			}
    			cout<<"A: "<<a<<" C: "<<c<<" P: "<<p<<" Sum: "<<endl;
    		}
    
    		cout<<"Took "<<a + c + p<<" times to get APCPP"<<endl;
    		a = c = p = 0;
    	}
    
    	cout<<"On average, it took "<<average(sum, counter)<<" boxes."<<endl;
    
    	getchar();
    	return 0;
    }
    
    int prem()
    {
    	return 1 + (rand() % 4);
    }
    
    int average(int sum, int counter)
    {
    	return sum;
    }

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Average=sum of elements / amount of elements
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    I know that, I just wasn't sure how to implement that into the program. I figured it out though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width