Array - cant't find the smallest number

I'm assigning values from 0-30 to an array and expecting it to tell me that the smallest element is element no. 0, with the value of 0. But it tells me that the smallest element is element no. 1, with the value of 1.

Can anyone see what I'm doing wrong?

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
//header files
#include<stdio.h>

//Initializing constants

int grill[31] = {0};

int i;
int j;
int high;
int low;

int main(void)
{
	for(i=0;i<=30;i++)
	{
		grill[i] = i;
	}

	for (j=0;j<=30;j++)
	{
		if (grill[j]>grill[high]) high=j;
		if (grill[j]>0 && grill[j]<low) low=j;
		if (grill[low]==0) low=j;
	}

	//utskrift
	printf("\n\n The largest element is element nr %d. The value is %d", high,grill[high]);
	printf("\n\n The smallest element is element nr %d. The value is %d", low, grill[low]);
	return 0;
}
I'm assigning values from 0-30 to an array and expecting it to tell me that the smallest element is element no. 0, with the value of 0


if (grill[low]==0) low=j;
Is that where the error is?
You didn't declare values for high and low so they can be anything

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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <fstream>
using namespace std;

int main()
{
	int grill[31] = {0};
	
	int high=0;
	int low=0;
	
	
	for(int i=0;i<=30;i++)
	{
		grill[i] = i;
	}
	
	for (int j=0;j<=30;j++)
	{
		
		if (grill[j]>high) 
			high=grill[j];
		if (grill[j]<low) 
			low=grill[j];
	}
	
	cout << high << " " << low;
	
	
	system("PAUSE");
	return EXIT_SUCCESS;
}

wakadarc wrote:
You didn't declare values for high and low so they can be anything

Global variables are initialized to 0.
Topic archived. No new replies allowed.