Solved

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

Posted on 2006-11-07
4
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 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