Anonymous
Given an array of positive integers. Your task is to find that element whose value is equal to that of its index value. Input: The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N, size of array. The second line of each test case contains array elements. Output: Print the element whose value is equal to index value. Print "Not Found" when index value does not match with value. Note: There can be more than one element in the array which have same value as their index. You need to print every such element's index separated by a single space. Follows 1-based indexing of the array. Constraints: 1 ≤ T ≤ 30 1 ≤ N ≤ 50 1 ≤ A[i] ≤ 1000 Example: Input: 2 5 15 2 45 12 7 1 1 Output: 2 1 For More Input/Output Examples Use 'Expected Output' option
Anonymous
#include <iostream> using namespace std; int main() { int t;cin>>t; for(int i=0;i<t;i++) { int sz; cin>>sz; int arr[sz]; for(int k=0;k<sz;k++) { cin>>arr[k]; } int count = 0; for(int j=0;j<sz;j++) { if(arr[j]==(j+1)) cout<<arr[j]<<" "; count++; } if(count==0) { cout<<"Not Found"<<endl; } cout<<""<<endl; } return 0; }
Anonymous
what is my error1??
Pavel
I guess, you output two line breaks when nothing is found
Pavel
But, the construction cout << "" << endl is really weird
Pavel
And, you shouldn't create arrays on stack usually
Pavel
Because stack is limited by 1-2-4-8 MB
Pavel
But in this task it doesn't play a role, because sz is less than 50, so the array will take less than 50 (size of array) * 4 (size of int) = 200 bytes
olli
And, you shouldn't create arrays on stack usually
agreed, C++ does not support VLAs (you can create arrays on the stack if you know the size in advance)
Nizar
Hhhhhhhh
Pavel
agreed, C++ does not support VLAs (you can create arrays on the stack if you know the size in advance)
Well, I guess that's wrong #include <iostream> int main() { int a[(int)1e7]; for (int i = 0; i * 1024 < 1e7; ++i) { a[i * 1024] = 1; } } This program crashes with SIGSEGV during the array declaration
Pavel
Perfect, symmetric construction, but absolutely meaningless
Pavel
Stack overflow
Yes, that's my point
olli
VLA is also on the stack btw.
I know, but the language C++ does not support VLAs, it's basically an extension of many compiler vendors since they support it in c anyway
Pavel
Upendra
Anybody having knowledge on rabbitmq with C++
Kaepee
/get cbook
Anonymous
/warn
Anonymous
Hello Kelebek can you help me a resource to start with Python, I basically know nothing about it
Asdew
#ot
Anonymous
Anyone need any type of free app code dm meee
Asdew
No.
Aman
/get cbook
Aman
i am geeting compilation error for this program
Aman
https://hastebin.com/upacozecah.cpp
Aman
Compilation Error: Compilation Error prog.cpp: In function ‘std::vector<int> printNonRepeated(int*, int)’: prog.cpp:50:22: error: cannot convert ‘std::unordered_set<int>::iterator {aka std::__detail::_Node_iterator<int, true, false>}’ to ‘int’ in assignment arr1[counter]=itr;
Anonymous
#include<stdio.h> #include<stdlib.h> struct slope{ int m; int x; int c; }; int slp(struct slope *q); int main(){ int z; struct slope s={2,3,4}; struct slope *p; p=&s; z=slp (p); printf("%d \n",z); } int slp(struct slope *q) { int y; y= q->m +q->x +q->c; return y; }
Anonymous
how can i minimize lines of code?
Vlad
how can i minimize lines of code?
For what particular reason?
Vlad
Code golf? :P
Anonymous
I got this as college assignment and it was asked that best possible optimized code
Anonymous
I was confused if I could optimize it furthermore
Anonymous
Best optimized doesn't mean the shortest.
Could this code be any shorter then?
Asdew
Well, you could remove newlines.
Vlad
Well, you could remove newlines.
That's bad idea. He's not code golfing :P
Asdew
Well, he wanted the shortest code.
Anonymous
Sure it could
What would be the best way?
Vlad
Well, he wanted the shortest code.
If he wants the shortest, then he could just do this: main() { puts("123");} where 123 is a hardcoded value
Asdew
It wouldn't do what the wants, which is calculation.
Vlad
So no difference really. And fast as heck :P
Anonymous
#include<stdio.h> #include<stdlib.h> struct slope{ int m; int x; int c; }; int slp(struct slope *q); int main(){ int z; struct slope s={2,3,4}; struct slope *p; p=&s; z=slp (p); printf("%d \n",z); } int slp(struct slope *q) { int y; y= q->m +q->x +q->c; return y; }
#include<stdio.h> union slope { struct { int m; int x; int c; }; char pad[32]; }; int main(){ struct slope s={2,3,4}; printf("%d \n", slp(&s)); } static inline int slp(struct slope *q) { return (q->m * q->x) + q->c; }
Asdew
Don't foonspeed.
Anonymous
Not at PC rn, can somebody tell me I'm padding correctly?
Anonymous
You can manually force a specific padding to occur by nesting a struct inside a union
Anonymous
#include <stdio.h> union slope { struct { int m; int x; int c; }; char pad[32]; }; static inline int slp(union slope *q) { return (q->m * q->x) + q->c; } int main() { union slope s = {2, 3, 4}; printf("%d \n", slp(&s)); }
Anonymous
Yeah I know. But why tho?
Tbh it's been so long since I've done this
Anonymous
Give me a few seconds
Vlad
Tbh it's been so long since I've done this
I see. You just want to have useless 24 bytes on each struct.
Vlad
For no particular reason
Anonymous
Meh they perform almost the same
Anonymous
Maybe if I turn on optimization?
Anonymous
Nah. Still the same
Vlad
Nah. Still the same
The only difference is size
Asdew
nohello.com
Kenny
i did it one year ago
Kenny
i could see
Anonymous
https://beej.us/guide/bgnet/
Dima
https://beej.us/guide/bgnet/
Asdew
I might have a TCP server I've written.
Daniele
Using vectors, is there a way to erase the object that calls is without passing the whole vector?