Array Values

Hello All,
I have tried searching through the forums for this with no luck.

I need to find the highest and lowest values inside an array without changing the way they are sorted. I also need to catch the subscripts the two numbers are stored in.

 
float data[50] // Array Name 



Thanks
Say that the largest element is stored at index m = 0
Now have a for loop i = 1 to 50 and if data[i] > data[m], set m to i.
I cant seem to get this to work

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
#include <iostream>
using namespace std;
void returnValues(float [], int, int);
int main ()
{
    int sub;
    float strg [5] = {3.5, 8, 4, 6, 1};
    returnValues(strg, 5, sub);
    cout << "Subscript " << sub <<endl;
    cout<< "Value " <<strg [ sub] <<endl;
    
    system("pause");
    return 0;
}

void returnValues(float array [], int size, int index)
{
     int count = 0, i = 0 ;
     
     float storage;
     for(count = 1; count < size; count ++)
        {
               if( array [i] > array [ count ])
                 {
                       
                       array [i] = array [count];
                       index = count;
                 }
        }
}
:

It always displays :


Subscript 2
Value 4
Something like this?:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int lower = 100;
int higher = 0;

for( int i = 0; i < size; ++i )
{
	if( myArray[ i ] < lower )
	{
		//new lowest ammount
		lower = array[ i ];

		//new index
		indexLower = i;
	}

	if( myArray[ i ] > higher )
	{
		//new highest ammount
		higher = array[ i ];

		//new index
		indexHigher = i;
	}
}
Last edited on
Here is a basic example of my problem.
For some reason, the indexes, and the values are incorrect. Doesnt display the highest, or lowest value. But the values it displays correspond to the index it displays. There may be some small errors in this code because i quickly typed it out to give an example.


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
//Function Prototype
void returnHighestLowest(float [], int, int, int, int);

//Function Call
int main ()
{
     //data[50] has 50 float values in it
     int i = 0, highIndex, lowIndex;
     float data[50];
     returnHighestLowest(data, 50, );
     cout<<"Highest Index "<<highIndex <<endl;
     cout<<"Highest Value " << data[highIndex]<<endl<<endl;
     cout<<"Lowest Index "<<lowIndex <<endl;
     cout<<"Lowset Value " <<index[lowIndex];






//Function
void returnHigestLowest(float array [], int size, int highIndex, int lowIndex )
{
     int count = 0, indexLower, indexHigher;
     float highest, lowest;
     for(count = 0; count < size; count ++)
         {
               if ( array [count] < array [count + 1])
                  {
                         highest = array [count +1];
                         indexHigher = count +1;
                  }
               else if(array [count] > array [count +1])
                        {
                               lowest = array [ count +1];
                               indexLower = count +1;
                         }
          }
}
Pass by reference, so what ever changes are made within a function, they are the same in the function that called it:

void returnHigestLowest(float array [], int size, int &highIndex, int &lowIndex )
Last edited on
Well I really just need to figure out how to find the highest and lowest values in an array and save their subscripts as well.

I tried it here, but for some reason the values turn out the same :/

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
#include <iostream>
#include <fstream>
using namespace std;
void createArray(float []);
void displayHighestLowest(float [], float, float, int, int); 
int main ()
{
int count = 0, indexLower = 0, indexHigher = 0;
float lower;
float higher;
float MyArray[6];
createArray(MyArray);
displayHighestLowest(MyArray, lower, higher, indexLower, indexHigher);

cout<<"Lowest Subscript " << indexLower<<endl;
cout<<"Lowest Value " << MyArray [ indexLower]<<endl<<endl;
cout<<"Highest Subscript " << indexHigher<<endl;
cout<<"Highest Value " <<MyArray [ indexHigher]<<endl;

system("pause");

return 0;

}

void createArray(float arry[])
{
     ifstream array;
     int count = 0;
     array.open("farty.txt");
     for(count = 0; count < 6; count ++)
        {
             array >> arry[count];
        }
}

void displayHighestLowest(float ary [], float lowest, float highest, int indexLow, int indexHigh)
{
     int i = 0;
     for(i = 0; i < 6; i ++)
        {
             if(ary[i] > ary [i + 1])
               {
                    i = i + 1;
                    indexLow = i;
               }
                               
               if(ary[i + 1] > ary [i])
               {
                    i = i + 1;
                    indexHigh = i;
               }  

        }
     lowest = ary [indexLow];
     highest = ary [indexHigh];
return ;
}  
Lynx told you how to make it right. You could also just return what you need (you'll have to write two functions for min and max then).
Ok, I tried different variations of returning, and pass by reference, but it always displays values of e notation. How do i fix 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <fstream>
using namespace std;

void welcome();

string getData ();

void readData (string &, float []) ;

void returnHighest(float [], int, int&);

void returnLowest(float [], int, int&);

void calculations (float [], float &, float &, int)  ;

void sortArray(float [], int, float&) ;

void printResults () ;

int main ()
{
    
    int count = 0, indexL, indexH;
    float data[50], average, median, total;
    string name;
    welcome ();
    name = getData();
    readData(name, data);
    returnHighest(data, 50, indexH);
    returnLowest(data, 50, indexL);
    calculations (data, average, total, 50);
    sortArray(data, 50, median);
    ofstream results;
    results.open("results.txt");
    results << "Day " << indexL + 1 <<" Had The Lowest Sales With $ " <<data[indexL]<<endl<<endl;
    results << "Day " << indexH + 1 <<" Had The Highest Sales With $ " <<data[indexH]<<endl<<endl;
    results << "The Average Of All 50 Days Is $ " << average<<endl<<endl;
    results << "The Median Of All 50 Items Is $ " <<median<<endl<<endl;
    results << "The Total Of All 50 Days Is $ " << total <<endl <<endl;
    results << "The Items In Ascending Order "<<endl;
    for(count = 0; count < 50; count ++)
        results << data[count]<<endl;
    results.close();        
    cout<<"results.txt Has Been Created Containing:"<<endl;
    cout<<"- The Day With The Lowest Sales"<<endl;
    cout<<"- The Day With The Highest Sales"<<endl;
    cout<<"- The Average Of All Data"<<endl;
    cout<<"- The Median Of The Data"<<endl;
    cout<<"- The Total Of All The Data"<<endl;
    cout<<"* The Data Has Also Been Sorted Into Ascending Order *"<<endl<<endl;         
    
    system("pause");
    return 0;
    
}

void welcome ()
{
     cout<<"========================================="<<endl;
     cout<<"== Welcome To The Sales Amount Program =="<<endl;
     cout<<"========================================="<<endl<<endl;
     
     cout<<"This Program Lets You :"<<endl;
     cout<<"(*)"<<endl;
     cout<<"    - Create A File."<<endl<<endl;
     cout<<"    - Input The Sales For 50 Days Into That File."<<endl;
     cout<<"(*)"<<endl<<endl;
     cout<<"Then The Program Calculates :"<<endl;
     cout<<"(**)"<<endl;
     cout<<"    - The Day With The Lowest/Highest Sales Amount."<<endl<<endl;
     cout<<"    - The Total Sales For All Days."<<endl<<endl;
     cout<<"    - The Average Sales For All Days."<<endl<<endl;
     cout<<"    - The Contents Sorted In Ascending Order."<<endl<<endl;
     cout<<"    - The Median Value Of The Contents."<<endl;
     cout<<"(**)"<<endl<<endl;
     return ;
}
string getData ()
{
     float contents [ 50 ], none[50];
     int count = 0;
     string UserFileName, EndFileName, CompleteFile;
     cout<<"Please Name The File To Store The Sales In : ";
     getline(cin,UserFileName);
     EndFileName = ".txt";
     CompleteFile = UserFileName + EndFileName;
     ofstream daySales;
     daySales.open(CompleteFile.c_str());
     for(count = 0; count <50; count ++)
    {
         cout << " What Are The Sales For Day  " << count+1 << "?  $" ;
         cin >> contents[count];
         daySales << contents[count]<<endl;
    }
         daySales.close ();
         return CompleteFile;
}

void readData (string & name, float numb [])
{
     int count = 0, counterMax = 60;
     ifstream openFile;
     openFile.open(name.c_str());
     while(count < counterMax && openFile >> numb [count])
     {
          count ++;
     }
     openFile.close();
}


void returnHighest(float MyArray [], int size, int & indexH)
{
      int count = 0;
      float value = MyArray [ 0 ];
      for(count = 1; count < 50; count ++)
         {
              if(MyArray[count] > value)
                 value = MyArray[count];
         }
      indexH = count;
}

void returnLowest(float MyArray [], int size, int & indexL)
{
     int count = 0;
     float othervalue = MyArray[0];
     for(count = 1; count < size; count ++)
     {
          if(MyArray[count] < othervalue)
             othervalue = MyArray[count];
     }
     indexL = count;
}

void calculations (float ary [], float& average, float& total, int size)
{
      int count = 0;
      for(count = 0; count < size; count ++)
         {
              total += ary [ count ];
         }
         average = total / size;
}

//Sorting List Into Ascending Order
  void sortArray(float array [], int size, float& median)
  {
       bool swap;
       float temp;
       
       do
       {
             swap = false;
             for (int count = 0; count < (size - 1); count ++)
                 {
                      if(array[count] > array [count +1])
                        {
                             temp = array[count];
                             array[count] = array[count + 1];
                             array[count+1] = temp;
                             swap = true;
                  }
             }
             
     }
     while (swap);

     median = (array[25] + array[26])/2;

} 

See your loops:
1
2
3
4
for(count = 1; count < 50; count ++) {
    if(MyArray[count] > value)
        value = MyArray[count];
}
What is the value of count at the end of it. Could it be something other than 50? Why are you saving the highest value if index is what you need?
Well, I just decided to put it in main and it seems to work just fine. Thanks for all the help
Well, you're going about it wrong.

1
2
3
4
5
6
for(count = 1; count < size; count ++)
     {
          if(MyArray[count] < othervalue)
             othervalue = MyArray[count];
     }
     indexL = count;


count should = 0! 0 is the first index. When you declare an array, it's: array[ n - 1 ]. An array of 50 is 0 through 49

Also, you want to catch the index within the if statement to get the correct one. What you are doing is, saving the lowest and then saving the count at the end of the loop instead of when the lowest is caught.

1
2
3
4
5
6
7
8
for(count = 0; count < size; count ++)
     {
          if(MyArray[count] < othervalue)
          {
               othervalue = MyArray[count];
               indexL = count;
          }
     }
Last edited on
This works fine, passing by reference and showing the values have changed back in the main() function.

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
//function prototypes
int randomRange( int _Min, int _Max );
void getHighestAndIndex( int rN[], const int s, int &h, int &in );


int main()
{
	srand( unsigned( time( NULL ) ) );

	const int size = 10;

	int highest = 0;
	int index = 0;

	//array = NULL
	int randomNums[ size ] = { '\0' };

	for( int i = 0; i < size; ++i )
		randomNums[ i ] = randomRange( 5, 20 );

	//print values - indexes
	for( int i = 0; i < size; ++i )
		std::cout << "index " << i << ": " << randomNums[ i ] << '\n';

	getHighestAndIndex( randomNums, size, highest, index );

	std::cout <<"\n\nhighest value: " << highest;
	std::cout << "\nindex of highest value: " << index << "\n\n";

	return 0;
}


void getHighestAndIndex( int rN[], const int s, int &h, int &in )
{
	for( int i = 0; i < s; ++i )
	{
		//if array index value greater or equal that highest value
		//I used >= because if two numbers are the same and highest, it will only catch the first one
		//it depends on which one you want to catch. First or last of that number/index
		if( rN[ i ] >= h )
		{
			//store new high value
			h = rN[ i ];

			//catch the index
			in = i;
		}
	}
}


int randomRange( int _Min, int _Max )
{
	//add 1 to the max, so it's included in the result.
	_Max += 1;

	int x = _Min + rand() % ( _Max - _Min );

	return x;
}
Topic archived. No new replies allowed.