Priority Queue not printing the values pushed into it [closed] - c++

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 1 year ago.
Improve this question
I just started Data Structures and i have stumbled upon Priority queues, I wrote a simple program to print the values that i have pushed in the Queues but it wont print anything.
#include<iostream>
#include <queue>
using namespace std;
int main(){
priority_queue<int> maxi;
priority_queue<int, vector<int>, greater<int>> mini;
int m = maxi.size();
int n = mini.size();
maxi.push(1);
maxi.push(3);
maxi.push(2);
maxi.push(5);
maxi.push(0);
cout<<"Max size->"<<maxi.size()<<endl;
for(int i=0; i<m; i++){
cout<<maxi.top()<<" ";
maxi.pop();
}cout<<endl;
mini.push(1);
mini.push(3);
mini.push(2);
mini.push(5);
mini.push(0);
cout<<"Mini size->"<<mini.size()<<endl;
for(int j=0; j<n; j++){
cout<<mini.top()<<" ";
mini.pop();
}cout<<endl;
}
I have read so many articles i just dont seem to find any error. Even the Compiler doesnt give any error.

You are getting the size of maxi and mini before adding elements to them. Since you're copying the value of the size (explained here), it wouldn't be updated when adding elements to the containers. Therefore your loop is not executed at all.

Related

Not Displaying Spaces [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 9 months ago.
Improve this question
I'm very new to c++ and everything in general. I dont understand why my code doesn't work:
#include <cstdio>
int main(){
int a;
scanf("%d", &a);
for(int i = 1; i < a; i++){
printf(" ");
}
printf("*");
}
what I'm trying to do is make the number of spaces as the value inputted by the user minus and add a * at the end so if a = 10 :
*
but when I try it the spaces don't show.
Ideally what I'm trying to do is get an output something like:
*
**
***
****
The length depends on user input if it was 10 then the spaces would be 9 and the * at the end would be the tenth.
You will have to use 3 for-loops (two of them nested inside one). The parent for loop for each row. And the nested two are for spaces and stars.
#include <cstdio>
int main()
{
int a;
scanf("%d", &a);
for(int i=1; i<a; i++)
{
for(int j=i; j<a; j++)
printf(" ");
for(int k=0; k<i; k++)
printf("*");
printf("\n");
}
return 0;
}
You will get your desired output if you put the value 5 for a, i.e., a=5.
To learn more in detail, you can follow this link star patterns in c
Your code may contain many errors. That's understandable, cause you are new to C/C++. Try problem-solving in beecrowd to improve your problem-solving capabilities and learn C/C++ in a better way. These errors will automatically go away day by day as you progress. But the secret is to practice and never give up. Good luck!

Why threads doesn't accept this input? [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 3 years ago.
Improve this question
I was doing a project that uses threads to sum some matrix but at the time of creating the threads and adding the params it always shows up the same error. Any ideas?
void sum(std::vector <double>& matrix, std::vector <double>& other) {
for (auto i = 0; i < 15; i++) {
matrix[i] += other[i];
}
}
here it is the operation that threads should do.
std::vector <double>* mat1 = new std::vector <double>[15];
std::vector <double>* mat2 = new std::vector <double>[15];
std::vector <std::thread*> threads;
for (int j = 0; j < 15; j++) {
sum(mat1[j], mat2[j]); //this works;
threads.push_back(new std::thread(sum,mat1[j],mat2[j])); //this dont why?;
}
Thanks in advance
To get it to compile, change:
std::thread(sum,mat1[j],mat2[j])
to:
std::thread(sum, std::ref(mat1[j]), std::ref(mat2[j]))
Example: https://godbolt.org/z/Ek-cnm
But there are multiple problems with your question and code besides just getting it to compile, please listen to what other have said in the comments.

Whats Wrong in it? [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 3 years ago.
Improve this question
Problem: https://www.hackerrank.com/challenges/migratory-birds/problem
Test Case Link: https://hr-testcases-us-east-1.s3.amazonaws.com/33294/input04.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1573162301&Signature=MgpSHa3lxX%2FwYwumjzAmF8uviZE%3D&response-content-type=text%2Fplain
for this test case i cant get any output why ?
Thanks in advance
#include<iostream>
using namespace std;
int main()
{
long long n,i=0,num=0,mx=0,r=0;
cin>>n;
long long arr[6]{0};
for(int i=0;i<n;i++)
{
cin>>num;
arr[num]++;
}
for(int i=1;i<6;i++)
{
if(arr[i]>mx)
{
mx=arr[i];
r=i;
}
}
cout<<r;
}
This looks problematic:
long long arr[6]{0};
for(int i=0;i<n;i++)
{
cin>>num;
arr[num]++;
}
If the input value read into num is greater than 5 (or less than zero), undefined behavior will exist when a value is written into an invalid memory location offset from arr.
There is nothing wrong with you code i got accepted all test cases. But the solution can be return in more optimized way

As a student, how can I improve poor code? [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 4 years ago.
Improve this question
How do I improve this spaghetti code? I am a student and this is me being slow in my first programming course.
What I think I want to achieve
Find an alternating sum up to a number.
Problem overview
https://www.hackerrank.com/contests/cs102-s18-march31/challenges/treasure-road
This is a problem from the archive at provided by the teacher. I have deduced that an alternating sum has to be calculated, where the sum of the first odd integers has to be subtracted from the sum of the first even integers (of course up to #ofsteps/2).
What I did
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arrodds[100]={0},arrevens[100]={0},sumevens=0,sumodds=0;
int steps;
cin >> steps;
int i=0;
while(i<steps/2)
{
if(i%2==0)
arrevens[i]=i;
else arrodds[i]=i;
i++;
}
for(int d=0;d<100;d++)
sumevens+=arrevens[d];
for(int h=0;h<100;h++)
sumodds+=arrodds[h];
cout << sumodds-sumevens;
return 0;
}
My logic/reasoning
Zingo can only move Steps/2, if he moves an odd step he moves forward, and Ringo moves a step closer to to Zingo, else if Zingo moves an even step Ringo moves away from Zingo.
Let's say there are 10 steps... 10/5=5... 5 steps= 1+2+3+4+5, odd steps=1,3,5; even steps=2,4; so displacement is 1+3-(2+4)=-2.
The code
I try to store the even and odd numbers in different arrays, then take the sum of each one and subtract the sums.
What's wrong?
My code works for small numbers it seems, as some test cases go beyond what I think My code could handle.
What I tried
I tried playing around with the sizes of my arrays but didn't get what I want.
Should I abandon this code and try something else? If not, how can I fix this abomination and guide me through it?
You can use a running sum instead of using arrays:
sum_evens = 0;
sum_odds = 0;
for (int i = 0; i < steps; ++i)
{
if (i & 1)
{
sum_odds += i;
}
else
{
sum_evens += i;
}
}
Edit 1:
The sum of the first N odd numbers is N*N.
You could change the calculation to:
sum_odds = steps * steps;
I'll leave the calculation of the first N even numbers up to the reader / OP.

What's the difference between the given two codes. One gives time limit exceeded when run on ideone and the other works fine [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 5 years ago.
Improve this question
1st code: works fine gives success with time of 0sec
int main()
{
int n=100000;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{}
cout<<"ffdfdf";
}
2nd code: gives a time limit exceeded
int main()
{
int n=100000;
bool **a=new bool*[n];
for(int i=0;i<n;i++)
{
bool[i]=new bool[n];
for(int j=0;j<n;j++)
{
bool[i][j]=1;
}
}
cout<<"ffdfdf";
}
can anyone explain why the two code fragments have a vast time difference.I am not understanding it.
bool[i]=new bool[n]; is extremely expensive cf. the other statements.
A good compiler will optimise out your first program to cout << "ffdfdf";, since it will know that the loop doesn't do anything.
Once you've replaced your errant bools with as so the second snippet actually compiles, you'd be advised to pair your new[] calls with delete[].