//
// Inputs:user
//
// Returns:Min, Max, Low to High Sort,
// High to Low Sort, Splitted array
//
#include %26lt;iostream%26gt;
#include %26lt;algorithm%26gt; // for C++ sort function
using namespace std;
const int SIZE = 10000;
void GetInputArray (int%26amp; userSize, int originalArray []); //Gets input array from user
void FindMinMax (int userSize, int%26amp; min, int%26amp; max, int originalArray []); //Determines min and max
void Sort (int userSize, int originalArray [], int lowSortedArray [], int highSortedArray []); //Sorts the arrays (H to L and L to H)
void SplitArrayProcessing (int userSize, int originalArray [], int lowSplitArray [], int highSplitArray []); //splits the array in half
int main ()
    {
     int originalArrayMain [SIZE];
     int lowSortedArrayMain [SIZE];
     int highSortedArrayMain [SIZE];
     int lowSplitArrayMain [SIZE];
     int highSplitArrayMain [SIZE];
     int userSizeMain;
     int minimum, maximum;
     
     GetInputArray (userSizeMain, originalArrayMain);
     
     if (userSizeMain %26gt; 0)
         {
          FindMinMax (userSizeMain, minimum, maximum, originalArrayMain);
          Sort (userSizeMain, originalArrayMain, lowSortedArrayMain, highSortedArrayMain);
          SplitArrayProcessing (userSizeMain, originalArrayMain, lowSplitArrayMain, highSplitArrayMain);
          
          //outputs
          cout %26lt;%26lt; "Min: " %26lt;%26lt; minimum %26lt;%26lt; endl;  //minimum number in array
          cout %26lt;%26lt; "Max: " %26lt;%26lt; maximum %26lt;%26lt; endl;  //maximum number in array
          
          cout %26lt;%26lt; "Low to High Sort: ";   //low to high sorted array
          for (int i = 0; i %26lt;= userSizeMain - 1; i++)
              {
               cout %26lt;%26lt; lowSortedArrayMain [i] %26lt;%26lt; " ";
              }
              
              cout %26lt;%26lt; endl %26lt;%26lt; "High to Low Sort: ";   //high to low sorted array
              for (int i = 0; i %26lt;= userSizeMain - 1; i++)
                  {
                   cout %26lt;%26lt; highSortedArrayMain [i] %26lt;%26lt; " ";
                  }
                  cout %26lt;%26lt; endl;
                 }
                 cout %26lt;%26lt; "First half split of array: ";  //first half split of array
                 for (int i = 0; i %26lt;= ((userSizeMain/2) - 1); i++)
                     {
                      cout %26lt;%26lt; lowSplitArrayMain [i] %26lt;%26lt; " ";
                      
                     }
                     cout %26lt;%26lt; endl;
                     cout %26lt;%26lt; "Second half split of array: ";  // second half split of array
                     for (int i = 0; i %26lt; ((userSizeMain+1)/2); i++)
                         {
                          cout %26lt;%26lt; highSplitArrayMain [i] %26lt;%26lt; " ";
                         }
                         cout %26lt;%26lt; endl %26lt;%26lt; endl;
                         cout %26lt;%26lt; endl;
                         system("pause");
                         return 0;
                    };
                    void GetInputArray (int%26amp; userSize, int originalArray []) //Gets input array from user
                        {
                         cout %26lt;%26lt; "How many integers do you want to put into the array?: " %26lt;%26lt; endl;
                         cin %26gt;%26gt; userSize;
                         cout %26lt;%26lt; endl;
                         
                         if (userSize %26gt; 0)
                             {
                              cout %26lt;%26lt; "Enter " %26lt;%26lt; userSize %26lt;%26lt; " integer(s) into the array (e.g. 5 88 4 etc):" %26lt;%26lt; endl;
                              for (int i = 0; i %26lt;= userSize - 1; i++)
                                  {
                                   cin %26gt;%26gt; originalArray [i];
                                  }
                                  cout %26lt;%26lt; endl;
                                 }
                                 else
                                     {
                                      cout %26lt;%26lt; "Invalid Entry!" %26lt;%26lt; endl;
                                     }
                                }
                                void FindMinMax (int userSize, int%26amp; min, int%26amp; max, int originalArray [])
                                    {
                                     //find the mimimum number in the array
                                     min = originalArray [0];
                                     for (int i = 0; i %26lt;= userSize - 1; i++)
                                         {
                                          if (originalArray [i] %26lt;= min)
                                              {
                                               min = originalArray [i];
                                              }
                                             }
                                             //find the maximum number in the array
                                             max = originalArray [0];
                                             for (int i = 0; i %26lt;= userSize - 1; i++)
                                                 {
                                                  if (originalArray [i] %26gt;= max)
                                                      {
                                                       max = originalArray [i];
                                                      }
                                                     }
                                                }
                                                void Sort (int userSize, int originalArray [], int lowSortedArray [], int highSortedArray [])
                                                    {
                                                     int j = (userSize - 1);
                                                     
                                                     //copy contents of originalArray into lowSortedArray
                                                     for (int i = 0; i %26lt;= userSize - 1; i++)
                                                         {
                                                          lowSortedArray [i] = originalArray [i];
                                                         }
                                                         
                                                         //sorts the lowSortedArray from low to high
                                                         sort (lowSortedArray, lowSortedArray + userSize);
                                                         //reverse copies the lowSortedArray to the highSortedArray (high to low sorted array)
                                                         for (int i = 0; i %26lt;= userSize - 1; i++)
                                                         { 
                                                          if (j %26gt;= 0)
                                                              {
                                                               highSortedArray [j] = lowSortedArray [i];
                                                               j--;
                                                              }
                                                             }
                                                        }
                                                        void SplitArrayProcessing (int userSize, int originalArray [], int lowSplitArray [], int highSplitArray [])
                                                            {
                                                             int j = 0;
                                                             //splits the array in half
                                                             for (int i = 0; i %26lt;= userSize - 1; i++)
                                                                 {
                                                                  if (i %26lt; (userSize/2))
                                                                      {
                                                                       lowSplitArray [i] = originalArray [i];
                                                                      }
                                                                      else
                                                                          {
                                                                           highSplitArray [j] = originalArray [i];
                                                                           j++;
                                                                          }
                                                                         }
                                                                    }
C programming wrote a prog to find out max and min between integer array?
#include%26lt;stdio.h%26gt;
#include%26lt;conio.h%26gt;
#define n 10;
int n[10];
void main()
{
     int i;
     //to store the maximum and minimum numbers
     int max,min;
    (for i=0;i%26lt;n;i++)
     {
          //sorry, i cannot remember the printf scanf commands. ope u know them !!!
          printf("Enter the"%d"th value",i);
          scanf a[i];
     }
     //assigning the max and min to the 1st pos.of the array.
     max=a[0];
     min=a[1];
     (for i=0;i%26lt;n;i++)
      {
            if(a[i]%26gt;max)
            {
                    max=a[i];
            }
            
            if(a[i]%26lt;min)
            {
                    min=a[i];
             }
     }
     printf("The max is %d",max);
     printf("Min is %d",min);
    getch();
}
//sorry the printf scanf command may be wrong in syntax. please refer them. 
//but the logic is the BEST!!!
GOOD LUCK!!!!!
Subscribe to:
Post Comments (Atom)
 
No comments:
Post a Comment