\Device\NUL
// { Driver Code Starts // Program to find the maximum profit job sequence from a given array // of jobs with deadlines and profits #include<bits/stdc++.h> using namespace std; // A structure to represent a job struct Job { int id; // Job Id int dead; // Deadline of job int profit; // Profit if job is over before or on deadline }; // } Driver Code Ends /* struct Job { int id; // Job Id int dead; // Deadline of job int profit; // Profit if job is over before or on deadline }; */ class Solution { public: //Function to find the maximum profit and the number of jobs done. vector<int> JobScheduling(Job arr[], int n) { // your code here vector<vector<int>> m(n); for(int i=0;i<n;i++) { if(m[arr[i].dead].size()) { m[arr[i].dead][0]=max(m[arr[i].dead][0],arr[i].profit); } m[arr[i].dead].push_back(arr[i].profit); } int task=0; int profit=0; for(auto x: m) { if(x[0]) { task+=1; profit+=x[0]; } } return {task,profit}; } }; // { Driver Code Starts. // Driver program to test methods int main() { int t; //testcases cin >> t; while(t--){ int n; //size of array cin >> n; Job arr[n]; //adding id, deadline, profit for(int i = 0;i<n;i++){ int x, y, z; cin >> x >> y >> z; arr[i].id = x; arr[i].dead = y; arr[i].profit = z; } Solution ob; //function call vector<int> ans = ob.JobScheduling(arr, n); cout<<ans[0]<<" "<<ans[1]<<endl; } return 0; } // } Driver Code Ends
Wait > Job arr[n] Why not allocate as much as you need ?
\Device\NUL
ISO C++ doesn't support Variable length array
klimi
How is this related to c or c++?
Ольга
Hello, I understand that asking such a question is a bit strange, and this group for C and Cpp but I don't have variant. And still try my luck. Maybe someone can help in writing laboratory work on the assembler NASM x86? At least just suggest what's wrong...
Anonymous
My apologies
Anonymous
How would I make a loop to create objects with different names
klimi
How would I make a loop to create objects with different names
i don't know if you can do this, but you can save them into an array or some other datastructure
Ольга
Not sure if people will help you in writing, but if you write it by yourself and just ask questions related to nasm i don't see a problem
How to properly divide the assembler into 10 types to get the rest. Because at me normally divides by ten but in the edx register the rest is not transferred I want to compare it but at me nothing turns out?
Michel
If you writing generic code that other programmers will use you need to account to the fact that they can use your code in different ways
Ok, I'm not, but still, I don't understand why making it size 1 is a problem. Can I say new T[0] ?
Anonymous
Ok, I'm not, but still, I don't understand why making it size 1 is a problem. Can I say new T[0] ?
You can say new T[0]. But you shouldn't do it. Read my message above.
Michel
Now I'm getting the uninitialized error from valgrind
Michel
Did this: class Array{ T *arr; size_t _size; public: /** * Default constructor * @param n size of array */ Array(size_t n = 0){ _size = n; arr = new T[_size + 1]; } /** * Move constructor; this allows the array to be passed * as a return value from a function sapping the pointers * and keeping the allocated data on the heap. */ Array(Array&& other): Array() { std::swap(_size, other._size); std::swap(arr, other.arr); } };
Pavel
Or actually no, wait, it's a constructor, not a move assignment operator, so it seems to be alright, except for not setting the size to the moved thing (which is not necessarily a mistake)
Michel
Or actually no, wait, it's a constructor, not a move assignment operator, so it seems to be alright, except for not setting the size to the moved thing (which is not necessarily a mistake)
Still, the memory allocated to arr would have got lost. It didn't until now because I' wasn't allocating anything before, but then it lead to another error when trying to free something that wasn't allocated.
Nobody🪲
Im trying to building a calcutor which will ask how many elements i want to calculate.. I tried it with array but not working
Nobody🪲
#include<stdio.h> int main() { int a[100],total_elements,i; char oper; printf("choose your expected operation(+,-,*,/):"); scanf("%c",&oper); printf("enter total number of element you want to work with "); scanf("%d",&total_elements); for(i=0;i<total_elements;i++) { printf("enter %d no element:\n",i+1); scanf("%d",&a[i]); } switch(oper) { case '+': { printf("%d +%d=%d",a[i],a[i],a[i]+a[i]); break; } case '-': { printf("%d-%d=%d",a[i],a[i],a[i]-a[i]); break; } case '*': { printf("%d * %d=%d",a[i],a[i],a[i]*a[i]); break; } return 0; } }
Nobody🪲
Whats wrong here?
Anonymous
Hello guy am not understanding this shit c language...pls help men
Anonymous
🙆‍♂️
Captain
What should i do then?
Take 2 variables and do the operations
Nobody🪲
Take 2 variables and do the operations
But if the user want to calculate 4/5 numbers together?
Nobody🪲
Like this 60+70+80+90+100=?
Captain
Like this 60+70+80+90+100=?
The you'll need loop for each case
Pavel
This didn't work
In what way didn't work?
Pavel
Did it compile?
Michel
Yes yes
Michel
but I keep getting the same error from valgrind
Michel
Uninitialized stuff
Pavel
but I keep getting the same error from valgrind
OK, sad, i can't see other issues, do you have code how you use it?
Michel
OK, sad, i can't see other issues, do you have code how you use it?
I'll share the whole repo so that you can see if you have the time. It's not massive but it's bigger than a pastebin
Nobody🪲
Michel
OK, sad, i can't see other issues, do you have code how you use it?
https://github.com/studentenherz/FOCUS This is the repo. The errors come from test/magnetic_field; it runs ok, but the Valgrind inspection gives a lot of uninitialized errors.
Michel
I'll appreciate it, thanks!
Ольга
Hello, I'm so sorry I don't understand why my function don't work. Why this program don't see vector? I hope someone knows. Thanks for listening https://onlinegdb.com/pTbx74-JX
Nobody🪲
The you'll need loop for each case
#include<stdio.h> int main() { int a[100],total_elements,i; char oper; printf("enter your expected operation:"); scanf("%c",&oper); printf("enter total number of numbers you want to work with:"); scanf("%d",&total_elements); for(i=0;i<total_elements;i++) { printf("enter %d no element:\n",i+1); scanf("%d",&a[i]); } switch(oper) { case '+': { for(i=0;i<total_elements;i++) { printf("%d+%d=%d\n",a[i],a[i],a[i]+a[i]); break; } } case '-': { for(i=0;i<total_elements;i++) { printf("%d-%d=%d\n",a[i],a[i],a[i]-a[i]); break; } } } return 0; }
Nobody🪲
Not working 🥺
.
Is there anyone here who is good at math?
AKI David
It's case ... : Statments; case ... : not case: { }
\Device\NUL
I have a question about (u)int_fastX_t on x86-64 platform Does u(int)_fast16 - 64 is 8 byte because its alligned ? I heard alligned memory is faster. Why does (u)int_fast8_t is 1 byte ? Unlike other typedefs ?
Anonymous
Anyone to assist with a c program that reverse a number
Αλι
Anyone to assist with a c program that reverse a number
int rev = 1 while not num == 0 do rev = (rev *10) + (num % 10) num /= 10 end return rev something like this🤔
Anshul
class Solution { public: int helper(vector<int> coins,int n,int dp[]) { if(n==0)return 0; if(coins[0]>n)return -1; if(dp[n]!=-1)return dp[n]; int ans=INT_MAX; for(int i=0;i<coins.size();i++) { if(coins[i]<=n) { int sub=helper(coins,n-coins[i],dp); if(sub!=-1) { ans=min(ans,sub+1); } } } if(ans==INT_MAX) { return -1; } return ans; } int coinChange(vector<int>& coins, int amount) { int dp[10001]; for(int i=0;i<amount;i++) { dp[i]=-1; } return helper(coins,amount,dp); } };
Anshul
is there anything wrong with this code for coin change problem? input: coins[]={1,2,5} amount=11
Anonymous
Is there anyone here who is good at math?
Not a Math community. Moreover Maths is a vast field. If you ask a generic question like that, you are unlikely to get an answer. I could have a cubic equation that I am unable to solve but instead of asking that directly, if i go into a community full of category theorists and ask if anyone is good in math, I will look stupid.
Ольга
what ?
Well, I just have a task. Given a real number to determine whether there are digit in this number, which are calfing or 3 or 6 or 9. If they are then deduce their number. So I want to divide a number of type m = n% 10 and if m = 3 increase the counter. Well and accordingly so for all points. Maybe I chose the wrong algorithm at all. And in the assembler it is possible to organize it somehow still?
Anshul
What is ci
Anonymous
class Solution { public: int helper(vector<int> coins,int n,int dp[]) { if(n==0)return 0; if(coins[0]>n)return -1; if(dp[n]!=-1)return dp[n]; int ans=INT_MAX; for(int i=0;i<coins.size();i++) { if(coins[i]<=n) { int sub=helper(coins,n-coins[i],dp); if(sub!=-1) { ans=min(ans,sub+1); } } } if(ans==INT_MAX) { return -1; } return ans; } int coinChange(vector<int>& coins, int amount) { int dp[10001]; for(int i=0;i<amount;i++) { dp[i]=-1; } return helper(coins,amount,dp); } };
You are doing DP programming wrong again. This is not the way to solve this problem. You are looping through the coins vector and calling helper recursively. What is the DP equation for your algorithm? You are just blindly applying the same recursive equation for a DP problem. Some DP solutions require a 2D Array. Some DP solutions require a 3D array solution. To solve problems using DP, you will have to first determine the DP equation. For your coin change problem, the equation would be something like this: dp[ci][curramt] = dp[ci-1][curramt]+dp[ci][curramt-coins[ci-1]];   where dp is a m*n 2D array where m is the total number of coins availabile and curramt varies from 0 to the amt desired. So you populate this 2D array and return dp[m-1][n-1] as the answer The explanation for DP equation is this either coin i is chosen or not. You build DP matrix by assuming that only a subsection of the coins are available to you. Suppose say you aregiven1,2 and 5 then initially you figure out the number of coins required to make amounts1 to n using only the coin 1. Then you do the same using coins 1 and 2. Finally you do the exercise using all 3 coins. The DP Equation is now self explanatory. If coin is not chosen then dp[i][curramt] = dp[i-1][curramt]. If it is chosen then dp[i][curramt] = dp[i][curramt - Val(coin i)] So the actual will be the sum of both and hence you need a 2D array for this solution
Anonymous
What is ci
I have explained it below
Anonymous
i still can't understand but thanks
Well dynamic programming is just constructing solutions from previously known solutions. So think how your code generates solutions based on your previous solutions. It doesn't. Now think what is the best way to generate an amount from 1 to n using only coins 1 to i-1 (You ignore coins i to m). Now to figure out the best way to generate amounts from 1 to n using coins 1 to i will only be dependent on the solutions you found earlier using the equation I showed you earlier. This is how you construct a DP solution.
'''''''
i still can't understand but thanks
U should try watching Aditya Verma's DP playlist on YouTube(it's in hindi)
Pavel
https://github.com/studentenherz/FOCUS This is the repo. The errors come from test/magnetic_field; it runs ok, but the Valgrind inspection gives a lot of uninitialized errors.
Hi, I had a look at it, I see you don't have the previous issue reproducible, but running valgrind showed me another one in derivative_Chebyshev_T, you have new and delete for an array, but there's a possible return in-between that may resulting in not calling that delete and memory leak. Looking at this part of code and other parts it seems that you not really using RAII, which is one of the most important features of C++. You can use gsl::finally, or write one yourself to call arbitrary cleanup code on scope end. Or if it's available to you, use std::vector, (with optimizations and exceptions disabled it doesn't have a lot of overhead), or if you're not sure you can just write a class that allocates memory in constructor, deallocates in desctuctor and has all copy/move deleted (or implemented, but then be careful with the implementations, e.g. your matrix implementation allocated in move constructor, which probably a lot of runtime overhead if you don't care about the moved-from object).
Anonymous
Write a program that takes marks as input and displays the relevant GPA. For example, if the marks are between 85 and 100, the GPA should be 4. If the marks are between 74 and 84, the GPA should be 3,2 for marks between 60 and 73, and 1 if the marks are 50 ar more but less than 60. The GPA is 0 for marks between 0 and 49. Any other value for marks is invalid. Write the problem using swicth case statments
Anonymous
yup
Can you explain a little
Anonymous
#include<iostream> Using namespace std; Swap & addition (int a,int b); Swap & addition (int x,int y); float z; z=x+y; return z; } int main(){ addition (3,3.5) cout<<z; } // Tha is a return by reference code That is true?and I can't get output Any solution?
Null
Any idea?
What's swap? You are returning float by value. So function is float addition (){}