Dynamic Array and For Loop not working

Hello Guys

My dynamic array is not picking up the number of students entered and working out maximum student score. Please advise how to fix it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#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.

using namespace std;

int main()
{
    int *arrayName, arraySize = 0 ;
    arrayName = new int [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;
}
   

Do you realize that:
1
2
3
4
5
6
int arraySize = 0;
int * arrayName = new int [arraySize];

// is same as
int arraySize = 0;
int * arrayName = new int [ 0 ];

How many elements are in 0 element array?

If your array would have arraySize elements, you would still cause an out-of-range error with lines 29 and 31.


What is the purpose of your two loops?
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

Please take a look.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

#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.

using namespace std;

int main()
{
    int *arrayName, arraySize = 0 ;
    arrayName = new int [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.

?

Last edited on
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 
#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.

using namespace std;

int main()
{
    int *arrayName, arraySize = 0 ;
    arrayName = new int [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;
}
Last edited on
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#include <iostream>
#include <cassert>
#include <cstddef>
#include <cstdlib>

using namespace 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;
}

Last edited on
Line 18: arraySize is 0.

Line 19: You're allocating a zero length array.

Line 27: The first element of your array is [0], not 1.

Line 44: You can't delete an array that was never allocated with new.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cassert>
#include <cstddef>
#include <cstdlib>

using namespace 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 = new int[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;
}

Last edited on
Thanks for the help, rather new to this
Topic archived. No new replies allowed.