i'm trying to do a program where the user will give some numbers and the program will find the number that its half and its double are included in the numbers that the user gave..
Please can you help me with the code? i am a beginner
#include<iostream>
using namespace std;
int main()
{
int N;
cin>>N;
int spitia[N];
int i;
int a,b;
bool c=false,d=false;
for(i=0;i<=N-1;i++)
cin>>spitia[i];
for(i=0;i<=N-1;i++)
{
a=spitia[i]*2;
b=spitia[i]/2;
for(i=0;i<=N-1;i++)
{
if(spitia[i]==a)
c=true;
When you nest two for loops, don't use the same variable as the incrementing variable. Also when you print out spitia[i], set c and d back to false. Otherwise, they will remain true and the program will keep on printing spitia[i] until the loop end. Furthermore, use meaningful variable names and comment on your code. It helps a fellow contributor understand which variables are for what purpose and which part of code does what.
And also can you please explain clearly what your program should do with some examples?
well my program will take the number (N) of some numbers and will put these numbers on spitia[].after the program has to find a number which its half(a) and its double(b) is included in the numbers the user gave and print this number. sorry for my english
When you nest two for loops, don't use the same variable as the incrementing variable. do you mean i ? do i have to use j for the second for ?
thank you for the help
Well, your problem occurs right when you create that array. You see, arrays cannot be normally created to the size of a variable entered by a user. Static arrays, anyway. You need to make a dynamic array, which is much more complicated (pointers, new/delete). If you feel confident in what you are trying to do here, feel free to do a quick search for dynamic arrays. Otherwise, you might want to try a different method.
do you have an idea how i can create a program that does what i said before?
Yes. Actually, there's a simpler algorithm than the one (I think) you're trying to implement:
For each number n entered by the user, store n in a container and n * 4 in a different container.
Then, for each number n in the intersection (τομή) of the containers, the number you want is n / 2.
Why are you using floats? Why not just use "ints" to increment and declare size of the array (or even better, size_t)?
Also, you can't dynamically allocate like that. If your compiler lets you (especially if it doesn't even give a warning), get a better compiler. You should be allocating the array like @mobotus suggested above.
EDIT:
This is a C++ program, you should probably avoid using C-Style casts anyway (probably want something like static_cast for this)