Anonymous
Why we don't use gets function
Here's some history and explanation of why you shouldn't and alternatives. https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used
Anonymous
Write a program in c++ to find the sum of n terms of the following notation = 3/4 + 6/8 + 9/12 + 12/16😢😢
Anonymous
Write a program in c++ to find the sum of n terms of the following notation = 3/4 + 6/8 + 9/12 + 12/16😢😢
Are you really sure that is the problem? The sum is very simple for that. You dont even need a computer. Before writing the program try some maths to see what the sum would be.
Anonymous
I'm trying to write the code but I'm having a hard time💔
I said try solving it mathematically by hand. You will then figure out that writing a program is as good as writing 2 lines of code.
Anonymous
Sum=3
Sum of 4 terms is 3.
Anonymous
Yes
Now while adding the fractions did you notice any similarity between the terms?
Anonymous
Now while adding the fractions did you notice any similarity between the terms?
#include<iostream> #include<cmath> using namespace std; int main() { double sum=0,a; float i; int n; cout<<"nEnter value of n"; cin>>n; for(i=0.75;i<=n;i++) { a=0.75; sum+=a; } cout<<"Sum="<<sum; }
Anonymous
#include<iostream> #include<cmath> using namespace std; int main() { double sum=0,a; float i; int n; cout<<"nEnter value of n"; cin>>n; for(i=0.75;i<=n;i++) { a=0.75; sum+=a; } cout<<"Sum="<<sum; }
= 3/4 + 6/8 + 9/12 + 12/16 The pattern looks like (numerator*i)/(denominator*i) where i=1,2,3,4,... which will result in 0.75+0.75+0.75+.... for n times... in this case( 3/4 + 6/8 + 9/12 + 12/16).. So what you could do is, something like result = (numerator/denominator)* no.of times
Anonymous
#include<iostream> #include<cmath> using namespace std; int main() { double sum=0,a; float i; int n; cout<<"nEnter value of n"; cin>>n; for(i=0.75;i<=n;i++) { a=0.75; sum+=a; } cout<<"Sum="<<sum; }
Just do a n×0.75 instead. Why do you need a loop? And this is why I asked you to try mathematically first. This is just an exercise in reducing fractions to its simplest form. It is something that we learn in primary schools
Sam
Sam
Is that true?
Sandeep
// C++ Program to find kth element from two sorted arrays #include <iostream> using namespace std; int kthElement(int arr1[], int arr2[], int n, int m, int k) { // C++ Program to find kth element from two sorted arrays // In case we have reached end of array 1 int st1=0, st2=0; while (1) { if (st1 == n) return arr2[st2 + k - 1]; // In case we have reached end of array 2 if (st2 == m) return arr1[st1 + k - 1]; // k should never reach 0 or exceed sizes // of arrays if (k == 0 || k > (n - st1) + (m - st2)) return -1; // Compare first elements of arrays and return if (k == 1) return (arr1[st1] < arr2[st2]) ? arr1[st1] : arr2[st2]; int curr = k / 2; // Size of array 1 is less than k / 2 if(curr-1>=n-st1) { // Last element of array 1 is not kth // We can directly return the (k - m)th // element in array 2 if (arr1[n - 1] < arr2[st2 + curr - 1]) return arr2[st2 + (k - (n - st1) - 1)]; else { st2 += curr; k -= curr; } } // Size of array 2 is less than k / 2 if(curr - 1 >= m - st2) { if (arr2[m - 1] < arr1[st1 + curr - 1]) return arr1[st1 + (k - (m - st2) - 1)]; else { st1 += curr; k -= curr; } } else { // Normal comparison, move starting index // of one array k / 2 to the right if (arr1[curr + st1 - 1] < arr2[curr + st2 - 1]) 🥲🥲🥲🥲🥲🥲🥲//This condition fails if the size of the step ..i.e curr exceeds the size of array...so this code is giving me error if there is a skew in the size of the array { st1 += curr; k -= curr; } else { st2 += curr; k -= curr; } } } } // Driver code int main() { int arr1[7] = { 5 ,33, 55, 65, 76, 80, 90 }; int arr2[39] = { 10 ,13 ,14, 15, 15, 22, 27, 32, 34, 36, 36, 37, 39, 40, 42, 45, 49, 50, 50, 53, 56, 56, 57, 61, 65, 66, 70, 70 ,71, 74, 78, 84, 87, 90, 91, 94, 94 ,96 ,99 }; int k = 39; int p= kthElement(arr1, arr2, 7, 39, k); return 0; }
Pavel
Is that true?
Every community has it's own memes. If you want to know more about relation of two languages it's better to read some articles about it, rather than looking at random gifs.
Anonymous
// C++ Program to find kth element from two sorted arrays #include <iostream> using namespace std; int kthElement(int arr1[], int arr2[], int n, int m, int k) { // C++ Program to find kth element from two sorted arrays // In case we have reached end of array 1 int st1=0, st2=0; while (1) { if (st1 == n) return arr2[st2 + k - 1]; // In case we have reached end of array 2 if (st2 == m) return arr1[st1 + k - 1]; // k should never reach 0 or exceed sizes // of arrays if (k == 0 || k > (n - st1) + (m - st2)) return -1; // Compare first elements of arrays and return if (k == 1) return (arr1[st1] < arr2[st2]) ? arr1[st1] : arr2[st2]; int curr = k / 2; // Size of array 1 is less than k / 2 if(curr-1>=n-st1) { // Last element of array 1 is not kth // We can directly return the (k - m)th // element in array 2 if (arr1[n - 1] < arr2[st2 + curr - 1]) return arr2[st2 + (k - (n - st1) - 1)]; else { st2 += curr; k -= curr; } } // Size of array 2 is less than k / 2 if(curr - 1 >= m - st2) { if (arr2[m - 1] < arr1[st1 + curr - 1]) return arr1[st1 + (k - (m - st2) - 1)]; else { st1 += curr; k -= curr; } } else { // Normal comparison, move starting index // of one array k / 2 to the right if (arr1[curr + st1 - 1] < arr2[curr + st2 - 1]) 🥲🥲🥲🥲🥲🥲🥲//This condition fails if the size of the step ..i.e curr exceeds the size of array...so this code is giving me error if there is a skew in the size of the array { st1 += curr; k -= curr; } else { st2 += curr; k -= curr; } } } } // Driver code int main() { int arr1[7] = { 5 ,33, 55, 65, 76, 80, 90 }; int arr2[39] = { 10 ,13 ,14, 15, 15, 22, 27, 32, 34, 36, 36, 37, 39, 40, 42, 45, 49, 50, 50, 53, 56, 56, 57, 61, 65, 66, 70, 70 ,71, 74, 78, 84, 87, 90, 91, 94, 94 ,96 ,99 }; int k = 39; int p= kthElement(arr1, arr2, 7, 39, k); return 0; }
What exactly is your question? Is the element that you are looking for the k'th element in the array merged from the 2 sorted arrays? If yes, shouldn't you then be also checking the sum of the lengths to see if k is within that? And your code for merging doesn't seem right (I didnt go through it all. Just skimmed through).Check the code for merging in merge sort algorithm. That is exactly what you are looking for.
Anonymous
I didn't merge...I used a log k algorithm
What is your question then? Are you just sharing your code so that others can use it?
Sandeep
no...see the line with sad emojiis
Sandeep
thaths the line causing error
Sandeep
i donno how to rectify
Anonymous
no...see the line with sad emojiis
Can you just give a brief outline of your algorithm?
Sandeep
compare the k/2 th elements of each array
klimi
Is that true?
its a meme
Sandeep
which ever element is small..go to that corresponding array and increment the starting index of that array
Sandeep
can i dm
Anonymous
which ever element is small..go to that corresponding array and increment the starting index of that array
What if one array has less than k/2 elements but the elements towards the end are all greater than the k'th element?
Anonymous
yes see the algorithm...if(curr-1<n-start1)
This is why I asked you to give the outline. It is difficult to just understand it based on your code
Sandeep
hazer_hazer
Hi I've got template function that I directly call somewhere: template<class First, class ...Rest> void foo(First && first, Rest && ...other) const { bar(first); bar(other...); } It invokes these: template<class Arg> void bar(Arg && single) const { std::cout << single << ' '; } void bar(Specific && specific) const { std::cout << "SPECIFIC"; } template<class ...Args> void bar(Args && ...args) const { (bar(std::forward<Args>(args)), ...); } I thought that it gonna work because C++ chooses the most specific overloading, but, as I see, it doesn't if I call it from another templated function, so bar(Specific) never be called. I know, that if I directly call bar then bar(Specific) will be used if I pass Specific type, anyway, from in templated function it is not. Help please 😔
hazer_hazer
What is specific a template specialization?
It is just not templated. It is a specific type
Anonymous
i am not sure but as far as i remember you need the template <> part for specialisation to work
hazer_hazer
Anonymous
I've already tried this, but this functions are methods, so I cannot use template<>. I'll try again with separate struct to use template<>
https://en.cppreference.com/w/cpp/language/member_template#Member_function_templates A non-template member function and a template member function with the same name may be declared. In case of conflict (when some template specialization matches the non-template function signature exactly), the use of that name and type refers to the non-template member unless an explicit template argument list is supplied. 🤷
Anonymous
// C++ Program to find kth element from two sorted arrays #include <iostream> using namespace std; int kthElement(int arr1[], int arr2[], int n, int m, int k) { // C++ Program to find kth element from two sorted arrays // In case we have reached end of array 1 int st1=0, st2=0; while (1) { if (st1 == n) return arr2[st2 + k - 1]; // In case we have reached end of array 2 if (st2 == m) return arr1[st1 + k - 1]; // k should never reach 0 or exceed sizes // of arrays if (k == 0 || k > (n - st1) + (m - st2)) return -1; // Compare first elements of arrays and return if (k == 1) return (arr1[st1] < arr2[st2]) ? arr1[st1] : arr2[st2]; int curr = k / 2; // Size of array 1 is less than k / 2 if(curr-1>=n-st1) { // Last element of array 1 is not kth // We can directly return the (k - m)th // element in array 2 if (arr1[n - 1] < arr2[st2 + curr - 1]) return arr2[st2 + (k - (n - st1) - 1)]; else { st2 += curr; k -= curr; } } // Size of array 2 is less than k / 2 if(curr - 1 >= m - st2) { if (arr2[m - 1] < arr1[st1 + curr - 1]) return arr1[st1 + (k - (m - st2) - 1)]; else { st1 += curr; k -= curr; } } else { // Normal comparison, move starting index // of one array k / 2 to the right if (arr1[curr + st1 - 1] < arr2[curr + st2 - 1]) 🥲🥲🥲🥲🥲🥲🥲//This condition fails if the size of the step ..i.e curr exceeds the size of array...so this code is giving me error if there is a skew in the size of the array { st1 += curr; k -= curr; } else { st2 += curr; k -= curr; } } } } // Driver code int main() { int arr1[7] = { 5 ,33, 55, 65, 76, 80, 90 }; int arr2[39] = { 10 ,13 ,14, 15, 15, 22, 27, 32, 34, 36, 36, 37, 39, 40, 42, 45, 49, 50, 50, 53, 56, 56, 57, 61, 65, 66, 70, 70 ,71, 74, 78, 84, 87, 90, 91, 94, 94 ,96 ,99 }; int k = 39; int p= kthElement(arr1, arr2, 7, 39, k); return 0; }
are you sure you are even allowed to solve this is linear time. this looked like a leetcode problem so i checked. https://leetcode.com/problems/median-of-two-sorted-arrays/ imagine dividing the sorted array into two sections, with indices [0, k) and [k, m + n) (don't actually create the new array). find out how many elements the larger array contributes to the [0, k) part using binary search. use that information to deduce the kth element.
kin
hi
klimi
hi
nohello.com
kin
can anyone help me with this plz
kin
https://gist.github.com/aninditkarmakar/ac012e0459c9bf4b76ea
Hamad
Hi everyone
Himanshu
Hey guys I have a doubt can I post code here ??
Hamad
I want to make a code in matlab gui to be a text watermarking in image
Hamad
I want to make a code in matlab gui to be a text watermarking in image
Do u know from where I can I get it or anyone can help on it?
hazer_hazer
the cppreference example works for me :/
This example does not use template function from template function and it is also dependent on return type. My problem is that I call template<T>bar from template<Arg>foo and want foo to call bar<Specific> when I pass Specific to foo 😭
hazer_hazer
Hey guys I have a doubt can I post code here ??
just enclose it into monospace backticks
Himanshu
1st----- #include<bits/stdc++.h> using namespace std; void check_req(int X, int Y, int K, int N){ bool oke = false; int page[N], cost[N]; for(int i=0; i<N; i++){ cin >> page[i] >> cost[i]; } for(int i=0; i<N; i++){ if(page[i] >= X-Y && cost[i] <= K){ oke = true; break; } } if(oke) cout << "LuckChef" << endl; else cout << "UnluckyChef" << endl; } int main(){ int t; cin >> t; int X, Y, K, N; for(int i=0; i<t; i++){ cin >> X >> Y >> K >> N; check_req(X, Y, K, N); } return 0; } 2nd---- #include <iostream> using namespace std; int main() { int t; cin>>t; for(int i=0; i<t; i++) { int X, Y, K, N; cin>> X >> Y >> K >> N ; int pages[N], costs[N]; for(int i=0; i<N; i++) { cin>>pages[i]>>costs[i]; } bool flag = false; for(int i=0; i<N; i++) { if(pages[i] >= X-Y && costs[i] <= K) { flag = true; break; } } if(flag) cout<<"LuckyChef"<<endl; else cout<<"UnluckyChef"<<endl; } return 0; } Why 2nd code is faster than 1st code??
Himanshu
just enclose it into monospace backticks
Sorry but I think you need to review full code
hazer_hazer
hazer_hazer
? I'm talking about ``````
it's just easier to read. you also can just select your code and Right-Click -> Format -> Monospace
Hamad
Guys can u please tell me where is the error here in this code? If ("!image.data") { Cout << "could not open or" ; Cout << "find the image" ; Cout << endl; Return 0; }
Hamad
u use string as boolean
What should I change
hazer_hazer
So, if will be after the return??
no, it is just in lowercase
hazer_hazer
Guys can u please tell me where is the error here in this code? If ("!image.data") { Cout << "could not open or" ; Cout << "find the image" ; Cout << endl; Return 0; }
also, what is "!image.data"? Looks like path to file, but file path cannot contain ! as far as I know. String literal is a contstant, so there's no need to check it in if. If you want to check if file exists or something else it is more complex than just if "..."
Rohit
Hii
Rohit
Hello
Kalpak
Hi
Kalpak
I have some error
Kalpak
But I can't send my error
Kalpak
*You people i need help please* *I'm compiling my first Kotlin file in Intellij IDEA* *I'm getting some errors i don't understand* *Yet it's just a simple Hello World Code*
Kalpak
Any one solve my error Please help me