Unexpected closing

Hi!
I just wrote this code:
//This program take some numbers and output the average of them, by changing the type
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream.h>
void main(){
	int a[10];
	int i;
	cout<<"Please enter the numbers ";
	for(i=1;i<=10; i++)
		cin>>a[i];
	int sum=0;
	i=0;
	for(i=1;i<=10;i++)
		sum +=a[i];
	int avg=(sum)/10;
	cout<<"This the average = "<<avg;}

it gets the numbers, but it suddenly close program.
windows error: average.exe has stopet working.(just it)
I am using:
Windows 7.
Kaspersky internet security 9.
Visual studio 2000.
Firstly, use int main() instead of void main() and <iostream> as opposed to <iostream.h>. Plenty of debate / explanations on this forum - just search for it and you'll strike gold.

Your program is probably crashing because your accessing outside of your array's boundaries -

1
2
3
int a[10];
for (i = 1; i <= 10; i++)
    cin >> a[i];


You try to access a[1], which is good, but then as the loop progresses, you try to access a[10] because the for loop condition is still not fulfilled (i <= 10) and you will access a[10] which is outside of your array's boundaries. Remember when you declare an array it goes from 0 to n - 1 and in your case, these would be valid:
a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9]


Last edited on
Topic archived. No new replies allowed.