3 Replies - 3275 Views - Last Post: 21 February 2010 - 09:53 PM Rate Topic: -----

#1 bossardnate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-February 10

can't find the second highest or lowest value in an array

Posted 20 February 2010 - 10:41 PM

My issue is with finding the second highest and second lowest values in an array. The crux of my dilemma is that I am trying to minimize the amount of variables I am using. I have been able to find the highest and lowest values, but inside the same for loop I can't find any way to get the second values. The specifics are that I am to read from a file containing the text of Alice in Wonderland and find the letters that occur the the most, second to most, least and second to least. As I mentioned before the most and least are pretty straightforward , but the second to least and second to most are definitely hard for me to find. I do not want exact syntax, rather some guidance as to whether I should be working in the same for loop that I used to find the two aforementioned values or if I should have an entirely new for loop for each new value. I have tried everything I can think of but I have to keep the variables to an absolute minimum. I've already gone over the recommended amount so now I am completely at a loss. This is what I have so far. Thanks in advance for any help.
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    const int firstLowercase='a';
    const int lastLowercase='z';
    int a[lastLowercase-firstLowercase+1]={0};
     int index=0;
     int index2=0;
    char letter;
   
    ifstream inputFile;
     inputFile.open("alice-in-wonderland.txt");
     
     if (!inputFile)
      {cout<<"error opening file \n";
      system("PAUSE");
      return EXIT_SUCCESS;}
    
   
    while (inputFile>>letter)
    
    {          
      if(letter>=firstLowercase && letter<=lastLowercase)
         a [letter-firstLowercase]++ ; 
    }
    
       for (int i=0;i<=(lastLowercase-firstLowercase);i++)
         { 
            if (a[index]<a[i])
            index=i;          
            
         }
            cout<<"The letter that occurs most happens this many times "<<a[index];
            cout<<endl;
            cout<<"that letter is "<<char(index+firstLowercase); 
            cout<<endl;
            
          index=0; 
          
        for (int i=0;i<=(lastLowercase-firstLowercase);i++)
          { 
             if (a[index]>=a[i])
             index=i;
          }
           // cout<<char('i+firstLowercase')<<" ";
         cout<<"The letter that occurs least happens this many times "<<a[index];
         cout<<endl;
         cout<<"that letter is "<<char(index+firstLowercase);
         cout<<endl; 
           
          //cout<<least<<" "<<most;
        
      /*      
          
          */          
            
  
    system("PAUSE");
    return EXIT_SUCCESS;
    
    
    
    
}


Is This A Good Question/Topic? 0
  • +

Replies To: can't find the second highest or lowest value in an array

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,901
  • Joined: 10-May 07

Re: can't find the second highest or lowest value in an array

Posted 20 February 2010 - 10:49 PM

I would make another array, & assign all values other than the highest. The 2nd run through will find the highest, which is the 2nd highest overall value. Loop through the array until your 2nd array is filled, thus ordering your array by value.
Was This Post Helpful? 0
  • +
  • -

#3 Paul-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: can't find the second highest or lowest value in an array

Posted 21 February 2010 - 09:42 PM

If you are allowed to use 2 variables, index and index2, then you can modify the tests in the for loops to keep track of both the first and second max or min.

A second solution would be to do a partial sort, where you move the max (or min) element to the front of the array. Then you do a second for loop and look for the max (or min) beginning from index 1 instead of 0.
Was This Post Helpful? 0
  • +
  • -

#4 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: can't find the second highest or lowest value in an array

Posted 21 February 2010 - 09:53 PM

You need to move the highest to the second highest before you put the new highest value into the highest value. So let's say you have variables highest, second-highest and a value n. If n is greater than highest, then second_highest equals highest and highest equals n. The same principle can of course be applied to the lowest value, such that if n < lowest then second_lowest equals lowest and lowest becomes n.

Hope that helps you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1