Using For Loop

Jul 29, 2013 at 10:38pm
Hello I'm suppose to write a program using (for loop) that asks the user to enter any amount of numbers, so that it can display the smallest and largest. My program successfully finds the largest, but I'm having trouble with
line 9:int number = 999;
although it does the job at displaying the smallest as long as one of the numbers entered has 3 digits or less , I still dont think Im doing it correct because if all the numbers I enter have more than 3 digits it will display the 999 as the smallest. Is there any way that it can be for any number entered?

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>
using namespace std;

int main()
{
  int amount;
  int count;
  int number = 0;
  int smallest = 999;
  int largest = 0;

  cout << "Enter total numbers to process: ";
  cin >> amount;

  for(count = 1; count <= amount; count++)
  {
    cout << "Enter number: ";
    cin >> number;

    if(number > largest)
    {
      largest = number;
    }

    if(number < smallest)
    {
      smallest = number;
    }
  }
  
  cout << "The largest integer is " << largest <<endl;
  cout << "The smallest integer is " << smallest <<endl;

  system ("pause");
  return 0;
}
Jul 29, 2013 at 10:48pm
1
2
3
4
 
#include <cstdint> 

 int smallest = intmax_t;   // largest possible integer for implementation 


Jul 29, 2013 at 11:52pm
Thank you! but when I try it it wont let compile because of line 16

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
#include <iostream>
#include <cstdint>




using namespace std;
int main()
{


 

 
    int amount;
    int smallest = intmax_t;// largest possible integer for implementation
    int count;
    int number = 0;
    int largest = 0;

 cout << "Enter total numbers to process: ";
 cin >> amount;

    for(count = 1; count <= amount; count++)
     {

 

        cout << "Enter number: ";

        cin >> number;

 


        if(number > largest)
        {

    largest = number;

}


       if(number < smallest)
       {

           smallest = number;


 

 

    }

 
}
 

    

    cout << "The largest integer is " << largest <<endl;

    cout << "The smallest integer is " << smallest <<endl;

 
system ("pause");
    return 0;

}
Jul 30, 2013 at 1:24am
Sorry. Should have been:
 
  int smallest = INTMAX_MAX;
Last edited on Jul 30, 2013 at 1:24am
Jul 30, 2013 at 2:36am
Thank for helping, I appreciate it.
Topic archived. No new replies allowed.