Shahadat Shanto
I want to run a loop in a array of integers. Like, I took a 4 sized array. In this array, If I put any number more than 4 it will print false, otherwise it will print true. like, if the 4 size array is 1, 2, 3, 4 it will print true if the 4 sized array is 1, 2, 4, 5 it will print false how to do this? Actually I'm stuck in printing true multiple times
Do
It is so easy
Sylvester Lim
Hi all, i have a simple but confusing issue. { double r; double areaCircle= 3.142*r*r; cin >> r; cout << areaCircle; }
Sylvester Lim
why when I print areaCircle, it doesnt give me the output i want?
Sylvester Lim
lets say i enter 4.5 for radius ( r), output is 0 ...
Talula
Hi all, i have a simple but confusing issue. { double r; double areaCircle= 3.142*r*r; cin >> r; cout << areaCircle; }
#include <iostream> using namespace std; int main() { double r; cin >> r; double areaCircle = 3.142 * r * r; cout << areaCircle; return 0; }
klimi
Hi all, i have a simple but confusing issue. { double r; double areaCircle= 3.142*r*r; cin >> r; cout << areaCircle; }
so you have uninitialized double r which can hold any value that is currently in the memory (it also can be a zero) then you do 3.142*random*random which will give you random result then you load value into r but you don't use variable r anywhere after
Sylvester Lim
ouh , so if i were to build a program where it asks user to input the radius , I would have to do something like this? instead of directly using the one variable which stores the formula int main() { double r; cout<< "Please input the radius"; cin >> r; cout << 3.142*r*r; }
Ster-Devs
???
Sorry I responded to the wrong message 😅
Sylvester Lim
Thank you all appreaciate it
Aleksandr
Is it hard to contribute in opensoure project?
Talula
Is it hard to contribute in opensoure project?
No, if you know what you're doing, if you don't they will block you.
Anonymous
Hi all, i have a simple but confusing issue. { double r; double areaCircle= 3.142*r*r; cin >> r; cout << areaCircle; }
{ double r; double areaCircle; cin >> r; areaCircle=3.142*r*r; cout << areaCircle; }
Anonymous
Why is this group restricted from posting pictures?
Anonymous
it's inconvenient
Aquatica
Why is this group restricted from posting pictures?
To prevent people from sending photos of their code instead of using pastebin
Anonymous
what’s the problem? int main() { printf("segmentation fault"); }
Talula
Why is this group restricted from posting pictures?
Because we don't need to see your homework posted with picture clicked using your phone.
blify
what would be better? and why ? size_t szArr = strlen(someString); for(int i = 0; i < szArr; i++) ... or for(int i = 0; i < strlen(someString); i++) ... thanks
the best for everyone
how can I let this loop start counting the index from 1 instead of zero
Ludovic 'Archivist'
Please do not send code that way, send code through pastebin.com or godbolt.org
Ludovic 'Archivist'
@zey34354
\Device\NUL
what would be better? and why ? size_t szArr = strlen(someString); for(int i = 0; i < szArr; i++) ... or for(int i = 0; i < strlen(someString); i++) ... thanks
the first one but i prefer iterate the string until find NULL rather than do strlen() The second one, the strlen() part is O(N). It will called again and again on the loop
Anonymous
#include<iostream> using namespace std; int main(){     int a[]={1,2,3,4};     int (&b)[4]=a;     int c[4]=a;     return 0; } hey, Please tell me where I am going wrong
Anonymous
why I have to do like this?
● Igor
why when std::vector needs to resize, it copies all contents and drop previous values? i mean, if its capacity is 10, and it needs to grow, why it does not allocate +10 space in heap, but keep old as it is example: my_vector = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 } size is 10, capacity is 10 and my_vector[9] = 1 now i push_back(0) it can allocate another size 10 space in heap, but it does not need to copy my_vector, just point indexes > 9 to the new space allocated, and <= 9 to the old allocated space
Anonymous
why when std::vector needs to resize, it copies all contents and drop previous values? i mean, if its capacity is 10, and it needs to grow, why it does not allocate +10 space in heap, but keep old as it is example: my_vector = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 } size is 10, capacity is 10 and my_vector[9] = 1 now i push_back(0) it can allocate another size 10 space in heap, but it does not need to copy my_vector, just point indexes > 9 to the new space allocated, and <= 9 to the old allocated space
No it can't do that. Firstly, the standard requires elements of a vector to be contiguously allocated meaning that for a vector, &a[i] +1 = &a[i+1] for all valid i. The consequences of this requirement are that an iterator into a vector is a random access iterator. If a vector were to be implemented like you suggested then accessing elements of a vector will no longer be a constant time operation. The Iterator or operator[] has to first determine the memory segment where the element is located and then access it. This is not a constant time operation and will bring in the inefficiencies of a list (bidirectional iterator instead of a random access iterator) as against the efficiencies of a list. Further think about the case where you insert an element. An insertion into a vector whose capacity exceeds the current size guarantees that all iterators pointing to an element prior to the insertion point remain valid. Now suppose say a vector is implemented like you suggested. Assume there are 3 chunks of memory that the vector is using to house it's elements. Now if you insert into the middle of the middle chunk, you will have to allocate more memory for the middle chunk and then move all elements from the middle chunk into the newly allocated area. This would not only invalidate the iterators for elements past the insertion in the middle chunk but also elements before the insertion point. Also it contradicts requirements on other methods that are required to be very efficient. For example size() is typically implemented as return static_cast<vector<T>::size_type>(end()-begin()); Now you can't do this anymore. All of these contradict the requirements placed on a vector by the standard.
● Igor
No it can't do that. Firstly, the standard requires elements of a vector to be contiguously allocated meaning that for a vector, &a[i] +1 = &a[i+1] for all valid i. The consequences of this requirement are that an iterator into a vector is a random access iterator. If a vector were to be implemented like you suggested then accessing elements of a vector will no longer be a constant time operation. The Iterator or operator[] has to first determine the memory segment where the element is located and then access it. This is not a constant time operation and will bring in the inefficiencies of a list (bidirectional iterator instead of a random access iterator) as against the efficiencies of a list. Further think about the case where you insert an element. An insertion into a vector whose capacity exceeds the current size guarantees that all iterators pointing to an element prior to the insertion point remain valid. Now suppose say a vector is implemented like you suggested. Assume there are 3 chunks of memory that the vector is using to house it's elements. Now if you insert into the middle of the middle chunk, you will have to allocate more memory for the middle chunk and then move all elements from the middle chunk into the newly allocated area. This would not only invalidate the iterators for elements past the insertion in the middle chunk but also elements before the insertion point. Also it contradicts requirements on other methods that are required to be very efficient. For example size() is typically implemented as return static_cast<vector<T>::size_type>(end()-begin()); Now you can't do this anymore. All of these contradict the requirements placed on a vector by the standard.
thanks for the great explanation. I have just a few questions: "This is not a constant time operation" Can't it be constant by using a idea similar to buckets? Lets say we determine that the size of the chunks will always be 10. Each bucket represents the chunk we want to index. So if i try to read from index 5: 5 / 10 = 0, so bucket 0 5 % 10 = 5, so index 5 of the bucket 0 If I try to read from index 28: 28 / 10 = 2, so bucket 2 28 % 10 = 8, so index 8 of bucket 2 "All of these contradict the requirements placed on a vector by the standard." I can see it wouldn't be a valid vector, but I'm thinking if it would be useful in some scenario
Anonymous
How can I run sort_heap on a min heap array? If I use greater<>(), it tends to think that I want to reverse sort the array.
Ludovic 'Archivist'
How can I run sort_heap on a min heap array? If I use greater<>(), it tends to think that I want to reverse sort the array.
Heap sort is generally done by putting the largest value of the heap (the start) at the end and recovering the invariant of the heap. There is a bottom-up heapsort but I never used it
Anonymous
How can I run sort_heap on a min heap array? If I use greater<>(), it tends to think that I want to reverse sort the array.
sort_heap works only on a max heap. That is the precondition and it can't be run on min heap. If you want it to work on a min heap, you can just use a comparison operator that returns the opposite of what a less operator would return and then do a reverse iteration on the resulting range.
X
#include <stdio.h> #include <stdlib.h> void free(void *ptr); int main(){ int *p; int *q; int i; q = (int *)malloc(10*sizeof(int )); p = (int *)malloc(5*sizeof(int )); p[0] =3; p[1] =5; p[2] =7; p[3] =9; p[4] =11; for(i = 0;i<5;i++) q[i] = p[i]; free(p); p = q; q = NULL; for(int i = 0;i<5;i++) printf("%d \n",q[i]); }
X
I have a question about this code. I have sorted my arrays to q pointer but when I want to execute this code it returns an error SIGSEGV ?
X
instead, changing my q pointer to p, it works fine
Ammar
I have a question about this code. I have sorted my arrays to q pointer but when I want to execute this code it returns an error SIGSEGV ?
This is a NULL pointer dereference. q = NULL; for(int i = 0;i<5;i++) printf("%d \n",q[i]); after setting q to NULL, you must not dereference it. Or it will segfault.
X
A yes i see it thanks !!
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
Hi guys, I have a fgets question. So, if I have a buffer char buff[16]; I think it is necessary to check if the user gave me more than 15 chars. If so, what remains staies on the stdin channel and is potentially dangerous if there's another I/O operation. Therefore, I check if in buff we also get the \n parameter: if so, it means that the user did give me something less than 15 bytes, otherwise I'll have to clear stdin as follows: if(input != "\n") while(getchar() != "\n"); Is this correct? And, if so, given the possibility that the user in fact did give me something between 1 - 15 bytes, do I have to remove the final \n? I never did and the code always worked fine, but now I'm wondering if it's necessary. Thank you!
Gianni
Hi guys, what does mean "?" in c ? There is this expression: Cmp= (Cmp<=65535-Td/2)? (Cmp+Td/2) : 65535;
morningstar
Does anyone know why it is producing seg fault? Thanks int main(void) { char *url = "http://www.youtube.com/index.html"; remove_filename(url); puts(url); return 0; } void remove_filename(char *url) { while (*url++); while (*url-- != '/'); *++url = '\0'; }
\Device\NUL
I prefer use scanf with max field width so i can Input string safely
Anonymous
Does anyone know why it is producing seg fault? Thanks int main(void) { char *url = "http://www.youtube.com/index.html"; remove_filename(url); puts(url); return 0; } void remove_filename(char *url) { while (*url++); while (*url-- != '/'); *++url = '\0'; }
Think when the while(*url++); loop would end. It would end when url points to one past the end of the url array. Now in while(*url-- != '/') you are dereferencing an invalid url pointer as it is pointing one past the end. This is Undefined Behavior.
Anonymous
String literal are already immutable, out of bound or not
The problem I mentioned happens even before he tries changing the string literal. That is why I pointed it out explicitly. Changing his code to char url[] = "http://www.youtube.com/index.html"; would still exhibit the issues I pointed out in my last message
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
I prefer use scanf with max field width so i can Input string safely
This is also a possibility, you are referring to the safe scanf then?
\Device\NUL
I don't use fgets since it can contain newlines and i lazy to iterate it
Anonymous
what the extra qualification error means in c++?
pavel
what the extra qualification error means in c++?
Unnecessary class name before function call
Anonymous
#include"RYBase" RYQuad RYDot::operator < (const RYDot OtherDot){ return RYQuad(*this,OtherDot); }
Anonymous
i got this error on cpp file
Anonymous
maybe because RYDot::?
pavel
Show full error
Anonymous
ok
Anonymous
In file included from main.cpp:2: RYBase:13:12: error: extra qualification 'RYDot::' on member 'operator<' [-fpermissive] 13 | RYQuad RYDot::operator < (const RYDot OtherDot); | ^~~~~
pavel
Yes
pavel
Try remove RYDot::