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
Iwan
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
and how is your code look like?
Anonymous
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
let me see your previous code
Do
It is so easy
Anonymous
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
Hey buddy first you have to run a for loop
Then the loop will start from 0 not from 1 ok
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
Ster-Devs
Talula
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;
}
Talula
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?
Anonymous
Anonymous
Runtime
Anonymous
Why is this group restricted from posting pictures?
Anonymous
it's inconvenient
Anonymous
what’s the problem?
int main()
{
printf("segmentation fault");
}
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
Anonymous
blify
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
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
Someone
Anonymous
why I have to do like this?
Leovan
Leovan
Anonymous
● 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
You know me
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
Bcoz, vector gurantees contigious memory. In other words we can access the emelemnts using [] similar to an array.
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
Anonymous
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.
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
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;
Aquatica
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
Gianni
\Device\NUL
\Device\NUL
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!
Clear stdin if the string doesn't contain newline
\Device\NUL
I prefer use scanf with max field width so i can Input string safely
Anonymous
Anonymous
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!
No it is not correct. What if getchar() returns EOF? You are not handling that case. You have to read a char untill you encounter '\n' or EOF.
\Device\NUL
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
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
\Device\NUL
\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
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::