class ship {
public:
int location;
int length;
};
void createship(ship ship1, int gridsize) { //function to set ship1's length and location
srand(time(NULL));
ship1.length = (int)((rand()) % gridsize) / 4 + 1;
ship1.location = (int)(rand()) % gridsize;
}
void insertship(ship ship1, vector <char> grid) { //insert ship into grid, change grid's elements from 'e' to 'f'
for (int temp2= 0; temp2 < ship1.length; temp2++) {
grid[ship1.location + temp2] = 'f';
}
}
int main()
{
int gridsize;
cout << "Gridsize: ";
cin >> gridsize;
cout << "\n";
vector <char> grid(gridsize, 'e');
for (int temp3 = 0; temp3 < grid.size(); temp3++) { //cout vector grid
cout << grid[temp3];
}
cout << "\n";
ship* ship1 = new ship(); //create ship1
createship(*ship1, gridsize);
insertship(*ship1, grid);
cout << (*ship1).length << "\n";
cout << (*ship1).location << "\n";
for (int temp4 = 0; temp4 < grid.size(); temp4++) { //cout vector grid again (with ship)
cout << grid[temp4];
}
return 0;
}
My ship1.length and ship1.location always remain as zero, even though the createship() function is supposed to change it to a random figure? Is there any mistake I made?
Just to add more words below because stackexchange doesn't allow me to add mostly code in my question
You were passing a parameter by value which will create and change a local variable in the function instead of the source variable.
You should pass the parameter by reference.
void createship(ship &ship1, int gridsize) {
srand(time(NULL));
ship1.length = (int)((rand()) % gridsize) / 4 + 1;
ship1.location = (int)(rand()) % gridsize;
}
Anyway,in you case, using a member function maybe a better solution.
class ship {
public:
int location;
int length;
void createship(int gridsize) {
srand(time(NULL));
this->length = (int)((rand()) % gridsize) / 4 + 1;
this->location = (int)(rand()) % gridsize;
}
};
call:
ship1->createship(100);
You pass the ship by value, so the parameter createship gets is a copy of the original object and changing it won't change the original.
Pass a reference/pointer to ship and then changes to the parameter will change the original object.
Or much better, use a constructor.
Related
Write a assign_obj.h file so that the assign_driver file will output the
following. When implementing assign_obj.h think about how efficient your program will run. You must
implement your assign_obj class using a dynamic array.
assign_obj:: assign_obj() function will just set size to zero
assign_obj:: assign_obj(std::string s) function will go through each character of string and convert it to uppercase and push it the last of A
std::ostream& operator<<(std::ostream & out, assign_obj & obj) function will go through each data in obj.A vector and convert integer into string and
than output the data in this format [value:count value:count ....]
class assign_obj{
private:
struct item{
char value;
int count;
};
item *A;
int size;
public:
assign_obj();
assign_obj(std::string);
friend std::ostream& operator<<(std::ostream & out, assign_obj & obj);
}
int main(){
assign_obj ao1("dsdfdf");
std::cout << ao1 << std::endl;
}
This is the output that I want
[ D:1 S:1 D:1 F:1 D:1 F:1 ]
assign_obj:: assign_obj(){
size = 0;
}
assign_obj:: assign_obj(std::string s){
size = s.size();
for(int i = 0; i < s.size(); i++){
char c = std::toupper(s[i]);
A = new item{c,1};
}
}
# std::ostream& operator<<(std::ostream & out, assign_obj & obj){
std::cout<< "size: " << obj.size <<std::endl;
out << "[ ";
for(int i = 0; i < obj.size; i++){
out << obj.A[i].value << ":" << std::to_string(obj.A[i].count) << " ";
}
out << "]" << "\n";
return out;
}
#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?
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;
}
I'm implementing the show Deal Or No Deal, there's a class 'box' which in the main file i used to store the random values of the boxes and than, i saved each box in the vector. im trying now to print in the screen the boxes saved in the vector whit the iterator,without succeeding, any help???
//random assignation of pound value to the 22 boxes
for (int e = 1; e < 23; e++)
{
int pos;
bool op = true;
while (op)
{
pos = rand();
if (pos > 0 && pos < 23)
{
if (myArray[pos][1] == 0)
{
myArray[pos][1] = 1;
op = false;
}
}
}
box b(e, myArray[pos][0]); //creating the class box
game_box.push_back(b); //function of the vector to insert a data in it
}
//show boxes
for (auto a = game_box.begin(); a!= game_box.end(); a++)
{
cout << *a << endl;
}
You first need to remove the dereference operator (*) from the a.
Next you need to add an output operator for box. Assuming that box is a class with member data member1 and member2 this would look something like:
friend std::ostream& operator<< (std::ostream &out, const box& b)
{
out << box.memeber1 << " " << box.member2;
}
The key point is that you need to define this operator for every class.
Once you've done that and got it working you might also like to look at this library which defines << operators for all stl containers. This lets you replace
for (auto a = game_box.begin(); a!= game_box.end(); a++)
{
cout << *a << endl;
}
by simply
cout << game_box << endl;
this works..
class Box
{
float pound_contained;
int box_number;
public:
Box(int box_number, float pound_contained);
int getbox_number();
float getpound_contained();
};
int Box::getbox_number()
{
return this->box_number;
}
float Box::getpound_contained()
{
return this->pound_contained;
}
main()
{
vector<Box> game_box;
Box* boxes = &game_box[i];
cout <<boxes->getbox_number()<<endl;
}
I was writing a program which could read an input file and store the read data in nodes linked by a "link list". However, I was getting a few errors:
In constructor List::List(), no match for 'operator =' in *((List*)this)->List::list[0] = 0
In constructor Polynomial::Polynomial(): no match for 'operator =' in *((Polynomial*)this)->Polynomial::poly = (operator new(400u), (<statement>), ...)
I have a feeling where I do: I try to access a certain node through an array is where I go wrong, however, I can't figure it out much.
Here is the code:
#include <iostream>
#include <fstream>
using namespace std;
enum result{success, failure};
struct Node
{
double coefficient;
int power;
Node();
Node(double coef, int pwr);
};
struct List
{
Node *list[100];
//Default constructor
List();
};
Node::Node()
{
coefficient = 0;
power = 0;
}
List::List()
{
*list[0] = NULL;
}
Node::Node(double coef, int pwr)
{
coefficient = coef;
power = pwr;
}
class Polynomial
{
public:
Polynomial();
result multiply(Polynomial &p, Polynomial &q);
result add(Polynomial p, Polynomial &q);
void initialize(ifstream &file);
void simplify(Polynomial &var);
void print_poly();
~Polynomial();
private:
List *poly; //Store the pointer links in an array
Node first_node;
int val;
};
Polynomial::Polynomial()
{
*poly = new List();
}
Polynomial::void initialize(ifstream &file)
{
int y[20];
double x[20];
int i = 0, j = 0;
//Read from the file
file >> x[j];
file >> y[j];
first_node(x[j], y[j++]); //Create the first node with coef, and pwr
*poly->list[i] = &first_node; //Link to the fist node
//Creat a linked list
while(y[j] != 0)
{
file >> x[j];
file >> y[j];
*poly->list[++i] = new Node(x[j], y[j++]);
}
val = i+1; //Keeps track of the number of nodes
}
Polynomail::result multiply(Polynomial &p, Polynomial &q)
{
int i, j, k = 0;
for(i = 0; i < p.val; i++)
{
for(j = 0; j < q.val; j++)
{
*poly->list[k] = new Node(0, 0);
*poly->list[k].coefficient = (p.poly->list[i].coefficient)*(q.poly->list[j].coefficient);
*poly->list[k++].power = (p.poly->list[i].power)+(q.poly->list[j].power);
}
}
val = k+1; //Store the nunber of nodes
return success;
}
Polynomial::void simplify(Polynomial &var)
{
int i, j, k = 0;
//Create a copy of the polynomial
for(j = 0; j < var.val; j++)
{
*poly->list[j] = new Node(0, 0);
*poly->list[j].coefficient = var.poly->list[j].coefficient;
*poly->list[j].power = var.poly->list[j].power;
}
//Iterate through the nodes to find entries which have the same power and add them, otherwise do nothing
for(k = 0; k < var.val; k++)
{
for(i = k; i < var.val;)
{
if(*poly->list[k].power == var.poly->list[++i].power)
{
if(*poly->list.power[0] == 0)
{
NULL;
}
else
{
*poly->list[k].coefficient = *poly->list[k].coefficient + var.poly->list[i].ceofficient;
var.poly->list[i] = Node(0, 0);
}
}
}
}
}
Polynomial::void print_pol()
{
int i = 0;
for(i = 0; i < temp.val; i++)
{
cout << "Coefficient: " << temp.poly->list[i].coefficient << ", and " << "Power: " << temp.poly->list[i].power << endl;
}
}
The problem is a wrong dereference. Line 34 should probably be
list[0] = NULL; // remove the *
You try to assign the value NULL to a variable of the type Node, but you probably mean a pointer to Node.
The very same is true in line 63.
In addition, line 66 sould probably b:
void Polynomial::initialize(ifstream &file) // start with return type