Please Help me with min/max values Arrays!

I have struggled with finding the highest value and lowest value in the array. Here is the code.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// This program will read in a group of test scores( positive integers from 1 to 100)
// from the keyboard and then calculates and outputs  the average score
// as well as the highest and lowest score. There will be a maximum of 100 scores.

#include <iostream>
using namespace std;

typedef int GradeType[100];  // declares a new data type:
                             // an integer array of 100 elements


float findAverage (const GradeType, int);  // finds average of all grades
int   findHighest (const GradeType, int);  // finds highest of all grades
int   findLowest  (const GradeType, int);  // finds lowest of all grades

int main()

{
    GradeType  grades;					   // the array holding the grades.
    int  numberOfGrades;				   // the number of grades read.
    int pos;							   // index to the array.

	float avgOfGrades;					   // contains the average of the grades.
	int highestGrade;					   // contains the highest grade.
	int lowestGrade;					   // contains the lowest grade.

	// Read in the values into the array

	pos = 0;
	cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;
	cin  >> grades[pos];

	while (grades[pos] != -99)
	{
        pos ++;
        // Fill in the code to read the grades
       cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;
       cin >> grades[pos];
	}

	numberOfGrades = pos; // Fill blank with appropriate identifier

	// call to the function to find average

	avgOfGrades = findAverage(grades, numberOfGrades);

	cout << endl << "The average of all the grades is " << avgOfGrades << endl;


	//  Fill in the call to the function that calculates highest grade
    highestGrade = findHighest(grades, numberOfGrades);

	cout << endl << "The highest grade is " << highestGrade << endl;


	// Fill in the call to the function that calculates lowest grade
    lowestGrade = findLowest(grades,numberOfGrades);

    // Fill in code to write the lowest to the screen
    cout << "The lowest grade is " << lowestGrade << endl;
	return 0;
}

//****************************************************************************
//                                 findAverage
//
// task:          This function receives an array of integers and its size.
//                It finds and returns the average of the numbers in the array
// data in:       array of floating point numbers
// data returned: avarage of the numbers in the array
//
//****************************************************************************

float findAverage (const GradeType  array, int size)

{

	float sum = 0;   // holds the sum of all the numbers

    for (int pos = 0; pos < size; pos++)


	   sum = sum + array[pos];

    return (sum / size);  //returns the average

}

//****************************************************************************
//                                 findHighest
//
// task:          This function receives an array of integers and its size.
//                It finds and returns the highest value of the numbers in
//                the array
// data in:       array of floating point numbers
// data returned: highest value of the numbers in the array
//
//****************************************************************************

int   findHighest (const GradeType array, int size)

{

   // Fill in the code for this function   int highestGrade;
   for (int count =0; count <size; count++)
    cout << array[count] << " ";
   cout <<endl;

}

//****************************************************************************
//                                 findLowest
//
// task:          This function receives an array of integers and its size.
//                It finds and returns the lowest value of the numbers in
//                the array
// data in:       array of floating point numbers
// data returned: lowest value of the numbers in the array
//
//****************************************************************************

int   findLowest  (const GradeType array, int size)

{
   // Fill in the code for this function

}


For the highest I have tried a bubble sort and the selection sort with no luck ):

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
int   findHighest (const GradeType array, int size)
{
   // Fill in the code for this function   int highestGrade;
   bool swap;
   int highestGrade;

   do
   {
       swap = false;

       for (int pos = 0; pos < (size - 1); pos++)
       {

            if (array[pos] < array[pos + 1])
            {
                highestGrade = array[pos];
                array[pos] = array[pos + 1];
                array[pos +1] = highestGrade;
                swap = true;
            }


       }

   }while (swap);


And this.

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
int   findHighest (const GradeType array, int size)

{

   // Fill in the code for this function   int highestGrade;
   int startScan, minIndex, minValue;

   for (startScan = 0;startScan < (size - 1); startScan++)
   {
       minIndex = startScan;
       minValue =array[startScan];
       for (int index= startScan +1; index < size; index++)
       {

            if (array[index] < minValue)
            {
                minValue = array[index];
                minIndex = index;
            }


       }
       array[minIndex] = array[startScan];
       array[startScan] = minValue;

   }



}


I have tried switching the ints with grades pos and just about everything. I really don't understand arrays but I'm getting the hang of them kinda... Every time I have replaced the variables I get a error that says assignment of read-only location '*(array +((sizetype) (((unsigned int)minIndex or pos or GradeType. I'm at a loss, am I plugging in the wrong things? Or should not use these sorters?
Last edited on
- Lines 29 - 41 must be modified like this:

1
2
3
4
5
6
7
8
9
10
        pos = 0;
	cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;

	while (grades[pos] != -99)
	{
             cin >> grades[pos];
             ++pos;
	}

	numberOfGrades = pos ;  // Fill blank with appropriate identifier 


- Try to understand then adapt this code for your needs.
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
# include <iostream>

using namespace std;

int main()
{	
	int n;
	cout << "\nHow many random numbers(<= 0 -> stop): ";
	cin >> n;
        if(n <=0) return 0;

	int max = 100;				// 0 - max will be the number's range
	
	int *array = new int[ n ];		// Dynamic memory allocation.
	
	srand((unsigned int)time		// Pseudo-random initialized using actual time passed as seed.
	((time_t *)NULL));

	for(int i = 0; i < n; ++i)
		array[i] = rand() % (max + 1);	// Populating array (n random integers in the range 0 - max).
		
	cout << "\nInitial array(" << n << " random integers):\n";
	for(int i = 0; i < n; ++i)
		cout << array[i] << ' ';
	cout << '\n';
	
	// Here starts your "interest" ;)
	
	int val1 = - 1;				// These values have been selected because
	int val2 = 101;				// your numbers are in the range 0 - 100
	
	for(int i = 0; i < n; ++i)		// Finding the Highest value
		if(array[i] > val1)
			val1 = array[i];
	
	for(int i = 0; i < n; ++i)		// and the Lowest one
		if(array[i] < val2)
			val2 = array[i];
	
	cout << "\nLowest value: " << val2 << '\n';
	cout << "Highest value: " << val1 << '\n';
	
        delete[] array;                          // Memory allocation release
	return 0;
}

Hope this helps.
EDIT: If necessary (your compiler gives some errors) add # include <cstdlib) and # include <ctime>
Sample of output:

How many random numbers(<= 0 -> stop): 30

Initial array(30 random integers):
50 66 16 13 57 8 27 42 95 40 94 55 82 91 67 17 55 90 83 4 87 65 21 36 87 15 77 94 21 73

Lowest value: 4
Highest value: 95
Last edited on
Thank you so much for your time. I was sticking everything in the function itself!! D: I know now! Thank you so much and ill study that code and master it!
You're welcome!
BTW, the lines (27 - 41 in my code) can be simplified as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Here starts your "interest" ;)
	
	int highest = array[0];                 // The highest and the lowest values
	int lowest = array[0];                  // are in the beginning the same ones ;)
	
	for(int i = 0; i < n; ++i)
	{
		if(array[i] > highest)	        // Finding the Highest value
			highest = array[i];
			
		else if(array[i] < lowest)	// and the Lowest one
			lowest = array[i];				
	}

        cout << "\nLowest value: " << lowest << '\n';
	cout << "Highest value: " << highest << '\n';
Last edited on
@learningeveryday

When you'll consider all is OK with this thread, please mark it as Solved.
Topic archived. No new replies allowed.