Solved

Need to know if this program using an array is correct.

Posted on 2006-11-07
4
169 Views
Last Modified: 2010-04-24
Hi Experts!

I need to know if this program that I have made is correct according to the instructions given to me.
I want to know if I did something incorrectly or misunderstood the instructions.

Instructions:

Write a complete C++ program that will let a user input 8 integer values into an array. After entering the values display the following statistics: The third element in the array The average of all the numbers The difference between the maximum and the minimum numbers in the array.

First of all, I should note that I'm not getting the difference between the max and min numbers in the array. Why?

Program:

#include <iostream>
#include <iomanip>
using namespace std;
 
void readem(int * & number, int &quant)

{
    quant = 8;
    number = new int[quant];

   
    for(int i=0; i<quant; i++)
    {
            cout << "\nEnter the value for number " << i+1 << "? ";
        cin >> number[i];
    }

    return;
}



int sum(const int array[], int num)

{
    int total=0;

   for(int i=0; i < num; i++)
        total += array[i];

    return total;
}

void sortArray(int array[], int elems)
{
      int temp, diff;
      bool swap;

      do
      {
            swap = false;
            for (int count = 0; count < (elems - 1); count++)
            {
                  if (array[count] > array[count +1])
                  {
                        temp = array [count];
                        array[count] = array[count+1];
                        array[count +1] = temp;
                        swap = true;
                  }
            }
      } while (swap);
      cout << "\nThe third element is " << array[2];
      diff = (array[8] - array[0]);
      cout << "\nThe difference between the max and min numbers is: "<< diff;

}




int main()
{
    int * number;  
    int num = 0;      
    int total;

   
    readem(number, num);
 
   
 
    total = sum(number, num);

      sortArray(number, num);
      

    cout << "\nAverage = " << float(total)/num << endl;
 
     
     delete[] number;  
 
    return 0;
}




0
Comment
Question by:manz2
  • 2
  • 2
4 Comments
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
Hi manz2,

You dont have to sort the array to get max and min ... a simple loop would do

int max = INT_MIN;
int min = INT_MAX;

for ( i=0; i<arraylength; i++)
{
       if ( array[i] > max)
             max = array[i];
      if (array[i] < min )
             min = array[i];
}

Cheers!
sunnycoder
0
 

Author Comment

by:manz2
Comment Utility
Ok cool! Thanks for that, it will help.

As far as the way I have it right now, why am I getting weird numbers when it displays the difference of max and min.

diff = (array[8] - array[0]);
cout << "\nThe difference between the max and min numbers is: "<< diff;

is this not correctly coded?


0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
Comment Utility
Valid indices are from 0 to 7 ... array[8] is invalid .. Have not verified your sorting routine yet.
0
 

Author Comment

by:manz2
Comment Utility
I just realized there was not array[8] only 7 like you said. Thanks again for your help and sample tip!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now