Hi everyone and thanks for looking to help me. I am working with a program involving arrays. I needed to write one that involves the use to
have the program take
about 10 numbers, list the minimum, the maximum, and the average.
Now the program that I have below runs fine which finds the average. But when I try to loop the program to find the minimum and maximum numbers it just repeats.
How do I loop in a loop to find the minimum and maximum nubers.
Please let me know. TY

#include 
using namespace std;


const int arraymax = 10;    

typedef int arraytype[arraymax];

 

float findaverage(arraytype numarray, int count);

void array2(arraytype numarray, int & count);


int main(void)
   {
   int array1;
   arraytype array3;
   float avg;

   array2(array3, array1);
   avg = findaverage(array3, array1);
   cout << endl << "The average is: " << avg << endl << endl;

   return 0;
   }

 
void array2(arraytype numarray, int & count)
   {
   int k;

   cout << "How many numbers would you like to enter? ";
   cin >> count;

   for (k = 0; k < count; k++)
      {
      cout << "Enter your numbers " << k << " ";
      cin >> numarray[k];
      }
   }

 
float findaverage(arraytype numarray, int count)
   {
   int k;
   float sum;

   sum = 0.0;
   for (k = 0; k < count; k++)
      sum=sum + numarray[k];

   if (count > 0)
      return sum / count;
   else
      return 0.0;
   }
Member Avatar for iamthwee

Ah this is what you must do.

1. Write a flow chart first.
2. Try to do this without functions they will only confuse you.

>>How do I loop in a loop to find the minimum and maximum nubers

Let the first element be the largest (or smallest) element in the array. If the array only has one element then that must be true. If the array has more than one element it may or may not be true so look at each element in the array after the first element. If that element is larger/smaller than the current largest/smallest, then let the current element be the largest/smallest. At the end of the loop the current largest/smallest is the answer.

>>How do I loop in a loop to find the minimum and maximum nubers

Let the first element be the largest (or smallest) element in the array. If the array only has one element then that must be true. If the array has more than one element it may or may not be true so look at each element in the array after the first element. If that element is larger/smaller than the current largest/smallest, then let the current element be the largest/smallest. At the end of the loop the current largest/smallest is the answer.

Okay with hours of work I came up with this.

//Jessica Moore
#include <iostream>
using namespace std;
 
void getStats( double * min, 
               double * max,
               double * average,
               int numsNum );
               
int main()
{
    double min, max, average;
    int nums;
    do ;
    {
        cout << "How many Number will you Enter? " << flush;
        cin >> nums;
    }
 {
  while (nums <0);
                                                                                
                                                                           
                                                                               
    getStats( &min, &max, &average, nums );                      
    cout <<"The Minimum is: " << min << endl
         <<"The Maximum is: " << max <<endl
         <<"The Average is: " << average <<endl;                                                               

    return 0;
}
void getStats( double * min, double * max, double * average, int numsNum )
{
int *myArray=new int[numsNum];
int minimum = myArray[0];
int maximum = myArray[0];
int sum = 0;
for (int i = 0; i < numsNum; i++)
{
                cout << "Enter you number[" << i << "]: ";
              cin >> myArray[numsNum];
for (int i = 0; i < numsNum; i++)
    if (myArray[numsNum] < minimum)
{
                    minimum = myArray[numsNum];       
                    *min = minimum;
              
for (int i =0; i < numsNum; i++)
    if (myArray[numsNum] > minimum)
{        
              maximum = myArray[numsNum];
              *max = maximum;
for (int i = 0; i < numsNum; i++) 
{
     sum = ((sum + myArray[numsNum])/numsNum);   
     *average = sum;  
}
} 
}
}
} 
 
}

I still have some errors and I can't seem to figure out the mistakes TY for all your help

>I still have some errors and I can't seem to figure out the mistakes TY for all your help
I refuse to read your code until you format it and put it in code tags.

>I still have some errors and I can't seem to figure out the mistakes TY for all your help
I refuse to read your code until you format it and put it in code tags.

And don't let us try to figure out what the errors are. Tell us. It's not our program.

do ;
    {
        cout << "How many Number will you Enter? " << flush;
        cin >> nums;
    }
{
  while (nums <0);

Your loop is really screwed up. I don't think you want that semicolon after do , and that opening brace before the while statement. Here's how do...while statements are supposed to look like:

do {
// do stuff here

} while (expression == true);

And by the way you've got 1 too many closing braces at the end of your program. Proper indentation would help you to see problems like that.

Here's what I think is a correct version of your function. Look at this version versus yours. Identify differences and explain why this version is more likely to work than yours. Adapt this version to your needs if need be. Note: I haven't compiled this version, so there may still be some errors.

I always use reference variables in preference to pointer variables if possible because the syntax is so much easier.

void getStats( double & min, double & max, double & average, int numsNum )
{
  int * myArray=new int[numsNum];
  int sum;

  for (int i = 0; i < numsNum; i++)
  {
     cout << "Enter you number[" << i << "]: ";
     cin >> myArray[i];
  }

  //set min to be first element entered in myArray
  min = myArray[0];

  //for each element in myArray after first element
  for (int i = 1; i < numsNum; i++)
 {
    if (myArray[i] < min)
        min = myArray[i];    
 }   
                    
 //set max to be first element of myArray
 max = myArray[0];

 //look at all elements in myArray after first element            
 for (int i = 1; i < numsNum; i++)
 {
    if (myArray[i] > max)        
       max = myArray[i];
 }

 //set sum to zero
 sum = 0;
 for (int i = 0; i < numsNum; i++) 
 {
     //add each value of myArray to sum;
     sum = sum + myArray[i];
 }

 //now calculate average
 average = sum/numsNum;

 delete [] myArr;
}
Member Avatar for iamthwee

pretty good if you compiled that in your head.

Here's what I think is a correct version of your function. Look at this version versus yours.

I disagree on principal. She already has a function called findeaverage() so why would you want to make a do-all function? They are harder to debug and maintain. Take each piece you want to accomplish and make a function out of it. Return one value from each with the return statement.

And change the name of array1. It's not an array. Call it cout.

In principle I agree with WaltP. Filling the array, finding the maximum value of the array, finding the minimum value of the array, and finding the average of the array should ideally be done with separate functions. However, for teaching purposes, to compare the function posted by the OP with ways to correct it using their logic seemed most appropriate. Now, if the OP has a working function that does everything desired, breaking it up into smaller functions that do one task per function is the better way to go.

In principle I agree with WaltP. Filling the array, finding the maximum value of the array, finding the minimum value of the array, and finding the average of the array should ideally be done with separate functions.

:)

However, for teaching purposes, to compare the function posted by the OP with ways to correct it using their logic seemed most appropriate.

True, except OP had no code for anything except average, and that was in a function. Therefore starting out with additional functions is a preferred way -- it starts the learning process in the right direction rather than attempting to modularize after the all-in-one function is written. Takes twice as much work, and no unlearning bad habits in the future.

You can just do this. It checks through all the elements in the array and finds out each check which is the smallest, biggest and average value of the array so far. At the end you have your results

#include <iostream>

using namespace std;

const int MAX = 10;

int main()
{
    int array[MAX] = {6,7,5,9,8,98,3,2,4,10};

    int min=1, max=1, avg=0, i=0;

    for (i=0; i<MAX; i++)
    {
        if (array[i] < array[min])
            min = i;

        if (array[i] > array[max])
            max = i;

        avg += array[i];
    }

    cout << "min: " << array[min] << endl;
    cout << "max: " << array[max] << endl;
    cout << "avg: " << avg/MAX << endl;

    return 0;
}
commented: Don't give homework solutions to newbies. +0

You can just do this. It checks through all the elements in the array and finds out each check which is the smallest, biggest and average value of the array so far. At the end you have your results

Your code doesn't work. It returns a minimum of 1 and 1 is not in the list you gave it. And if the list contains all negative numbers, your max will also be 1, which is also wrong.

Before doing someone's homework for them at least test your code.

works fine for me.. Must be some differences with your compiler

>Before doing someone's homework for them at least test your code.
It looks like it should work just fine. How did you test it?

With Visual C++.. all my code is tested and always works before i post it. These people are simply boycotting me for their own reasons.

By the way, if i want do to post homework it doesnt have to work... Then again, its best not to work.. It'll keep you people happy (god knows that'll be a miracle), and the people reading the code will have to work on it to make it a working program..

It'll keep you people happy (god knows that'll be a miracle)

I was born happy ;)

Relax May4life, Narue was agreeing with you. She was asking WaltP what was wrong with it. Just a little mis-communication Works fine with VS2005 Pro too.

>With Visual C++.. all my code is tested and always works before i post it.
I was quoting WaltP. He claimed that it didn't work, yet I didn't see any problems and on multiple compilers it works as expected.

>These people are simply boycotting me for their own reasons.
"These people"? Have you been getting this in other threads as well? In this thread only one person claims that your code is broken.

These people are simply boycotting me for their own reasons.

Hold it buddy, everyone makes mistakes, and the same must have happened by looking at your code.

OK, I made a misteak (sic). So sue me. You used a technique I wouldn't use and misread the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.