Solved

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

Posted on 2006-11-07
4
175 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

717 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