Nov 27, 2013 at 12:36am UTC
Hi, I'm really new to programming and I'm having trouble using a function to let a user input array values and then passing those values to other functions. Can anyone tell me where I went wrong? I'm getting a buffer overrun error.
#include <iostream>
using namespace std;
double input(double[], int);
double display(double[], int);
double getAvg(double[], int);
typedef double arrayType[];
int main()
{
int const SIZE=10;
double myArray[SIZE];
double ans2, ans3;
for (int count=0; count<SIZE; count++)
myArray[SIZE]=input(myArray, SIZE);
ans2=display(myArray, SIZE);
ans3=getAvg(myArray, SIZE);
system("Pause");
return 0;
}
double input(int myArray[], int SIZE)
{
int a;
for(a=0; a<SIZE; a++)
{
cin>>myArray[a];
}
return myArray[10];
}
double display (int myArray[], int SIZE)
{
int a, count=1;
for (a=0; a<SIZE; a++)
{
cout<<"Grade "<<count<<"is "<<myArray[a]<<" ";
count++;
}
return 0;
}
double getAverage(int myArray[], int SIZE)
{
int a, sum=0;
double avg;
for (a=0; a<SIZE; a++)
{
sum += myArray[a];
}
avg=(sum/SIZE);
cout<<"The average of the ten numbers is "<<avg<<".";
return 0;
}
Nov 27, 2013 at 12:55am UTC
Arrays are zero-indexed, so if you have an array of SIZE elements, you can access elements 0 through SIZE-1. array[SIZE] is out-of-bounds.
Specifically this assignment:
myArray[SIZE]=input(myArray, SIZE);
is wrong.