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 have the following function:
template <class T>
T c_base (T num,T second, T first = 10)
{
T res = 0;
T secnum;
T bitseed[90];
int i = 1,k,jump,anex,len;
if(second==first)
{
res = num;
return (res);
}
if(first==10&&second!=10)
{
anex = num;
while(num>0)
{
jump = num/second;
bitseed[i] = num%second;
num/=second;
i++;
}
if(anex>0)
{
for(k=i;k>=1;k--)
{
if(k==i&&jump==0) {res = bitseed[k-1]; k--; continue;}
if(k==i&&jump!=0) {res = jump; continue;}
res = res*10+bitseed[k];
}
}
return (res);
}
if(second==10)
{
anex = num;
len = 1;
while(anex>=10)
{
len *= 10;
anex/=10;
i++;
}
anex = num;
if(anex>0)
{
for(k=i;k>=1;k--)
{
res = res*first+anex/len;
anex%=len;
len/=10;
}
}
return (res);
}
if(second!=10&&first!=10)
{
secnum = c_base <T> (num,10,first);
res = c_base <T> (secnum,second,10);
return (res);
}
}
I was wondering how efficient it is (from both speed and memory consumed point of view) and how/if can it be improved. (from the algorithm perspective)
Ps. Explication of function : c_base("number","to-base","from-base"->optional);
I see a lot of confusion here:
A number doesn't have a base. What do have bases are number representations. Both input and output should be number representations (e.g. std::strings in C++).
Why treating base 10 specially? there's nothing really special about it except that by historical accidents most humans today use it. This is totally irrelevant for an algorithm. A special case for power-of-two bases could make sense for technical reasons (because computers use base 2 internally).
Why doing a double conversion instead of just reading from base x and writing to base y?
Related
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 2 years ago.
Improve this question
29==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000478 at pc 0x0000003a5517 bp 0x7ffe2f5c2670 sp 0x7ffe2f5c2668
Here is my code:
class Solution {
public:
vector summaryRanges(vector& nums) {
if(nums.size() == 0)
return {};
if(nums.size() == 1)
return {to_string(nums[0])};
vector<string> ans{};
int i=0,j=1,initial=nums[0];
if(nums.empty()||nums.size()==0)
return ans;
while(j<=nums.size())
{
if(i<nums.size())
{
if(nums[i]+1==nums[j])
{
i++;
j++;
}
else
{
string str3;
if(initial!=nums[i])
{
string str = to_string(initial);
string str1= to_string(nums[i]);
string str2 = "->";
str3=str+str2+str1;
}
else
{
str3 = to_string(initial);
}
ans.push_back(str3);
initial=nums[j];
i=j;
j++;
}
}
else
{
string str = to_string(initial);
ans.push_back(str);
}
}
return ans;
}
};
I think you are accessing an index out of vector size . Index j is probably going out of bounds .
while(j< = nums.size())
and
nums[j]
This
while(j<=nums.size())
and these
if(nums[i]+1==nums[j])
...
initial=nums[j];
look very suspicious. If j == nums.size() then nums[j] is an out of bounds error on num. Maybe you meant this?
while(j<nums.size())
(I.e. the same as you have with the i variable).
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();
});
}
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 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 5 years ago.
Improve this question
Check the following code:
#include<iostream>
using namespace std;
class example
{
public:
int number;
example()
{
cout<<"1";
number = 1;
}
example(int value)
{
cout<<"2";
number = value;
}
int getNumber()
{
cout<<"3";
return number;
}
};
int main()
{
example e;
e = 10;
cout<<e.getNumber();
return 0;
}
What is the output of the above code. Also, I want to know what happens when an object is directly assigned to a value. How will the compiler interpret it?
first you typed
example e;
So first constructor called and 1 printed
example()
{
cout<<"1";
number = 1;
}
output :
1
then you typed :
e=10 its equal to e = example(10); so another constructor called :
example(int value) /// beacause you used example(10)
{
cout<<"2";
number = value;
}
so your output is :
12
and number is 2
Finally in :
cout<<e.getNumber();
3 is couted but in the other hand value is `10`
because number = value your number is 10
So in Finally your output is :
12310
thanx for #StoryTeller for editting explaintions
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.