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