highest and lowest value with array.

Hi. I don't know why it this code isn't working. I even tried to copy the format of another code that seemed to work perfectly fine and yet when I try it with mine it doesn't seem to work. Thanks.

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
  #include <iostream>
using namespace std;
int main()
{
    int i;
	double a[100];
	double bb,cc;
	bb = a[0];
	cc = a[0];
	cout<<"Input"<<endl;
	for(i = 0; i <= 7; i++){
		cin>> a[i];
	    if(cc > a[i]){
	    	cc = a[i];
		}
		else if(bb < a[i]){
			bb = a[i];
	    }
	    
	}
	
    cout<<"Highest: "<< bb <<endl;
    cout<<"Lowest: "<< cc <<endl;
return 0;
}
Put all the numbers into the array then sort the array. The highest number will be at the end and the lowest will be at the beginning.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    const unsigned SIZE = 8;
    double arr[SIZE] = {0};
    for(int i = 0; i < SIZE; ++i)
    {
        std::cout << "Enter a number ";
        cin >> arr[i];
        std::cout << std::endl;
    }

	std::sort(arr, arr + SIZE);

    cout<<"Highest: "<< arr[SIZE - 1] <<endl;
    cout<<"Lowest: "<< arr[0] <<endl;

    return 0;
}
Thanks for the answer. I just fixed it, I think. I put the cin and the choosing of the highest and lowest in different for loops. Also, I know this isn't the same question but how will I know where to put cins, cout, etc. to make the program work properly. Again, thanks. :)

*edit: ok it wasn't the cin, was the placing of the initialization of the variables. Is there a guide on how to initialize to make the program work properly? Thanks for the answer(s). :)
Last edited on
Topic archived. No new replies allowed.