|
-
Oct 31st, 2001, 08:58 PM
#1
Thread Starter
Fanatic Member
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;
}
-
Nov 1st, 2001, 11:25 AM
#2
transcendental analytic
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.
-
Nov 1st, 2001, 12:08 PM
#3
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|