Solved

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

Posted on 2006-11-07
4
170 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
ID: 17895723
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
ID: 17895808
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
ID: 17895815
Valid indices are from 0 to 7 ... array[8] is invalid .. Have not verified your sorting routine yet.
0
 

Author Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
.NET Best Practice for SQLclient Connection String - Persist Security Info 4 299
How to 4 51
Visual Studio community 2015 2 66
Paint/Redraw window while dragging 16 68
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 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

10 Experts available now in Live!

Get 1:1 Help Now