changing variables within loop from outside class - class

I have a time class and a Build class. The harvest function increases the minerals count for the entire time, but the nunmber of minerals depends on the number of available workers. As soon as a build starts, the number of workers decreases.
So, how can i decrease the number of workers, within the loop, and not after the loop has finished.
I want minerals to keep increasing, but the number of workers should increase and decrease based on the Build call.
class Time {
private:
int minerals = 50;
int workers = 6;
int vespene = 0;
int supply = 6;
int time = 0;
public:
Time ();
int Time_counter();
void harvest();
int mineral_count();
int worker_count();
int supply_count();
void update(int m, int w, int s, int t);
void Display();
};
Time::Time ()
{
for (int i =0; i<9; i++)
{
update(minerals, workers, supply, time);
harvest();
time = i;
// Time_counter();
}
}
int Time::Time_counter() {
return time;
}
void Time::harvest() {
minerals+= workers * (0.7);
}
int Time:: mineral_count() {
return minerals;
}
int Time::worker_count() {
//return workers.size();
return workers;
}
int Time::supply_count() {
return supply;
}
void Time:: update(int m, int w, int s, int t) {
minerals = m;
workers = w;
supply = s;
time = t;
cout << "No. of Minerals: " <<minerals <<"\n" <<"No.of Workers: " <<workers <<"\n"
<< "No. of Supply: " <<supply <<"\n" <<"Current Time: " <<time << endl;
}
void Time:: Display ()
{
cout << "No. of Minerals: " <<minerals <<"\n" <<"No.of Workers: " <<workers <<"\n"
<< "No. of Supply: " <<supply <<"\n" <<"Current Time: " <<time << endl;
}
class Build: public Time {
public:
void Build_create();
};
void Build::Build_create() {
int build_count=0;
SCV obj;
int m = obj.mineral_count();
int w = obj.worker_count();
int s = obj.supply_count();
int t; // = obj.Time_counter();
if (m >= 50 && biuld_count<2)
{
cout << "Build start....";
m-= 50;
obj.update(m,w,s,t);
obj.Display ();
w-=1;
for (t = obj.Time_counter(); t <obj.Time_counter()+ 10; t++)
{cout<< "Building...";}
cout << " Build complete" << endl;
scv_count++;
w += 1;
s += 1;
t = obj.Time_counter();
obj.update(m, w, s, t);
obj.Display ();
}
else if (m<50)
{
cout << "Cannot build " << endl;
}
else if (build_count==2)
{
cout<<"two already built.." <<endl;
}

Related

Sort an array A using Quick Sort. Using reccursion

#include<iostream>
using namespace std;
void quickSort(int input[], int start, int end)
{
// your code goes here
}
void quickSort(int input[], int size)
{
quickSort(input, 0, size - 1);
}
*/
void swap(int* a,int* b){
int temp=*a;
*a=*b;
*b=temp;
}
int count(int input[],int start,int end ){
static int c=0;
if(start==end)
return c;
if(input[start]>input[end])
c++;
return count(input,start,end-1);
}
int partionArray(int input[],int start,int end ){
int c=count(input,start,end);
int pi=c+start;
swap(&input[start],&input[pi]);
int i=start;
int j=end;
while(i<pi&&j>pi)
{
if(input[i]<input[pi])
{
i++;
}
else if(input[j]>=input[pi])
{
j--;
}
else
{
swap(&input[i],&input[j]);
i++;
j--;
}
}
return pi;
}
void qs(int input[],int start, int end){
if(start>=end)
return;
int pi=partionArray(input,start,end);
qs(input,start,pi-1);
qs(input,pi+1,end);
}
void quickSort(int input[], int size) {
qs(input,0,size-1);
}
int main(){
int n;
cin >> n;
int *input = new int[n];
for(int i = 0; i < n; i++) {
cin >> input[i];
}
quickSort(input, n);
for(int i = 0; i < n; i++) {
cout << input[i] << " ";
}
delete [] input;
}
Sort an array A using Quick Sort. Using reccursion is the question.
Input format :
Line 1 : Integer n i.e. Array size
Line 2 : Array elements (separated by space)
Output format :
Array elements in increasing order (separated by space)
Constraints :
1 <= n <= 10^3
What did i do wrong in this code pls can any one explain?Is every thing right with this code?

Omnet++ error when simulating multiple submodules

I am trying to simulate 10 submodules. But omnet++ gives an error when the number of submodules are greater than five. here is the error:
(omnetpp::cMessage)sampleEvent: par(int): Has no parameter #4 -- in module (Sam1) Net.sampler1[4] (id=6), during network initialization
My .ned file
simple Sam1
{
parameters:
#display("i=block/routing");
gates:
inout gate[]; // declare two way connections
}
simple Svr1
{
parameters:
#display("i=block/process");
gates:
inout gate[]; // declare two way connections
}
network Net
{
#display("bgb=670.56,274.32");
types:
channel Ch extends ned.DelayChannel
{
delay = 100ns;
}
submodules:
sampler1[10]: Sam1 {
#display("p=334,127;is=l");
}
server: Svr1 {
parameters:
#display("i=,gold;p=109.21999,88.899994");
}
connections:
for i=0..9 {
server.gate++ <--> Ch <--> sampler1[i].gate++;
}
}
omnetpp.ini file
[General]
[Config Net]
network = Net
record-eventlog = true
sam1 class
#include <stdio.h>
#include <string.h>
#include <omnetpp.h>
using namespace omnetpp;
class Sam1 : public cSimpleModule
{
private:
simtime_t timeout; // timeout
cMessage *sampleEvent; // holds pointer to the self-message
int seq; // message sequ ence number
cMessage *message; // message that has to be re-sent on timeout
double vK;
double vM;
double threshold = 1.0;
int counter;
double xmean = 0.01;
double xsigma = 1;
double sensigma = 0.1;
double x;
double zOut = 0.0;
double previousSentTime = 0.0;
int samplerID;
double xSum;
long numSent;
long numReceived;
long totalEvents;
cLongHistogram SampleStats;
cOutVector SampleVector;
cLongHistogram interSampleStats;
cOutVector interSampleVector;
cLongHistogram sentSampleStats;
cOutVector sentSampleVector;
cOutVector constructedSignal;
public:
Sam1();
virtual ~Sam1();
protected:
virtual cMessage *generateNewMessage();
virtual void sampleAndSend(cMessage *msg);
virtual void initialize() override;
virtual void handleMessage(cMessage *msg) override;
// The finish() function is called by OMNeT++ at the end of the simulation:
virtual void finish();
};
Define_Module(Sam1);
Sam1::Sam1()
{
sampleEvent = message = nullptr;
}
Sam1::~Sam1()
{
cancelAndDelete(sampleEvent);
delete message;
}
void Sam1::initialize()
{
// Initialize variables.
vM = 0;
seq = 0;
timeout = 100.0; // in every 100 second
sampleEvent = new cMessage("sampleEvent");
xSum = 0.0;
counter = 1;
numSent = 0;
numReceived = 0;
totalEvents = 0;
WATCH(numSent);
WATCH(totalEvents);
samplerID = getIndex();
interSampleStats.setName("interSampleStats");
interSampleStats.setRangeAutoUpper(0, 10, 1.5);
interSampleVector.setName("interSample");
SampleStats.setName("SampleStats");
// interSampleStats.setRangeAutoUpper(0, 10, 1.5);
SampleVector.setName("Sample");
sentSampleStats.setName("sentSampleStats");
// interSampleStats.setRangeAutoUpper(0, 10, 1.5);
sentSampleVector.setName("sentSample");
constructedSignal.setName("constructedSignal");
// Generate and send initial message.
EV << "Sending initial message\n";
message = generateNewMessage();
sampleAndSend(message);
// scheduleAt(simTime()+timeout, sampleEvent);
}
void Sam1::handleMessage(cMessage *msg)
{
if (msg == sampleEvent) {
// EV << "sampling Again\n";
// Ready to send another one.
message = generateNewMessage();
sampleAndSend(message);
}
}
cMessage *Sam1::generateNewMessage()
{
// Generate a message
cMessage *msg = new cMessage("sampleEvent");
double t1 = simTime().dbl() * 1000;
msg->addPar("sampleValue");
x = normal(xmean, xsigma) + normal(xmean, sensigma);
xSum = x + xSum;
msg->par("sampleValue").setDoubleValue(xSum);
msg->addPar("counter");
msg->par("counter").setLongValue(counter);
msg->addPar("generatedTimestamp");
msg->par("generatedTimestamp").setDoubleValue(t1);
msg->addPar("samplerName");
msg->par(samplerID);
counter = counter +1;
totalEvents++;
return msg;
}
void Sam1::sampleAndSend(cMessage *msg)
{
double sampleValue = msg->par("sampleValue").doubleValue();
double genTimestamp = msg->par("generatedTimestamp").doubleValue();
cMessage *copy = (cMessage *)msg->dup();
vK = sampleValue-vM;
// EV << vK << " vK \n";
if(fabs(sampleValue-vM) > threshold){
vM = sampleValue;
zOut = sampleValue;
// In this example, we just pick a random gate to send it on.
// We draw a random number between 0 and the size of gate `out[]'.
int n = gateSize("gate");
int k = intuniform(0, n-1);
send(copy, "gate$o", k);
double t2 = simTime().dbl() * 1000; // sample time in milli seconds
double interSample = t2 - previousSentTime;
previousSentTime = genTimestamp;
numSent++;
interSampleVector.record(interSample);
interSampleStats.collect(interSample);
sentSampleVector.record(1);
sentSampleStats.collect(1);
}else{
zOut = vM;
sentSampleVector.record(0);
sentSampleStats.collect(0);
}
EV << "vM " << vM << endl;
// EV << "sampling Again. node " << simTime() << endl;
SampleVector.record(sampleValue);
SampleStats.collect(sampleValue);
constructedSignal.record(zOut);
scheduleAt(simTime()+timeout, sampleEvent);
}
void Sam1::finish(){
// This function is called by OMNeT++ at the end of the simulation.
EV << "Sent: " << numSent << endl;
EV << "Total Events: " << totalEvents << endl;
float rate = (float)numSent/totalEvents;
EV << "Traffic Reduction: " << rate << endl;
EV << "Threshold: " << threshold << endl;
EV << "Signal Mean: " << xmean << endl;
EV << "Signam sigma: " << xsigma << endl;
EV << "Sensor Sigma: " << sensigma << endl;
EV << "Inter Sample Time, min: " << interSampleStats.getMin() << endl;
EV << "Inter Sample Time, max: " << interSampleStats.getMax() << endl;
EV << "Inter Sample Time, mean: " << interSampleStats.getMean() << endl;
EV << "Inter Sample Time, stddev: " << interSampleStats.getStddev() << endl;
recordScalar("#sent", numSent);
recordScalar("#total", totalEvents);
interSampleStats.recordAs("inter sample");
SampleStats.recordAs("sample");
sentSampleStats.recordAs("sent sample");
}
/**
* Sends back an acknowledgement -- or not.
*/
class Svr1 : public cSimpleModule
{
protected:
virtual void handleMessage(cMessage *msg) override;
};
Define_Module(Svr1);
void Svr1::handleMessage(cMessage *msg)
{
double sampleValue = msg->par("sampleValue").doubleValue();
long counter = msg->par("counter").longValue();
long samplerID = msg->par("samplerName").longValue();
EV << sampleValue << " received, sending back an acknowledgment from sampler "<< samplerID << ". counter " << counter << "\n";
delete msg;
// In this example, we just pick a random gate to send it on.
// We draw a random number between 0 and the size of gate `out[]'.
int n = gateSize("gate");
int k = msg->getArrivalGate()->getIndex();
EV << "n: " << n << "k: " << k << endl;
// send(new cMessage("ack"), "gate$o", k);
}

When calling a member function of a class, I get Error C3867

In my displayData member function of the car class, the error says I should create a pointer to the member, do I have to create pointers to the member? If I do how so? I completely forget pointers. Do i make the object a pointer then point to the displayData member function? I get no red squiggles just an error message saying "use '&' to create a pointer to the member. I tried but had no luck.
#include<iostream>
#include<string>
using namespace std;
class Car
{
private:
int xVal, yVal, zVal;
protected:
public:
Car() { int xVal = 0; int yVal = 0; int zVal = 0; }
Car(int x,int y,int z) { xVal = x; yVal = y; zVal = z; }
~Car() {};
int getX() { return xVal; }
int getY() { return yVal; }
int getZ() { return zVal; }
void changeX(int n) { xVal = n; }
void changeY(int n) { yVal = n; }
void changez(int n) { zVal = n; }
virtual void getData();
void displayData();
};
class Sensor : public Car
{
private:
string sensorType;
protected:
public:
Sensor() { sensorType = "EMPTY"; }
Sensor(int x, int y, int z, string type) :Car(x,y,z) { sensorType = type; }
};
void Car::displayData()
{
cout << "The x values is: " << getX() << endl;
cout << "The y values is: " << getY() << endl;
cout << "The z values is: " << getZ() << endl;
}
int main()
{
Sensor n1;
Sensor n2(20,30,40, "Accelerometer");
n1.displayData;
n2.displayData;
return 0;
}
There is a syntax error in calling the function.
it should be
n1.displayData();
instead of
n1.displayData;

How do I change a pointer variable's value and keep the changes outside of a function without pass-by-reference?

I am doing a project for a class writing C-String-editing functions. 3/5 of the functions I have to write change the size of the char arrays I have to use, and they are being read through an ifstream input. Here is the program:
#include <iostream>
#include <fstream>
using namespace std;
void stringCopy(char *A, char *B);
bool stringCompare(char *A, char *B);
void stringConcatenation(char *A, char *B); //added const to make sure b is never changed
int stringPosition(char *A, char B);
int stringLength(char *A);
//-------------------MY-FUNCTIONS----------------------
int cStringLen(const char*); //finds string length, but doesn't account for null char
void reSize(char*&, int len, int newLen);
void input(char*& A, istream& is);
void printMessage(const char* word1, const char* word2, const char* message);
int main()
{
ifstream ifs{"input.txt"};
ofstream ofs{"output.txt"};
char* word1 = "";
char* word2 = "";
input(word1, ifs);
input(word2, ifs);
printMessage(word1, word2, "stringCopy()");
stringCopy(word1, word2);
printMessage(word1, word2, "after stringCopy()");
cout << endl;
input(word1, ifs);
input(word2, ifs);
printMessage(word1, word2, "stringCompare()");
if(stringCompare(word1, word2))
{
cout << "They match!" << endl;
}
else
{
cout << "They don't match!" << endl;
}
stringCopy(word1, word2);
printMessage(word1, word2, "comparing after stringCopy()");
if(stringCompare(word1, word2))
{
cout << "They match!" << endl;
}
else
{
cout << "They don't match!" << endl;
}
cout << endl;
input(word1, ifs);
input(word2, ifs);
printMessage(word1, word2, "stringConcatenation()");
stringConcatenation(word1, word2);
printMessage(word1, word2, "after stringConcatenation()");
cout << endl;
input(word1, ifs);
input(word2, ifs);
printMessage(word1, word2, "stringPosition()");
cout << "Searching for 'm' in word1..." << endl << "position returned is: " << stringPosition(word1, 'm') << endl;
cout << "Searching for 'n' in word2..." << endl << "position returned is: " << stringPosition(word2, 'n') << endl;
cout << endl;
input(word1, ifs);
cout << "stringLength()" << endl;
cout << "word1: " << word1 << endl;
cout << "The length of word1 is: " << stringLength(word1) << endl;
cout << "after stringLength()" << endl;
cout << "word1: " << word1 << endl;
return 0;
}
void stringCopy(char *A, char *B)
{
///GETTING THE SIZES OF BOTH ARRAYS
int counterA = cStringLen(A) + 1;
int counterB = cStringLen(B) + 1;
///MAKES SURE BOTH ARE THE SAME SIZE BEFORE COPYING
if(counterA < counterB)
{
reSize(A, counterA, counterB);
}
else
{
reSize(A, counterB, counterA);
}
///THE COPY
for(int i = 0; i < counterB; i++) *(A + i) = *(B + i); //each character is copied to A from B
}
bool stringCompare(char *A, char *B)
{
///getting length of one string
int counter = cStringLen(A);
///will move through string until diff char found
for(int i = 0; i < counter + 1; i++)
{
if(*(A + i) != *(B + i))
{
return false;
}
}
return true;
}
void stringConcatenation(char *A, char *B) //added const to make sure b is never changed
{
///getting length of both strings
int counterA = cStringLen(A)+1;
int counterB = cStringLen(B)+1;
///putting the length of both together for new string
const int COUNTERS = counterA + counterB - 1;
///making A the size of both strings - 1
reSize(A, counterA, COUNTERS);
///copying b to the parts of a past the original
for(int i = 0; i < counterB; i++)
{
*(A + (counterA - 1) + i) = *(B + i); //will override the '/0' char of A
}
}
int stringPosition(char *A, char B)
{
int counter = cStringLen(A) + 1;
///searching through string for char
for(int i = 0; i < counter; i++)
{
if(*(A + i) == B)
{
return i; //found!
}
}
///checking if b == '\0' and a '\0' isn't found somewhere before last spot of A
if(B == '\0')
{
return counter;
}
return -1; //not found
}
int stringLength(char *A)
{
int counter = cStringLen(A) + 1;
char* car = new char[counter + 1];
for(int i = 0; i < counter; i++)
{
*(car + 1 + i) = *(A + i);
}
*(car + 0) = counter;
delete[] A;
A = car;
/**
* Will take string as param.
* Shifts all characters to the right by one and store the length of the string in position 0.
- Length doesn't include position 0.
*/
return counter; //temp
}
//-----------------------------------------MY FUNCTIONS---------------------------------------------------------------------------
int cStringLen(const char* A) //finds string length, but doesn't account for null char
{
int counter = 0;
while(*(A + counter) != '\0')
{
counter++;
}
return counter;
}
void reSize(char*& A, int len, int newLen)
{
char* car = new char[newLen];
for(int i = 0; i < newLen; i++)
{
if(i < len)
{
*(car + i) = *(A + i);
}
else if(i >= len && i < newLen)
{
*(car + i) = '\0';
}
}
delete[] A;
A = car;
}
void input(char*& A, istream& is)
{
int wordSize = 0;
int arrSize = 1;
char c = 'o'; //checking char
char* car = new char[arrSize];
while((!(is.eof())) && (c != ' ' && c != '\t' && c != '\n'))
{
is.unsetf(ios_base::skipws);
is >> c;
if(is.eof())
{
delete[] A;
A = car;
return;
}
if(c != ' ' && c != '\t' && c != '\n')
{
if(wordSize == arrSize)
{
reSize(car, arrSize, arrSize * 2);
}
*(car + wordSize) = c;
}
wordSize++;
}
is.setf(ios_base::skipws);
delete[] A;
A = car;
}
void printMessage(const char* word1, const char* word2, const char* message)
{
cout << message << endl;
cout << "word1: " << word1 << endl << "word2: " << word2 << endl;
}
I thought I got it all done just fine. Keep in mind that I added the "&" operator after each of the pointer parameters already. Here is how they were before:
void stringCopy(char *&A, char *B);
bool stringCompare(char *A, char *B);
void stringConcatenation(char *&A, char *B); //added const to make sure b
is never changed
int stringPosition(char *A, char B);
int stringLength(char *&A);
But, when I got to class, my teacher said we weren't allowed to change the function headers in any way. So, I am stuck passing by value for the assignment. The problem is that I have no way of changing the c-strings outside the editing functions now. Any changes I do to them stay inside there.
It all compiles just fine, and, if I make the pointers pass-by-reference, the program runs flawlessly. I am just wondering how I could change the values of the c-strings outside of the editing functions. This assignment is starting to become a pain (so many f***ing restrictions).
I think what your teacher wants you to do is to change the value at the character pointer instead of creating a new string.
So instead trying to reassigning parameter A to a new char* you change the value that A points to in memory. That way the method that called your function still points to that same memory and when they access that location the get the value you changed from within your function.

How to call functions in the main function by use class?

#include <iostream>
using namespace std;
class Money
{
public:
Money();
Money(int, int);
void setDollars(int d);
void setCents(int c);
int getDollars() const;
int getCents() const;
double getAmount(int, int);
private:
int dollars;
int cents;
};
Money::Money()
{
dollars = 0;
cents = 0;
}
Money::Money(int d, int c)
{
dollars = d;
cents = c;
}
void Money::setDollars(int d)
{
dollars = d;
}
void Money::setCents(int c)
{
if (c > 100)
{
c = c % 100;
dollars = dollars + (c / 100);
}
//update the dollars member if the cents input argument is 100 or larger.
cents = c;
}
int Money::getDollars() const
{
return dollars;
}
int Money::getCents() const
{
return cents;
}
double Money::getAmount(int d, int c)
{
return dollars + (cents / 100);
}
int main()
{
int dlr;
int cts;
//double amt;
cout << "Please input amount of dollars: ";
cin >> dlr;
cout << "Please input amount of cents: ";
cin >> cts;
Money money0(dlr, cts);
cout << "The money object1 has amount: " << money0.getDollars() << "." << money0.getCents() << endl;
cout << "The money object2 has amount: " << money0.getAmount() << endl;
//I try to call the functions to tell user how much is it
//use both ways to tell user how much is it(1.getDollars+getCents, 2. getAmount)
return 0;
system("pause");
}
When I try to call the get functions in the main, the errors show up. How to call those functions correctly?
Here is my assignment:
Define a class named Money that stores a monetary amount. The class should have two private integer variables, one to store the number of dollars and another to store the number of cents. Include a default constructor that initializes the amount to $0.00 and an overloaded parameterized constructor to initialize any non-zero amount(as demonstrated in the example program below).