Finding smallest number from user input(array) ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bernardo
    New Member
    • Dec 2010
    • 2

    #1

    Finding smallest number from user input(array) ?

    Okay, so I am making a program that accepts user input of integers. As you can see I am using an array and i need to get the greatest number, and smallest, range, mean, etc. But I successfully did this for the greatest and figured not much would need to be changed but everything I've tried has given me errors of like -8732113 as the smallest. If needed i can post the code with the greatest included, I took it out so i could just focus on this.

    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    
    void main()
    {
    	int nums[50];
    	int howmany=0;
    	int largest;
    	int smallest;
    	int x;
    
    	while(cin >> x)
    	{
    		nums[howmany] = x;
    		howmany++;
    	}
    
    	
    
    	smallest = nums[0];
    
    	for (int s=50; s >= howmany; s--)
    	{
    		if(nums[s] <= smallest)
    			smallest = nums[s];
    	}
    
    
    	
    
    	cout << howmany << "\n\n";
    	cout << smallest << "\n\n";
    
    	getche();
    }
  • Bill Hodgson
    New Member
    • Dec 2010
    • 1

    #2
    Try initializing smallest and largest with 0.

    Comment

    • Bernardo
      New Member
      • Dec 2010
      • 2

      #3
      well for the largest on my original code its largest = nums[0] which seemed fine so you're suggesting i use smallest=0 instead of nums[0]??

      Comment

      • fishlover
        New Member
        • Nov 2010
        • 5

        #4
        There is two question in you program, I think.
        First, the array nums[50] should be initialized.
        Second, I think there will be a dead loop in lines 15-19.
        and if you don't want to initialize the array, you can define it as global variables, and the compilier will defautly initialized it with 0;

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          1. What happens in the user enters more than 50 values? You would write past the end of the array. That would be bad.
          2. The first location accessed in your find-minimum loop is nums[50]. This is one slot past the end of the array. It is not good to access past the end of the array.
          3. The find-minimum loop examines those locations in the nums array that were not filled with user input; so you're finding the smallest randomly uninitialized value in the unused locations at the end of the array rather than the smallest value entered by the user.
          4. You were right to initialize smallest (and largest) to nums[0]. Do not follow the advice of initializing it to zero. What if the smallest user value were 10? You would erroneously report a smallest value of 0.

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            Yes.....a bad suggestion to initialize with 0

            Comment

            Working...