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();
});
}
Related
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 3 years ago.
Improve this question
I am writing this code on Visual studio 2019 which converts a prefix number to infix.
When i press F5 the window says: (process 10428) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
I don't know much about C++. I know Java and python fairly well but our instructor wants us to use C++.
string s[7];
int f = -1;
void push(string a)
{
s[f++] = a;
}
string pop()
{
return s[f--];
}
bool isop(char x) {
switch (x)
{
case '+':
case '-':
case '*':
case '/':
return true;
}
return false;
}
int main() {
string a = "*+ab+cd";
reverse(a.begin(), a.end());
for (int i = 0;i<int(a.length());i++) {
if (isop(a[i])) {
string v1 = pop();
string v2 = pop();
string h = "(" + v1 + a[i] + v2 + ")";
push(h);
}
else {
push(string(1, a[i]));
}
}
for (int i = 0;i < 7;i++)
{
cout << s[i];
}
return 0;
}
Problem here (it would be exactly the same in Java)
string s[7];
int f = -1;
void push(string a)
{
s[f++] = a;
}
The first time you push f is -1 so you have an out of bounds array access. I guess you meant this
void push(string a)
{
s[++f] = a;
}
You could have avoided this error by using a std::vector (similar to an ArrayList in Java).
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 3 years ago.
Improve this question
Why do we have to copy first then use the getfunction? Why can we just directly use the get function on like borrowers.getID() == ID instead of borrow.getID() == ID?
I just need an explanation. I know some things might be missing but I am just very confused here as it looks not very efficient!
class ReadershipList {
private:
ifstream myin;
ofstream myout;
string readerfile;
vector<Borrower> borrowers;
public:
int findBorrowerbyID(string ID); //Find the borrowers index by ID
int findBorrowerbyName(string name);
void addBorrower(Borrower newBorrow); //Add a borrower to the vector of borrowers
void listBorrowers(); //Prints the list of borrowers
bool deleteBorrower(string ID); //Delete borrower using ID
Borrower getBorrowerbyID(string ID); /
Borrower getBorrowerbyName(string name); //Get borrower details using name
};
Why cant I directly access the members?
Instead of using this:
bool ReadershipList::deleteBorrower(string ID) {
Borrower borrow;
for (int b = 0; b < borrowers.size(); b++) {
borrow = borrowers[b];
if (borrow.getID() == ID) {
borrowers.erase(borrowers.begin() + b);
return true;
}
}
return false;
}
you do not need to copy, you can do
bool ReadershipList::deleteBorrower(string ID) {
for (int b = 0; b < borrowers.size(); b++) {
if (borrowers[b].getID() == ID) {
borrowers.erase(borrowers.begin() + b);
return true;
}
}
return false;
}
or
bool ReadershipList::deleteBorrower(string ID) {
for (int b = 0; b < borrowers.size(); b++) {
Borrower & borrow = borrowers[b]; // or better "const Borrower & borrow = borrowers[b];" if "string Borrower::getID() const"
if (borrow.getID() == ID) {
borrowers.erase(borrowers.begin() + b);
return true;
}
}
return false;
}
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;
}
}
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 8 years ago.
Improve this question
I'm having a very nooby problem.
void update(node city, int costpath) {
int i;
for (i = 1; i < city.concity.size(); i++) {
city.costcity[i] = city.costcity[i] + costpath;
}
// Updates the sorrounding paths with the cost path of the previous one
}
node is a struct. It has vectors concity and costcity. When I call this function to update the values in the main, it doesn't work! When I print it out, it still shows the same old values…
Two problems:
void update(node city, int costpath) {
// ^^^^ 1) You're taking your node by-value. So it's a copy
// Internal to this function, you're just modifying the local city
int i;
for (i=1;i<city.concity.size();i++) {
// ^^^ 2) C++ is zero-indexed, so this loop skips the first element
The correct implementation would be:
void update(node& city, int costpath) {
for (int i = 0; i < city.concity.size(); ++i) {
city.costcity[i] += costpath;
}
}
C and C++ pass parameter by value when call function.
void update(node* city, int costpath) {
int i;
for (i=1;i<city->concity.size();i++) {
city->costcity[i] = city->costcity[i] + costpath;
}
}
See http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_value
and http://clc-wiki.net/wiki/C_language:Terms:Pass_by_value
pass node city as a pointer or reference to the function.
c++ is 0 indexed thus start with 0 and increment till the array size.
void update(node& city, int costpath) {
or
void update(node* city, int costpath) {
and
for (int i = 0; i < city.concity.size(); ++i) {
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
So I have a homework assignment due tonight and I'm trying to compile it to test but I'm running into a bunch of errors and some of them seem to make zero sense? The errors mention things like: "syntax error before '::'" and the like, but I have never encountered errors like these and have 0 idea on how to fix them.
UnsortedClass.cpp
#include "UnsortedClass.h"
void UnsortedType::UnsortedType()
{
length = 0;
}
bool UnsortedType::IsFull() const
{
return (length == MAX_ITEMS);
}
int UnsortedType::GetLength() const
{
return length;
}
NBA UnsortedType::GetItem(NBA customPlayer, bool& found)
{
bool moreToSearch;
int location = 0;
found = false;
moreToSearch = (location < length);
while (moreToSearch && !found)
{
switch (customPlayer.ComparedTo(info[location]))
{
case LESS :
case GREATER : location++;
moreToSearch = (location < length);
break;
case EQUAL : found = true;
item = info[location];
break;
}
}
return customPlayer;
}
void UnsortedType::MakeEmpty()
{
length = 0;
}
void UnsortedType::PutItem(NBA customPlayer)
{
info[length] = customPlayer;
length++;
}
void UnsortedType::DeleteItem(NBA customPlayer)
{
int location = 0;
while (customPlayer.ComparedTo(info[location]) != EQUAL)
location++;
info[location] = info[length - 1];
length--;
}
void UnsortedType::ResetList()
{
currentPos = -1;
}
NBA UnsortedType::GetNextItem()
{
currentPos++;
return info[currentPos];
}
UnsortedClass.h
#include "NBA.h"
class UnsortedClass //declares a class data type
{
public:
// 8 public member functions
void UnsortedType ( );
bool IsFull () const; //checks if list is full
int GetLength () const ; // returns length of list
NBA GetItem (NBA customPlayer, bool& found); //gets item specified in parameters
void PutItem (NBA customPlayer); //puts NBA player in list
void DeleteItem (NBA customPlayer); //deletes NBA player from list
void ResetList (); //resets list to 0
NBA GetNextItem (); //gets next item after current list position
private:
// 3 private data members
int length;
NBA info[MAX_ITEMS];
int currentPos;
};
NBA.h
#include <string>
using namespace std;
const int MAX_ITEMS = 10;
enum RelationType {LESS, GREATER, EQUAL};
class NBA {
private:
char firstInitial;
string lastName;
string team;
char position;
public:
void set_first_initial(char playerFirstInitial);
void set_last_name(string playerLastName);
void set_team(string teamName);
void set_position(char position);
char get_first_initial();
string get_last_name();
string get_team();
char get_position();
};
The errors I've been receiving are as follows (in picture format as I can't paste the lines without Stackoverflow interpreting it as code)
Constructors don't have a return type specified. Change
void UnsortedType::UnsortedType()
to
UnsortedType::UnsortedType()
Also the class name in its header declaration is wrong; everywhere else says UnsortedType but this says:
class UnsortedClass //declares a class data type