How to access array elements in a struct with pointors C++ - c++

I have the following function and the following struct.
void function(TestStruct *array) {
for (int i = 0; i < 3; i++){
array[i].value = 0;
array[i].remove = true;
}
}
struct TestStruct
{
int value;
bool remove;
};
int main(){
TestStruct sampleStruct[3];
sampleStruct[0].value = 1;
sampleStruct[1].value = 2;
sampleStruct[2].value = 3;
sampleStruct[0].value = false;
sampleStruct[1].value = false;
sampleStruct[2].value = false;
function(sampleStruct);
}
However, the values in sampleStruct do not change.
When I was debugging, the function had only access to the first index of the array 0x0025fb64 {Value = 1 remove = false}, but even the first index did not change.
In other words, I'm trying to access the full sampleStruct address to change the values.

Well, for me it works fine. The only thing I changed is .value = false to .remove = false (because this is probably how you wanted to do it). code:
#include <iostream>
using namespace std;
struct TestStruct
{
int value;
bool remove;
};
void function(TestStruct *array) {
for (int i = 0; i < 3; i++){
array[i].value = 0;
array[i].remove = true;
}
}
int main(){
TestStruct sampleStruct[3];
sampleStruct[0].value = 1;
sampleStruct[1].value = 2;
sampleStruct[2].value = 3;
sampleStruct[0].remove = false; // changed to .remove
sampleStruct[1].remove = false; // changed to .remove
sampleStruct[2].remove = false; // changed to .remove
function(sampleStruct);
for (int i = 0; i < 3; i++) // prints "0 1" 3 times
cout << sampleStruct[i].value << ' ' << sampleStruct[i].remove << '\n';
}

Related

Why does popping off my stack return garbage instead of the initial variables?

When I run my program, it pushed numbers into the array perfectly fine. But when it pops them, and then prints them, I get garbage numbers. Does the problem have to do something with my main function?
Or did I not initialize my array correctly in my Stack class? I had some problems with my constructor at first, but it seemed to work fine after making some adjustments.
Is there anything that stands out as to why I am receiving the garbage numbers after running my script?
#include<iostream>
#include<cstdlib>
#ifndef MYSTACK_H
#define MYSTACK_H
#include<iostream>
#include<new>
using namespace std;
class MyStack
{
private:
int *list;
int top;
int max;
public:
MyStack(int m)
{
int max = m;
list = new int[max];
int top = -1;
}
~MyStack()
{
delete[] list;
}
int push(int);
int pop();
int peek(int &a) const;
};
int MyStack::push(int a)
{
if (top < max - 1)
{
top = top + 1;
list[top] = a;
return 0;
}
return -1;
}
int MyStack::pop()
{
if (top > -1)
{
top = top - 1;
return 0;
}
return -1;
}
int MyStack::peek(int &a) const
{
if (top > -1)
{
return(list[top]);
return 0;
}
return -1;
}
#endif
int main()
{
MyStack m(5);
for (int i = 0; i < 6; i++)
{
int x = 1 + rand() % 100;
cout << x << "\t";
m.push(x);
}
cout << "\n";
for (int i = 0; i < 6; i++)
{
int x;
m.peek(x);
cout << x << "\t";
m.pop();
}
cout << "\n";
system("pause>nul");
}
top and max are not set after calling constructor of MyStack, you are creating local variable and members are not affected:
int max = m; // local
list = new int[max];
int top = -1; // local
change to
max = m;
list = new int[max];
top = -1;
Parameter a is never used in your peek() function:
int MyStack::peek(int &a) const
{
if (top > -1)
{
return(list[top]); // you return the value instead of assigning it to "a"
return 0; // unreachable by the way
}
return -1;
}

accessing to value in two dimensional pointer array in c++

I want to declare two dimensional array with variable size.
I wrote the following code but something goes wrong!
int **p2DArray;
p2DArray = new int*[target_counter_new];
for (int i = 0; i < target_counter_new; ++i)
{
p2DArray[i] = new int[target_counter_old];
}
for(int i_oghli=0;i_oghli<target_counter_new;i_oghli++)
for(int j_oghli=0;j_oghli<target_counter_old;j_oghli++)
{
p2DArray[i_oghli][j_oghli]=i_oghli+10;
cout<<p2DArray[i_oghli][j_oghli];
}
what is problem here ?
#include <iostream>
using namespace std;
const int target_counter_new = 4;
const int target_counter_old = 4;
int main() {
int **p2DArray;
p2DArray = new int*[target_counter_new];
for (int i = 0; i < target_counter_new; ++i) {
p2DArray[i] = new int[target_counter_old];
}
for(int i_oghli=0;i_oghli<target_counter_new;i_oghli++) {
for(int j_oghli=0;j_oghli<target_counter_old;j_oghli++) {
p2DArray[i_oghli][j_oghli]=i_oghli+10;
cout<<p2DArray[i_oghli][j_oghli] << " ";
}
cout << endl;
}
// don't forget to delete the array
for (int i = 0 ; i < target_counter_new; ++i) {
delete [] p2DArray[i];
}
delete [] p2DArray;
return 0;
}
Check here : code
There doesn't appear to be any problem.

sort struct array in c++

I have an array of a Struct and trying to sort them alphabetically by lname ,i tried bubble sort but when we have some empty items in our array bubble sort won't work correctly .so is there any function to sort these items ?
This is my code :
#include<iostream>
#include<algorithm>
using namespace std;
struct user {
char lname[30];
int userid;
};
user libuser[1000];
int main(){
strcpy(libuser[0].lname,"");
libuser[0].userid = 0;
strcpy(libuser[1].lname,"backzade");
libuser[1].userid = 1;
strcpy(libuser[2].lname,"akhondali");
libuser[2].userid = 2;
strcpy(libuser[3].lname,"sayidian");
libuser[3].userid = 3;
strcpy(libuser[4].lname,"navah");
libuser[4].userid = 4;
strcpy(libuser[5].lname,"mostarab");
libuser[5].userid = 5;
libuser[6].userid = 0;
strcpy(libuser[7].lname,"");
libuser[7].userid = 0;
strcpy(libuser[8].lname,"");
libuser[8].userid = 0;
strcpy(libuser[9].lname,"borobaba");
libuser[9].userid = 9;
strcpy(libuser[10].lname,"divune");
libuser[10].userid = 10;
for(int i=1;i<1000;i++)
if(libuser[i].userid!=0)
cout<<libuser[i].lname<<"\n";
system("PAUSE");
return 0;
}
This code just can be one method among many good methods. I just use std::sort and lambda.
I hope this will help you a little. Happy new year!
std::sort (std::begin(libuser), std::end(libuser), [&](const user& first, const user& second) -> bool
{
return (first.lname[0] < second.lname[0]);
});
#include <iostream>
#include <algorithm>
using namespace std;
struct user
{
char lname[30];
int userid;
};
user libuser[1000];
int elementCount = 0;
int main()
{
strcpy(libuser[0].lname,"");
libuser[0].userid = 0;
strcpy(libuser[1].lname,"backzade");
libuser[1].userid = 1;
strcpy(libuser[2].lname,"akhondali");
libuser[2].userid = 2;
strcpy(libuser[3].lname,"sayidian");
libuser[3].userid = 3;
strcpy(libuser[4].lname,"navah");
libuser[4].userid = 4;
strcpy(libuser[5].lname,"mostarab");
libuser[5].userid = 5;
libuser[6].userid = 0;
strcpy(libuser[7].lname,"");
libuser[7].userid = 0;
strcpy(libuser[8].lname,"");
libuser[8].userid = 0;
strcpy(libuser[9].lname,"borobaba");
libuser[9].userid = 9;
strcpy(libuser[10].lname,"divune");
libuser[10].userid = 10;
//sort
for(int i = 0; i < 1000; i++)
{
for(int j = 0; j < 1000 - 1; j++)
{
if(strcmp(libuser[j].lname, libuser[j + 1].lname) > 0) //change to < 0 for descending sort
{
user temp = libuser[j];
libuser[j] = libuser[j + 1];
libuser[j + 1] = temp;
}
}
}
for(int i = 1; i < 1000; i++)
{
if(libuser[i].userid!=0)
{
cout<<libuser[i].lname<<"\n";
}
}
system("PAUSE");
return 0;
}

2D vector class variable for a genetic algorithm gives a bad_alloc error

I'm writing a genetic algorithm for which I'm creating a "crossover" operator as a class object that is passed the two parent "chromosomes" Because the input and therefore the output chromosomes are variable lengths, my idea was two divide the input chromosomes and place in a sort of storage class variable, then resize the input chromosomes, and then finally refill the input chromosomes. I'm getting a bad_alloc error, however. If someone could spot my error I'd very much appreciate the help.
Thanks! My class code is below. Note that "plan_vector" is a 2d vector of int types.
#include <iostream>
#include <vector>
#include <eo>
class wetland_vector : public std::vector<int> {
public:
wetland_vector() : std::vector<int>(1, 0) {
}
};
std::istream& operator>>(std::istream& is, wetland_vector& q) {
for (unsigned int i = 0, n = 1; i < q.size(); ++i) {
is >> q[i];
}
return is;
}
std::ostream& operator<<(std::ostream& os, const wetland_vector& q) {
os << q[0];
for (unsigned int i = 1, n = 1; i < q.size(); ++i) {
os << " " << q[i];
}
os << " ";
return os;
}
class wetland_vector_Init : public eoInit<wetland_vector> {
public:
void operator()(wetland_vector& q) {
for (unsigned int i = 0, n = q.size(); i < n; ++i) {
q[i] = rng.random(10);
}
}
};
class plan_vector : public eoVector<double, wetland_vector> {
};
int read_plan_vector(plan_vector _plan_vector) {
for (unsigned i = 0; i < _plan_vector.size(); i++) {
//Call function that reads Quad[1]
//Call function that reads Quad[2]
//etc
return 0;
}
return 0;
};
class eoMutate : public eoMonOp<plan_vector> {
int subbasin_id_min;
int subbasin_id_max;
int wetland_id_min;
int wetland_id_max;
bool operator() (plan_vector& _plan_vector) {
//decide which Quad to mutate
int mutate_quad_ID = rng.random(_plan_vector.size());
//decide which Gene in Quad to mutate
int mutate_gene_ID = rng.random(_plan_vector[mutate_quad_ID].size());
//mutation procedure if first slot in the Quad is selected for mutation
if (mutate_quad_ID = 0) {
_plan_vector[mutate_quad_ID][mutate_gene_ID] = rng.random(subbasin_id_max);
}
//mutation procedure if second slot in the Quad is selected for mutation
if (mutate_quad_ID = 1) {
_plan_vector[mutate_quad_ID][mutate_gene_ID] = rng.random(subbasin_id_max);
}
//note: you'll need to add more for additional wetland characteristics
return true;
};
public:
void set_bounds(int, int, int, int);
};
void eoMutate::set_bounds(int a, int b, int c, int d) {
subbasin_id_min = a;
subbasin_id_max = b;
wetland_id_min = c;
wetland_id_max = d;
}
double evaluate(const plan_vector& _plan_vector) {
int count = 0;
for (int i = 0; i < _plan_vector.size(); i++) {
for (int j = 0; j < _plan_vector[i].size(); j++) {
count += _plan_vector[i][j];
}
}
return (count);
}
class eoQuadCross : public eoQuadOp<plan_vector> {
public:
std::string className() const {
return "eoQuadCross";
}
plan_vector a1;
plan_vector a2;
plan_vector b1;
plan_vector b2;
bool operator() (plan_vector& a, plan_vector& b) {
int cross_position_a = rng.random(a.size() - 1);
int cross_position_b = rng.random(b.size() - 1);
for (int i = 0; i < cross_position_a; i++) {
a1.push_back(a[i]);
}
for (int i = cross_position_a; i < a.size(); i++) {
a2.push_back(a[i]);
}
for (int i = 0; i < cross_position_b; i++) {
b1.push_back(b[i]);
}
for (int i = cross_position_b; i < b.size(); i++) {
b2.push_back(b[i]);
}
int size_a = b2.size() + a1.size();
int size_b = a2.size() + b1.size();
a.resize(size_a);
b.resize(size_b);
for (int i = 0; i < b2.size(); i++) {
a.push_back(b2[i]);
}
for (int i = 0; i < a1.size(); i++) {
a.push_back(a1[i]);
}
for (int i = 0; i < a2.size(); i++) {
b.push_back(a2[i]);
}
for (int i = 0; i < b1.size(); i++) {
b.push_back(b1[i]);
};
//Return bool
return true;
}
};
int main() {
unsigned int vec_size_min = 1;
unsigned int vec_size_max = 10;
unsigned int pop_size = 100;
//BEGIN COPY PARAMETRES
const unsigned int MAX_GEN = 100;
const unsigned int MIN_GEN = 5;
const unsigned int STEADY_GEN = 50;
const float P_CROSS = 0.8;
const float P_MUT = 0.5;
const double EPSILON = 0.01;
double SIGMA = 0.3;
const double uniformMutRate = 0.5;
const double detMutRate = 0.5;
const double normalMutRate = 0.5;
//END COPY PARAMETERS
rng.reseed(1);
//Create population
wetland_vector_Init atom_init;
eoInitVariableLength<plan_vector> vec_init(vec_size_min, vec_size_max, atom_init);
eoPop<plan_vector> pop(pop_size, vec_init);
//Create variation operators
eoMutate mutate;
mutate.set_bounds(1, 453, 1, 4);
eoQuadCross crossover;
eoDetTournamentSelect<plan_vector> select(3);
eoSGATransform<plan_vector> transform(crossover, .5, mutate, .2);
//Create fitness function
eoEvalFuncPtr<plan_vector> eval(evaluate);
//Evaluate initial population and cout
apply<plan_vector > (eval, pop);
std::cout << pop << std::endl;
//Set GA for execution and execute
eoGenContinue<plan_vector> GenCount(5);
eoSGA<plan_vector> gga(select, crossover, .5, mutate, .1, eval, GenCount);
gga(pop);
//cout final population and end
std::cout << pop << std::endl;
std::cout << "The End" << std::endl;
}
a1.~vector();
a2.~vector();
b1.~vector();
b2.~vector();
You shall not destruct the vectors manually, otherwise the next time you try to access them (upon next call to the operator ()) you get undefined behavior.
Why do you call vector destructor manually?? You should let C++ call that for you. If you want to clear the vector use clear member function

c++ function can not call twice

I have this code. From main function i twice call sportPrisevinners function and if it is first call of this function it works correctly and I recive correct result, but when i call it second time I recive incorrect result even I pass this function with same arguments. Please help me to solve this problem while it doesn`t make me crasy.
const char* countries[] = {"ru", "gb", "us", "uk", "ch", "de"};
const int countriesCount = 6;
const char* sports[] = {"runing", "swiming", "baseball", "football", "jumping", "kerling"};
const int sportsCount = 6;
enum {
Empty = 0,
Bronse,
Silver,
Gold
};
struct member {
char sport[9];
char country[3];
int points;
int medal;
};
struct members {
member* list;
int count;
};
string medalToStr(int medal)
{
switch (medal) {
case Gold:
return "Gold";
case Silver:
return "Silver";
case Bronse:
return "Bronse";
default:
return "Empty";
}
}
void printMembers(members &list)
{
for (int i = 0; i < list.count ; i++)
cout << /*i << " " <<*/ medalToStr(list.list[i].medal) << " in "
<< list.list[i].sport << " with " << list.list[i].points
<< " from " << list.list[i].country << endl;
}
void generate()
{
ofstream file("members.dat", ios::binary|ios::trunc);
member temp;
for (int i = 0; i < sportsCount ; i++)
for (int j = 0; j < countriesCount ; j++)
{
int count = rand()%5+5;
for (int k = 0; k < count ; k++)
{
strcpy(&temp.sport[0], sports[i]);
strcpy(&temp.country[0], countries[j]);
temp.points = rand()%100;
temp.medal = Empty;
file.write((char*)&temp, sizeof(member));
}
}
file.close();
}
members sportPrisevinners(const char* sport)
{
//reading
ifstream file("members.dat", ios::binary);
member* loaded = new member[60];
int pos = 0;
while (!file.eof())
{
member temp;
file.read((char*)&temp, sizeof(member));
static bool reading = false;
if (strncmp(&temp.sport[0], sport, strlen(sport))==0) {
reading = true;
loaded[pos++] = temp;
} else if (reading) {
break;
}
}
file.close();
//sorting
int count = 3;
for (int i = 0; i < pos-1 ; i++)
{
for (int j = i+1; j < pos ; j++)
if (loaded[i].points<loaded[j].points)
{
member temp = loaded[i];
loaded[i] = loaded[j];
loaded[j] = temp;
}
if (i<count) {
static int last = -1;
if (loaded[i].points==last)
count++;
loaded[i].medal = count-i;
last = loaded[i].points;
} else break;
}
//returning
members result;
result.list = new member[count];
memcpy(result.list, loaded, count*sizeof(member));
/*for (int i = 0; i < count; i++)
result.list[i] = loaded[i];*/
result.count = count;
delete[] loaded;
return result;
}
int main(int /*argc*/, char */*argv*/[])
{
srand(time(0));
generate();
members r = sportPrisevinners(sports[4]);
printMembers(r);
delete[] r.list;
members l = sportPrisevinners(sports[5]);
printMembers(l);
delete[] l.list;
system("pause");
return 0;
}
I suspect it's the static local variables in your function. They won't have the same values on each call to the function, and this could affect the results. The initialization of these variables is performed just once - the first time they come into scope - so each subsequent time the function is called, you pick up the values these variables had last time the function ran.