Welcome to Dream.In.Code
Become a C++ Expert!

Join 136,922 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,775 people online right now. Registration is fast and FREE... Join Now!




Minimizing Code

 
Reply to this topicStart new topic

Minimizing Code

cooplis
31 Mar, 2008 - 07:21 AM
Post #1

D.I.C Head
**

Joined: 11 Sep, 2007
Posts: 77


My Contributions
I am working with a lot of differenct codes in this program. I need to combine some of it so that I call by reference and have fewer little things working in each report. Where do I start?

Help and Thanks,

Cooplis:)

CODE

#include <iomanip>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <conio.h>

void high_low(double, double);
double arr1 = 20;
double arr3 = 8;    
void bubble_srt(double stock3[], double n);
void bubble_srt(double stock1[], double n);

using namespace std;

int main()
{
    int index;
    int size;
    double Avg1;
    double Avg2;
  
    
    
    double stock1[20] = {34.25,40.50,36.50,40.00,
                                30.25,30.25,35.50,36.00,
                                34.25,37.00,34.00,35.00,
                                36.25,34.25,40.50,41.50,
                                41.50,40.00,36.50,34.50};
   double stock2[20]=  {40.25,38.50,34.50,33.50,
                                30.50,29.75,37.50,36.00,
                                34.75,38.00,34.25,37.00,
                                34.25,37.50,34.50,38.50,
                                37.50,37.25,38.25,37.50};  
    double stock3[8]= {100.41, 90.45, 99.30, 102.99,
                      98.54, 95.30, 92.32, 110.88};                                                      

//Report 1 - show highest and lowest numbers in array    
{
cout << "                   REPORT 1                 \n\n";
    sort(stock1, stock1+20);
    sort(stock2, stock2+20);
    {
    for (int i=0; i<1; i++) {
       cout << "The highest number in Stock 1 is " << stock1[19] <<" \n";
       cout << "The lowest number in Stock 1 is " << stock1[0]  <<" \n\n\n";
       cout << "The highest number in Stock 2 is " << stock2[19] <<" \n";
       cout << "The lowest number in Stock 2 is " << stock2[0]  <<" \n\n\n";
}  
system("pause\n");    
//Report 2 - The average of each set of 20 stocks and the number of days,
//on which each stock price exceeded its average price.
{
cout << "\n\n                 REPORT 2                 \n\n";  
     int count = 0;
     int  count1=0;
   {
    
    int j = 20;
      for (j = 0; j < 20; j++)
           if(stock1[j] > stock2[j]) ++count;
          
      int k = 20;
      for (k = 0; k < 20; k++)
           if(stock2[k] < stock1[k]) ++count1;
        }
        cout << "Number of days Stock1 exceeded Stock2: "<<count;
        cout << "\n";
        cout << "Number of days Stock2 exceeded Stock1: "<<count1;    
        cout << "\n\n";          
       cout << "Number in Array: "<< arr1;
           cout << "\n\n";
           {
       double sum = 0;
       double average = 0;
     for (int i = 0; i < 20; ++i)
        sum+=stock1[i];
    average = sum/arr1;
    cout<<"Stock1 Average:"<<average;
    cout << "\n\n";  
}
}
    double sum = 0;
    double average1 = 0;
     for (int i = 0; i < 20; ++i)
        sum+=stock2[i];
    average1 = sum/arr1;
    cout<<"Stock2 Average:"<<average1;
    cout << "\n\n";
          }  
          }
system("pause");
        


//Report 3 - Using the functions from Reports 1 and 2, find the lowest
//and highest price in the stock3 array, and find the average of the stocks in
//the following array of 8 prices above.
//Sort the stock3 array and display in ascending order with this report


cout << "\n\n                  REPORT 3        \n\n";
{
     int i;
    
    sort(stock3, stock3+8);
    for (int i=0; i<1; i++) {
        cout << "The highest number in Stock 3 is " << stock3[7] <<" \n";
       cout << "The lowest number in Stock 3 is " << stock3[0]  <<" \n\n\n";
       }
      
    double sum = 0;
    double average3 = 0;
     for (int i = 0; i < 8; ++i)
        sum+=stock3[i];
    average3 = sum/arr3;
    cout<<"Stock3 Average:"<<average3;
    cout << "\n\n";

void bubble_srt(double stock3, double arr1);        // Sort the array
    
     cout<<("Stock 3 in ascending order:\n");     // Show results after the sort
     for(i = 0; i < 8; i++)
      cout << stock3[i]<< "\n";
      cout<<("\n");  
}
system("pause");


//Report 4 - Stock 1 values and how much each stock price differs from the
// average.
//
cout << "\n\n                  REPORT 4        \n\n";
              {
       double sum = 0;
       double average = 0;
     for (int i = 0; i < 20; ++i)
        sum+=stock1[i];
    average = sum/arr1;
    cout<<"Stock1 Average:"<<average;
    cout << "\n\n";  
    
    void bubble_srt(double stock1, double arr1);        // Sort the array
     {
         int i;
     cout<<("Difference of Stock 1 in ascending order:\n");     // Show results after the sort
     for(i = 0; i < 20; i++)
     cout << stock1[i] - average << "\n";
     cout<<("\n");  
      
      
}
}

system("PAUSE");  
     }
                 // Pause the screen


User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Minimizing Code
31 Mar, 2008 - 08:33 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well first you have two declarations for bubble_srt when you should only have one (since they even take the same parameters). Arrays passed to this function are also passed by reference. Problem lies in that you have said in the declaration that the function takes an array parameter, but down in the actual function for bubble_srt you have it taking just a double data type as the first parameter. Remember these must match.

Some of your couts print "\n\n". You realize you could just put these on the end of the other couts and save yourself a line for each one.

Each one of your reports could go into a function where you pass it the stocks array that is needed and from that function call the other functions for high low, sorting etc.

And lastly, organize the code a little. This thing is a mess in display. smile.gif
User is online!Profile CardPM
+Quote Post

cooplis
RE: Minimizing Code
31 Mar, 2008 - 10:54 AM
Post #3

D.I.C Head
**

Joined: 11 Sep, 2007
Posts: 77


My Contributions
Thank you I was thinking of making on big functions that does all of the averages and then just call from that.

Thanks alot,

Cooplis
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 10:14PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month