Reputation: 1
Now, the question is :
Assemble a program that will simulate the work of automatic telephone exchanges. For example, there are 10 subscribers, anyone can call anyone, Each has several conditions: waiting for an answer, calls, says, free. They randomly call each other, the program should show the operation of this system.
And, I figured out how to do some of it, but, don't know how to exactly implement it.
#include <cstdlib>
#include <chrono>
#include <random>
#include <string>
using namespace std;
int i, a;
string state[4]{ "free", "waiting", "calling", "talking" };
int states[4]{ 1,2,3,4 };
int subs[10]{ 1,2,3,4,5,6,7,8,9,10 };
int main(int subs[10], int states[4], string state[4])
{
srand(time(nullptr));
for (int x = 0; x < subs[10]; x++)
{
states[i] = rand() % 4;
states[i] = a;
cout << "Subscriber" << subs[x] << "is" << state[a] << endl << endl;
}
}
Right here, I also have an error in line states[i] = a
Now, what I tried to do there was to randomize a number, and then let it get assigned to any subscriber, and then showed to the person who runs the program. But, well... This is not exactly what the question told me to do. And, I am not sure what I can do, here. I also have limited time for this, with only 12 hours left to do this, because I am a lazy bum. Help please?
Upvotes: 0
Views: 39
Reputation: 7726
Clear version of your code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(void) {
int subs[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
size_t len = sizeof(subs) / sizeof(subs[0]);
string subsStatus[10];
string state[] = {"CALLING", "WAITING", "FREE", "TALKING"};
srand(time(NULL));
for (int i = 0; i < len; i++) {
subsStatus[i] = state[rand() % 4]; // example: Subscriber4 = "TALKING"[3]
cout << "Subscriber" << subs[i] << " is " << subsStatus[i] << endl;
}
return 0;
}
There are some guidelines as side tip:
nullptr
in srand(time())
main()
is only supposed to accept int argc, char *argv[]
and few more arguments, don't define your owns since you're not asking before launch of program from terminal.states[i] = a
defines states[i]
to a
not vice versa.int arr[] = {...}
, it'll be expanded automatically, you must use c++11
and above for this feature.Upvotes: 1