Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual c++ problem with command line conflicts 22 197
How to correct a C6001issue? 5 222
Random number generation (what's missing?) 7 94
MS Visual Studio 2015 - Install Error 5 98
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

820 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