#include <iostream>
#include <cassert>
// Question M
//Write a program that asks a user to enter the size of a dynamic array that stores scores obtained by students.
// Create the dynamic array and a loop that allows the user to enter a score into each array element.
// Loop through the array, find the maximum score and output it.
// Delete the memory allocated to your dynamic array before exiting your program.
usingnamespace std;
int main()
{
int arraySize;
int *arrayName;
arrayName = newint [arraySize] ;
int studentScores = 0 , maxScore = 0 ;
int i;
cout << "Enter the size of the array" << endl;
cin>>arraySize;
//assert ( arraySize > 0 ); // checking for a number greater than zero
cout<<" You entered and array of size "<<arraySize<<endl; // feedback
cout<<" Enter the scores for each student "<<endl; // requesting input for student scores
cin>> studentScores;
while ( studentScores >= 0 )
{
for ( i = 0 ; i <= arraySize ; i++)
arrayName[arraySize] = studentScores;
cout<< " Student scores are as follows ; "<<arrayName[i];
if (arrayName[i] > maxScore)
{
maxScore = arrayName[i];
cout<< " Maximum student score is "<<maxScore;
}
}
delete [] arrayName;
return 0;
}
You are creating the array with a size of arraySize before getting a value for arraySize. So the error you should get is arraySize not initialized. What you need to do is create the array after getting a value for arraySize.