Nov 4, 2018 at 10:34pm UTC
I need your assistance please, my code for average is giving me a negative average and instead of positive (pointers), what am i doing wrong
int main()
{
int *myIntPtr = NULL;
int arraySize = 0;
cout << "Enter array size:" << endl;
myIntPtr = new int[arraySize];
cin >> arraySize;
cout << endl;
srand(time(0));
cout << "10 random numbers between 1 and 10 generated are: ";
for (int i = 0; i < arraySize; i++)
{
*myIntPtr = 1 + rand() % (10 - 1 + 1);
cout << *myIntPtr << " ";
myIntPtr++;
}
*myIntPtr = *myIntPtr - arraySize;
int sum = 0.0;
double average = 0.0;
for (int i = 0; i < arraySize; i++)
{
sum += *myIntPtr;
average = sum / arraySize;
}
cout << "\nThe average of the elements in the array is " << average;
cout << endl;
cout << "Number of items above average: ";
int count = 0;
for (int i = 0; i < arraySize; i++)
{
if (*myIntPtr > average)
count++;
}
cout << count;
return 0;
}
Nov 4, 2018 at 11:31pm UTC
You are doing cin Into array size after you allocate.
there are a few more things. But please edit your post and put code into code blocks you have the option in the right above the BOLD option
Last edited on Nov 4, 2018 at 11:33pm UTC