The code is not working to find the max min for only negative nos.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//Count of +ve, -ve, zeroes and the smallest and largest number among it.
#include <iostream>
using namespace std;
int main()
{
	long long int range=0, cp=0, cn=0, cz=0, lpos=0, lneg=0, num, limit, lposs, lnegl, temp;
	cout << "\nEnter the count of numbers you want to enter : ";
	cin >> limit;
	if (limit<=0)
	{
		cout << "Aborting !!!";
	}
	else
	{
		limit--;
		cout << "Now enter the numbers : ";
		do
		{
			cin >> num;
			if (num>0)
			{
				cp++;
				if (num>lpos)
				{
					lpos=num;
				}
				else
				{
					lposs=num;
				}
			}
			else if (num<0)
			{
				cn++;
				if (num<lneg)
				{
					temp=num;
					lneg=num;
					if (temp>lneg)
					{
						lnegl=num;
					}
				}
			}
			else
			{
				cz++;
			}
			range++;
		}
		while (range<=limit);
		cout << "The count of positive numbers is : " << cp;
		cout << "\nThe count of negative numbers is : " << cn;
		cout << "\nThe count of zeroes is : " << cz;
		if (cp==0 && cz==0)
		{
			cout << "\nThe largest number is : " << lnegl;
		}
		else if (cp==0 && cz>0)
		{
			cout << "\nThe largest number is : 0";
		}
		else
		{
			cout << "\nThe largest number is : " << lpos;
		}
		if (cn==0 && cz==0)
		{
			cout << "\nThe smallest number is : " << lposs;
		}
		else if (cn==0 && cz>0)
		{
			cout <<"\nThe smallest number is : 0"; 
		}
		else
		{
			cout <<"\nThe smallest number is : " << lneg;
		}
	}
	return main();
}


Sample Output:-

Enter the count of numbers you want to enter :3
Now enter the numbers : -4
-2
-3
The count of positive numbers is : 0
The count of negative numbers is : 3
The count of zeroes is : 0
The largest number is : -1 !!!The problem is here. Can you fix this.!!!
The smallest number is : -4

I don't want to use array in this program. So suggest me without using any array.
Last edited on
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
#include<iostream>

int main()
{
    int n ;
    std::cout << "Enter the count of numbers you want to enter: " ;
    std::cin >> n ;

    if( n > 0 )
    {
        int cnt_negative = 0 ;
        int cnt_zero = 0 ;
        int min_negative = 1 ;
        int max_negative = std::numeric_limits<int>::min() ;

        std::cout << "Now enter the numbers: " ;
        for( int i = 0 ; i < n ; ++i )
        {
            int number ;
            std::cin >> number ;

            if( number < 0 )
            {
                ++cnt_negative ;
                if( number < min_negative ) min_negative = number ;
                if( number > max_negative ) max_negative = number ;
            }
            else if( number == 0 ) ++cnt_zero ;
        }

        std::cout << " count numbers: " << n << '\n'

                  << "count negative: " << cnt_negative << '\n'
                  << "    count zero: " << cnt_zero << '\n'
                  << "count positive: " << n - cnt_negative - cnt_zero << '\n'

                  << "the smallest negative number: " << min_negative << '\n'
                  << " the largest negative number: " << max_negative << '\n' ;
    }
}
I want to find the largest and smallest number from user input values.
My program almost works fine.
If I input -4, -2, -3. The program shows -1 is the largest. This is not possible. the largest number is -2 but not -1. I just want to find the largest and smallest number among the list irrespective of negative or positive.
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
#include <iostream>
#include <limits>

int main()
{
    int n ;
    std::cout << "Enter the count of numbers you want to enter: " ;
    std::cin >> n ;

    if( n > 0 )
    {
        int cnt_negative = 0 ;
        int cnt_zero = 0 ;
        int min_val = std::numeric_limits<int>::max() ;
        int max_val = std::numeric_limits<int>::min() ;

        std::cout << "Now enter the numbers: " ;
        for( int i = 0 ; i < n ; ++i )
        {
            int number ;
            std::cin >> number ;

            if( number < 0 ) ++cnt_negative ;
            else if( number == 0 ) ++cnt_zero ;

            if( number < min_val ) min_val = number ;
            if( number > max_val ) max_val = number ;

        }

        std::cout << " #numbers: " << n << '\n'

                  << "#negative: " << cnt_negative << '\n'
                  << "    #zero: " << cnt_zero << '\n'
                  << "#positive: " << n - cnt_negative - cnt_zero << '\n'

                  << "the smallest number: " << min_val << '\n'
                  << " the largest number: " << max_val << '\n' ;
    }
}
Thanks for the program.
Topic archived. No new replies allowed.