How to access the object array in c++? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am new to programming. My goal is to realize the pancake sort in C++ without using STL. I have 3 classes, they are pancake, pancakepile and MpancakePiles. I have a question about the access to the object array. My code is as following:
My pancake pile is a 3D pile and Z is it's height.
So for a single pancake pile, it has Z pancakes.
I need to find the max size's index of these Z pancakes.
However, I don't know how to access the object array, like what should I fill in the ??? area if I want to process the object array size inside the pancake P. Max is a defined function.
There is no particular reason for not using STL. N is a static size, N=512. burnt=0 means burnt side face down.
int Max(int size[], int n)
{
int mi,i;
for(mi=0,i=0;i<n;i++)
if(size[i]> size[mi])
mi=i;
return mi;
}
class pancake
{
public:
int size;
bool burnt;
void flip_pancake()
{
burnt=~burnt;
}
};
class pancakepile
{
public:
pancake P[N];
int Z;
void pan_sort_ascending()
{
int mi=Max(???,Z);
......
}
}

You throw away your current implementation of pan_sort_ascending, and replace it with a call to std::sort, passing a function that describes which of two pancakes should go below the other.
#include <algorithm>
// A pancake is smaller than another if it's size is less
bool pancake_less(const pancake & lhs, const pancake & rhs)
{
return lhs.size < rhs.size;
}
// sorts smallest first
void pancakepile::pan_sort_ascending()
{
std::sort(P, P + Z, pancake_less);
}
Now if you want a pan_sort_descending, you can just flip the logic of the comparison
// A pancake is larger than another if it's size is greater
bool pancake_greater(const pancake & lhs, const pancake & rhs)
{
return lhs.size > rhs.size;
}
// sorts largest first
void pancakepile::pan_sort_descending()
{
std::sort(P, P + Z, pancake_greater);
}

I am not sure what you want but If you only want to return the bigest pancake of the pancake list I would implement a memberfunction in the pancakepile class:
class pancakepile
{
public:
pancake P[N];
int Z;
void pan_sort_ascending()
{
int mi=max();
......
}
pancake max()
{
pancake bigestPancake;
foreach(pancake pan, P)
{
if(bigestPancake.Z < pan.Z)
bigestPancake = pan;
}
return bigestPancake;
}
}
Edit:
If you want to get the Index of the bigestPancake you can do this instead:
class pancakepile
{
public:
pancake P[N];
int Z;
void pan_sort_ascending()
{
int mi=max();
......
}
int max()
{
int bigestPancakeIndex;
for(int i = 0; i < P.size(); i++)
{
if(P[bigestPancakeIndex].Z < P[i].Z)
bigestPancakeIndex= i
}
return bigestPancakeIndex;
}
}

Related

Sorting function in a class [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a class Client :
#include <stdio.h>
#include <iostream>
class Client
{
private:
vector<Liked*>like;
public:
Client();
~Client();
sort_id();
};
where like is a vector connection between class Client and Liked.
I created adding function:
void Client::addLiked(int id, string title)
{
Liked* newLiked= new Liked(id, title, year, minute, genre);
like.push_back(newLiked);
return ;
}
which is responsible for adding movie to the list. I would like to have sorting function, which will sort id in ascending order while printing the whole list :
void Client::print_Liked()
{
int n = like.size();
if(n == 0)
{
cout<<"Is empty"<<endl;
}
for(int i=0;i<n;i++)
{
sort_id();
like[i]->print_Liked();
}
}
I have tried with a bubble sort but I got errors :
void Client::sort_id()
{
int n = like.size();
bool swapped = true;
int j = 0;
int temp;
while (swapped) {
swapped = false;
j++;
for(int i = 0;i < n - j;++i)
{
if(like[i]->getID() > like[i+1]->getID())
{
temp = like[i]->getID();
like[i]->getID() = like[i+1]->getID();
array[i+1]->getID() = temp;
swapped = true;
}
}
}
}
The easiest way to sort your vector is by using the standard std::sort function together with a suitable lambda function for the comparisons.
Something like this:
void Client::sort_id()
{
std::sort(begin(like), end(like), [](Liked const* a, Liked const* b)
{
return a->getID() > b->getID();
});
}

Program works fine only for one test case - Debugging [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to know whether my graph is bipartite or not, I have several test cases. If I run more than one test case it doesn't work properly, it always shows Bipartite. I am having a hard time figuring it out. For just one case, it works fine for any graph.
Here goes my code.
#include <iostream>
#include <cstdio>
#include <stack>
#include <list>
using namespace std;
class Graph
{
public:
int V;
list<int> *adj;
Graph(int V);
void addEdge(int v, int w);
};
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w);
adj[w].push_back(v);
}
class Bipartite
{
private:
bool isBipartite;
bool *color;
bool *marked;
int *edgeTo;
stack<int> cycle;
public:
Bipartite(Graph G)
{
isBipartite = true;
color = new bool [G.V];
marked = new bool [G.V];
edgeTo = new int [G.V];
for (int v = 0; v < G.V; v++)
{
if (!marked[v])
{
color[v] = false;
dfs(G, v);
}
}
delete color;
delete marked;
delete edgeTo;
}
void dfs(Graph G, int v)
{
marked[v] = true;
list<int>::iterator w;
for (w = G.adj[v].begin(); w != G.adj[v].end(); w++)
{
if (!cycle.empty())
return;
if (!marked[*w])
{
edgeTo[*w] = v;
color[*w] = !color[v];
dfs(G, *w);
}
else if (color[*w] == color[v])
{
isBipartite = false;
cycle.push(*w);
for (int x = v; x != *w; x = edgeTo[x])
{
cycle.push(x);
}
cycle.push(*w);
}
}
}
bool isBi()
{
return isBipartite;
}
};
void solve(int n,int **p){
long long int x,y;
Graph g(n);
for(x=0;x<n;x++)
for(y=0;y<n;y++)
{
if(p[x][y]==1)
g.addEdge(x,y);
}
Bipartite b(g);
if (b.isBi())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
int main()
{
long long int i,j,t,x,m,y,a,b;
int **p,n;
cin>>t;
for(i=0;i<t;i++)
{
cin>>n>>m;
p=new int*[n]();
for(x=0;x<n;x++)
{
p[x]=new int[n]();
}
for(j=0;j<m;j++)
{
cin>>a>>b;
a=a-1;
b=b-1;
p[a][b]=1;
p[b][a]=1;
}
for(x=0;x<n;x++)
{
for(y=0;y<n;y++)
{
if(x!=y)
{
p[x][y]=1-p[x][y];
}
}
}
/* for(x=0;x<n;x++)
{
for(y=0;y<n;y++)
cout<<p[x][y]<<" ";
cout<<"\n";
}
*/
solve(n,p);
}
return 0;
}
You never explicitly initialize the contents of marked, or, more accurately, the contents of the array that it points to.
The loop in your constructor reads elements of marked to decide how to assign to color, but you never initialized the elements of marked being read.
Similiar argument for color and edgeTo.
This means that, while they may have had the expected initializations for the first case, may well be using whatever value happened to be there in later cases.
Also Bipartite(Graph G) is calling the default copy constructor of Graph. Probably not what you want.
Try Bipartite(const Graph & G) instead (also in dfs).
And don't do new without delete.
Rather use vector<vector<int>> adj;, why even list? And reinit it in constructor with adj.resize(V);.
After your edit of code in question, as you use new to allocate array, you should delete it as array too, so use delete[] color;.
Or stop using new/delete completely. Again you can use std::vector<bool> color(G.V);, avoiding both new/delete hassle, and also having all values initialized to false by default.
In modern C++ there're very few (more like "none") reasons to ever use new or delete (unless you write some low level library, or you are optimizing for performance, and you know what you are doing).

C++ structure issue [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
What is the functionality of this part below?
bool operator < ( const edge& p ) const
{
return w < p.w;
}
I'm giving the Full code here(I don't know if it's necessary or not to paste the whole code). I just don't understand the structure part.
I've searched several resources but don't get any simplicity.
struct edge
{
int u,v,w;
bool operator < ( const edge& p ) const
{
return w < p.w;
}
};
int pr[MAXN];
vector<edge>e;
int find(int r)
{
return (pr[r]==r) ? r: find(pr[r]);
}
int mst(int n)
{
sort(e.begin(),e.end());
for(int i=1;i<=n;i++)pr[i]=i;
int count=0,s=0;
for(int i=0;i<(int)e.size();i++)
{
int u=find(e[i].u);
int v=find(e[i].v);
if(u!=v)
{
pr[u]=v;
count++;
s+=e[i].w;
if(count==n-1) break;
}
}
return s;
}
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
int u,v,w;
cin>>u>>v>>w;
edge get;
get.u=u; get.v=v; get.w=w;
e.push_back(get);
}
cout<<mst(n)<<endl;
return 0;
}
Think about when you do 1 < 3. 1 is obviously smaller than 3. Alright, but suppose you have this struct/class/union (note the 3 are almost the same thing in C++) called Toy:
struct Toy
{
float _volume;
float _weight;
std::string _brand;
};
Now you instantiate 2 Toy objects:
Toy car, kite;
car._volume = 27000.0; //27000 cm^3
car._weight = 150.0; //150 grams
kite._volume = 10000; //10000 cm^3
kite._weight = 200.0; // 200 grams
if (kite < car){
std::cout << "car!"; // is toy car bigger!?
}else{
std::cout << "kite!"; // or is it the kite?
}
Now, there, the C++ language doesn't know what you mean when you check if the kite is smaller than the toy car. It could be either that you want to see which has less weight, or it could be that you're checking which takes less space; smaller in volume. To solve the ambiguity, C++ asks you the programmer to implement operators for your custom objects.
If we strip the syntactic sugar part of the design of many operators, let it be smaller than (<) for the sake of the example, a < b becomes a.operator<(b). So operator< can be said to be a class/struct/union method like any other!
To clear the ambiguity in the toy example, we re-implement/overload our struct's operator<() method to let it compare volumes as follows:
struct Toy
{
float _volume;
float _weight;
std::string _brand;
bool operator<(const Toy & otherToy)
{
return _volume < otherToy._volume; // or ._weight for each if we want to compare by weight
}
};
if (kite < car){
std::cout << "car!"; // the car has more volume!
}else{
std::cout << "kite!";
}
With your code snippet, you can see that an edge object comparison criterion was defined in operator< definition as the member w. So whichever has the smaller w is the smaller object when compared with that operator.

Sorting using vectors [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm writing a program of a stock market where I read from a file and sort with symbols and percent gain/loss. I have completed sorting with symbols but having trouble establishing the percent gain loss. Basically i am instructed to use vectors. We are required to produce the list ordered by percent gain/loss and i need to sort the stock list by this component. However i'm not to physically sort the list by component percent gain/loss; instead provide a logical ordering with respect to this component.
so basically i added a data member, a vector to hold the indices of the stock list ordered by the component percent gain/loss. i called it array indexByGain. so when i print the list ordered by the percent gain/loss, i use the array indexByGain to print the list. my problem is an i need help on how to start if someone could show me an example or explain on how to go about this i can continue or correct me on my rough draft that will be helpful. below is a rough draft of my code. stockType has to do with the where data is stored from the file.
#include <iostream>
#include "stockType.h"
class stockListType
{
public:
void sortBySymbols();//sort out symbols and it comiples correctly.
void sortByGain();
void printByGain();
void insert(const stockType& item);
private:
vector<int> indexByGain;//declared a vector array indexByGain..
vector<stockType> list;
};
void stockListType::insert(const stockType& item)
{
list.push_back(item)//inserts the data from file to vector array.
}
//function prints out the gain
void stockListType::printByGain()
{
//my code to print out the gain..
}
//function to sort the gain and this is where i am stuck.
void stockListType::sortGain()
{
int i, j, min, maxindex;
for(i=0;i<list.size();i++)
{
min = i;
for(j=i+1;j<list.size();j++)
list[maxindex].getPercentage()<list[j].getPercentage();
maxindex = j;
indexGain.push_back(maxindex);
}
I know I am wrong but am i starting on a good base or totally of. please you could assist me or correct me. Thanks. oh sorry before i forget getPercentage() calculates and returns the percentage gain/loss.
Initialize the index and use std::sort:
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
struct Data {
int value;
int percent;
};
typedef std::vector<Data> DataVector;
typedef DataVector::size_type size_type;
typedef std::vector<size_type> IndexVector;
DataVector data { { 1, 1 }, { 2, -2 }, { 3, 3 }, { 4, -4 }, { 5, 5} };
IndexVector index;
index.resize(data.size());
for(size_type i = 0; i < data.size(); ++i) {
index[i] = i;
}
struct Less
{
const DataVector& data;
Less(const DataVector& data)
: data(data)
{}
bool operator () (size_type a, size_type b) {
return data[a].percent < data[b].percent;
}
};
std::sort(index.begin(), index.end(), Less(data));
for(size_type i = 0; i < index.size(); ++i) {
std::cout << data[index[i]].value << ": " << data[index[i]].percent << std::endl;
}
}
You may use C++11:
std::sort(index.begin(), index.end(),
[&](size_type a, size_type b) { return data[a].percent < data[b].percent; }
);
for(auto i: index)
std::cout << data[i].value << ": " << data[i].percent << std::endl;

sort an array of objects based on property c++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
c++ sort with structs
#include <iostream>
using namespace std;
class fish{
private:
int size;
int price;
public:
fish()
{
size=0;
price=0;
}
void set_price(int x)
{
price=x;
}
void set_size(int g)
{
size=g;
}
int get_size()
{
return size;
}
int get_price()
{
return price;
}
void display()
{
cout<<" Fish price is "<<price<<" Fish size is "<<size<<endl;
}
void sort(fish h[5])
{
for (int o=0;o<=5;o++)
{
fish temp;
temp.set_price(0);
if (h[o].get_price()>h[o+1].get_price())
{
temp.get_price()=h[o].get_price();
h[o].get_price()=h[o+1].get_price();
h[o+1].get_price()=temp.get_price();
}
}
}
};
void main()
{
fish a;
fish b[5];
a.set_size(500);
a.set_price(2);
a.display();
for (int i=0;i<=5;i++)
{
b[i].set_size(i*2);
b[i].set_price(i*100);
}
for (i=0;i<=5;i++)
b[i].display();
}
I want to to find out how I send array b, and sorting it. Also I was going to ask about the destructors and where I can put them into my code.
To swap fish around when you are sorting you should write this
fish tmp = h[o];
h[o] = h[o+1];
h[o+1] = tmp;
You are sorting based on the fish price, but it's the whole fish that should be sorted.
On your other question, there is no need for destructor in this code. Your fish class doesn't need to do any 'clean up' so it doesn't need a destructor.
if you're looking to sort your array by a given element the STL container should be just fine, if not i would use this method
template<class T>
void quickSort(T * elements, unsigned int first, unsigned int last)
{
if(first < last) //make sure params are in bounds
{
T t = elements[first]; //t is PIVOT
unsigned lastLow = first; //create last low item
unsigned i; //used for loop/swapping
for(i = first + 1; i <= last; i++) //run through entire bounds
if(elements[i] < t) //if elements is less than Low
{
<< " adding one onto lastLow...\n";
lastLow++; //move lastLow up one
swap(elements,lastLow, i); //swap lastlow and i
}
swap(elements,first, lastLow); //swap first and lastlow
if(lastLow != first) //if lastlow is not first element
quickSort(elements, first, lastLow - 1);
if(lastLow != last) //if lastlow is not last element
quickSort(elements, lastLow + 1, last);
}
}
this is a common quicksort function used to sort an array. Just replace the right variables to represent your data E.g. T * elements becomes Fish * stuff, T t = Elements[first] becomes double price = stuff[first] and so on.