Your suggested code has a number of logic issues which I will specify first, followed by my code which implements these notes, and finally, the result I obtained with this corrected code:
1) oc does not need to be a float and m is not needed, instead, define oc as an int and use it to store the number of occurrences.
2) I would highly recommend you sanitize your input to ensure only an int is being accepted as valid input before application execution resumes. The code below is adapted from How to make cin take only numbers except that the break statement is eliminated and a boolean variable is used instead. This is an important programming best practice as it is not safe to assume that a user, or calling application, will provide valid input.
3) Though not always necessary, it is good practice to initialize all of your variables with initial value. It is; however, necessary to to define initialize values for variables that are used before a value is defined (otherwise other application errors could occur). I have fixed this as well as it results in 0 code warnings.
4) The first while loop is completely unnecessary in addition to being incorrect for what is being requested and may have been intended to accomplish.
5) The logic in the first for loop, minus the incorrect break statement (especially since there is interest in the number of occurrences and not just the first one) and follows the while loop, can be used, and combined, once the break statement is removed, to count the number of occurrences as well as reporting which locations the number to search for has been found.
6) I left the location reference almost exactly the way you had it; however, do note that location 1, as you refer to it in your question, is actually the 0th element of the num array. It may be better if the code does not add a 1 to the location unless this is exactly how it is required. I am assuming that is how it is required so I left that part intact as-is.
7) Based on the requirements specified above, the number of occurrences of the search number should be 0 before the big and small numbers are determined, and displayed, otherwise their output does not need to be provided in the program output.
8) Lastly, the logic for determining big and small numbers uses num[20]. Note that num[20] does not exist in the array and, therefore, the value of num[20] is undefined and could potentially result in an access violation or unintended application consequences, although I did not find either in this case. Instead the count of 20, for number of values stored in the num array, should have been used.
Here is the code that I used, which corrected as per the above:
#include <iostream>
#include <limits>
#include <string>
#include <sstream>
using namespace std;
int main(int argc, char ** argv)
{
int num[20]={23,45,1,23,5,78,6,13,1,4,78,18,3,5,26,4,5,10,3,45};
int search = 0, c = 0, n = 0, big = 0, small = 0;
int oc = 0;
bool isint = false;
string line = "";
//search the item in the array
cout << "Enter the number to search: ";
while (!isint)
{
getline(std::cin, line);
stringstream ss(line);
if (ss >> search)
{
if (ss.eof())
{ // Success
isint = true;
}
}
if(!isint)
{
cout << "Please enter a valid number to search: ";
}
}
//show respective array locations
for(c = 0; c < 20; c++)
{
if(num[c] == search)
{
cout << search << " is present at location " << c + 1 << endl;
oc++;
}
}
cout << "Digit occurred " << oc << " times" << endl;
//show the largest and the smallest element
if(oc == 0)
{
big=num[0];
for(c = 1; c < 20; c++)
{
if(big < num[c])
{
big = num[c];
}
}
cout << "Largest element : " << big << endl;
small = num[0];
for(c = 1; c < 20; c++)
{
if(small > num[c])
small = num[c];
}
cout << "Smallest element : " << small << endl;
}
return 0;
}
Output for the Number 23:
Enter the number to search: 23
23 is present at location 1
23 is present at location 4
Digit occurred 2 time(s)
Output for the Number 25:
Enter the number to search: 25
Digit occurred 0 time(s)
Largest element : 78
Smallest element : 1