where is the mistake..?

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;

if(spitia[i]==b)
d=true;
}
if((c==true)&&(d==true))

cout<<spitia[i];

}

return 0;
}
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?
Last edited on
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.
so i cant create an array with an unknown number ?
do you have an idea how i can create a program that does what i said before?
thanks by the way
You can create a dynamic array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>

int main(){

	int num;
	std::cout << "Enter size of array: ";
	std::cin >> num;
	std::cout << std::endl;
	
	int* ary = new int[num];

	for (int i = 0; i < num; i++){
		std::cout << "Enter a number: ";
		std::cin >> ary[i];
		std::cout << std::endl;
	}

	//Print ary
	for (int i = 0; i < num; i++)
		std::cout << ary[i] << ' ';
	
	delete[] ary;
	return 0;
}
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.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <algorithm>
#include <iterator>
#include <iostream>
#include <vector>
#include <set>

int main()
{
    std::set<int> s, s4;

    int n;

    while (std::cin >> n) {
        s .insert(n    );
        s4.insert(n * 4);
    }

    std::vector<int> r;

    std::set_intersection(s .begin(), s .end(),
                          s4.begin(), s4.end(), std::back_inserter(r));

    for (auto n : r) {
        std::cout << n / 2 << " ("
                  << n / 4 << ", "
                  << n     << ") " << std::endl;
    }

    return 0;
}

http://ideone.com/SSPtXJ

Needless to say, if this is homework and you turn in the
solution I posted above, you'll probably get zero points.
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 ?


Yes thats exactly what I meant :).

And does this program do what you want?:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>

int main()
{
    int N;
    float a, b;
    
    bool c = false, d = false;
    
    std::cout << "Enter N: ";
    std::cin >> N;
    
    std::vector<float> num_vec(N);
    
    std::cout << "Enter the numbers: " << std::endl;
    
    for(int i = 0; i < N; i++)
    {
        std::cin >> num_vec[i];
    }
    
    for(int j = 0; j < N; j++)
    {
        a = num_vec[j] * 2;
        b = num_vec[j] / 2;
        
        for(int k = 0; k < N; k++)
        {
            if(num_vec[k] == a) 
            {
                c = true;
            }
            else if(num_vec[k] == b)
            {
                d = true;
            }
        }
        
        if(c == true && d == true)
        {
            std::cout << '\n' << "Result: " << num_vec[j] << std::endl;
        }
        
        c = false;
        d = false;
    }
    
    return 0;
}
Last edited on
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)
Last edited on
@ NT3: Well, I used g++ (MinGW 4.8.1) to compile the program and it compiled without any warnings/errors.

EDIT: OK, I've fixed the code :P.
Last edited on
With the warnings/errors, if you compile using -Wall -pedantic-errors (as you should), you will get at least an error.
Thank you so much for your help :))
Topic archived. No new replies allowed.