someone solve it please

write a program to: read 2000 values from the user then ask the user about a number N, then display how many numbers of N in this array ,then add each two cells, and display how many numbers of N in this array after addition.
We don't write people's homework for them. Try writing it, then when you hit a problem, come back and tell us, specifically, what the problem is.
Syntax:
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.h>

void main()
{
    int no[2000],i,c=0,N;
		
    for(i=0;i<2000;i++)
    {
        cin>>no[i];
	}
	for(i=0;i<2000;i++)
	{
		cin>>N;

        if(N=no[i])
		{
            c++;
		}
	}
    
    cout<<"numbers of N in this array"<<endl;
		cout<<c<<endl;
}

Okay that's the first part I wrote and the count is wrong, can you find out where is the problem?
Last edited on
The logic of your code doesn't coincide with what you're trying to accomplish. I'm going to reformat for readability:

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
#include <iostream> // <iostream.h> is a deprecated library that doesn't compile on some compilers

using namespace std; // the standard (std) namespace is needed for your compiler to know
// cout and cin are

// main returns an int of value 0 if code executed succesfully
int main()
{
    int no[2000] , c = 0, N; // the int i should be variable that is local to your for loop,
    // otherwise when you're done looping you'll have an int doing nothing
		
		
    for (int i=0;i < 2000;++i) // add some space for readability
    // notice how the first part of the for loop declares and instantiates 
    // an int i with a value of 0 
    // after the loop ends, the memory for i will be freed
    {
        cin >> no[i];
    }
    
    for(int i=0;i < 2000;++i)
    {
        cin >> N;
        
        if(N == no[i]) //Use the equality operator == when testing for equality,
        //not the assignment operator =
        {
            ++c; //use prefix (++N) operator over postfix (N++)
            //when you don't need a copy of the value before incrementing
        }
    }
    
    cout << "numbers of N in this array" << endl << c << endl; // No need to call cout twice
    
    return 0; //return successful execution
}


Now that your code is a bit more readable, let's dissect the logic. Your first for loop is fine, you're going to store 2000 values as desired. Your second for loop is wear it gets murky. Inside your loop you're asking for N through each iteration of the loop and then testing it for the current index of your 2000 values(no[i]). Is that what you want? Your goal is to get one N, and then test it against your array. How do you think that you would accomplish this? Read the comments for general programming tips.
Last edited on
Topic archived. No new replies allowed.