#include <iostream>
#include <cassert>
#include <cstddef>
#include <cstdlib>
// 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 *arrayName, arraySize = 0 ;
arrayName = newint [arraySize] ;
int studentScores = 0 , maxScore = 0 ;
int i = 0;
cout << "Enter the size of the array ";
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 ( cin>>studentScores )
{
for ( i = 0 ; i <= arraySize ; i++)
arrayName[arraySize] = studentScores;
cout<< " Student scores are as follows ; "<<arrayName[i]<<endl;
if (arrayName[i] > maxScore)
{
maxScore = arrayName[i];
cout<< " Maximum student score is "<<maxScore<<endl;
}
}
delete [] arrayName;
return 0;
}
Hi seem to have made some corrections but there is still an error in picking up the correct values, Its only getting the first value and not the others
#include <iostream>
#include <cassert>
#include <cstddef>
#include <cstdlib>
// 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 *arrayName, arraySize = 0 ;
arrayName = newint [arraySize] ;
int studentScores = 0 , maxScore = 0 ;
int i = 0;
cout << "Enter the size of the array ";
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
while ( cin>> arrayName[i] )
{
for ( i = 0 ; i <= arraySize ; i++)
{
studentScores = arrayName[i];
cout<< " Student scores are as follows ; "<<studentScores<<endl;
}
if (studentScores > maxScore)
{
maxScore = studentScores;
cout<< " Maximum student score is "<<maxScore<<endl;
}
}
delete [] arrayName;
return 0;
}
while ( cin>> arrayName[i] )
This seems off. Shouldn't it be
for(... < arraysize ...) instead?
cin >> arrayName[I] (don't forget to I++ SOMEWHERE in your loop!)
not entirely sure what exactly you WANT to happen in this loop.
But your logic seems ODD (which is a nice way of saying, it looks wrong) for any practical purposes.
It looks to me like you should read them all in (for loop)
and then do something like maxscore = max(maxscore, array[I])
as you go to pick off the biggest one.
I am also working on this subject and after looking at your code i have made a few changes but now im stuck with a compile, have a look and let me know
Getting the error on line 35
#include <iostream>
#include <cassert>
#include <cstddef>
#include <cstdlib>
// 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 *arrayName, arraySize = 0 ;
arrayName = newint [arraySize] ;
int studentScores = 0 , maxScore = 0 ;
int i = 0;
int z=0;
cout << "Enter the size of the array ";
cin>>arraySize;
z=arraySize;
assert ( arraySize > 0 ); // checking for a number greater than zero
cout<<" You entered and array of size "<<arraySize<<endl; // feedback
// requesting input for student scores
for ( int j=0; j <= z; j++)
{
cout<<" Enter the scores for each student "<<endl;
cin >> arrayName[z];
for ( int b= 0; b <= arraySize[z]; b++)
{
studentScores = arrayName[i];
cout<< " Student scores are as follows ; "<<studentScores<<endl;
}
if (studentScores > maxScore)
{
maxScore = studentScores;
cout<< " Maximum student score is "<<maxScore<<endl;
}
}
delete [] arrayName;
return 0;
}
Okay i think i have got it to work, maybe mine is wrong but its super simple, sorry updated it more just to add features etc, im gonna use this one for my assignment answer that i have displayed below
#include <iostream>
#include <cassert>
#include <cstddef>
#include <cstdlib>
usingnamespace std;
int main()
{
int arraySize=0;
int score[arraySize];
int maxScore=0;
int countdown=0;
cout << "Enter array size: ";
cin >> arraySize;
countdown=arraySize;
cout<< "The size of the array that you entered is: " << arraySize << endl;
for (int i=1; i <= arraySize; i++)
{
cout<< "You can enter: " <<countdown<< " more scores till it presents the maximum"<< endl;
countdown=countdown-1;
cout << "Enter Score: ";
cin>> score[i];
//cout << score[i]<< endl;
if (score[i] > maxScore)
{
maxScore = score[i];
//cout<< " Maximum student score is "<<maxScore<<endl;
}
}
cout<< " Maximum student score is "<<maxScore<<endl;
delete [] score;
return 0;
}
#include <iostream>
#include <cassert>
#include <cstddef>
#include <cstdlib>
usingnamespace std;
int main()
{ int arraySize=0;
int *score; // Pointer for dynamically allocated array
int maxScore=0;
int countdown=0;
cout << "Enter array size: ";
cin >> arraySize;
score = newint[arraySize]; // Dynamically allocate array
countdown=arraySize;
cout<< "The size of the array that you entered is: " << arraySize << endl;
for (int i=0; i < arraySize; i++) // Use correct bounds for array
{ cout<< "You can enter: " <<countdown<< " more scores till it presents the maximum"<< endl;
countdown=countdown-1;
cout << "Enter Score: ";
cin>> score[i];
if (score[i] > maxScore)
{ maxScore = score[i];
}
}
cout<< " Maximum student score is "<<maxScore<<endl;
delete [] score;
return 0;
}