0

My program is supposed to get random numbers and put them in the array. Then it's supposed to find lowest, highest, sum, and average. Everything works except the lowest function. Any help please! EDIT: posted whole program

  int main()
{
    int nums[SIZE] = { 0 };
    int smallest;
    int highest;
    int sum;
    double avg;

    putNums(nums);
    getNums(nums, SIZE);
    getLowest(nums, SIZE, smallest);
    getHighest(nums, SIZE, highest);
    getSum(nums, SIZE, sum);
    getAvg(nums, SIZE, sum, avg);
    cout << "Smallest number of array: " << nums[smallest] << endl;
    cout << " Highest number of array: " << nums[highest] << endl;
    cout << "Sum of the array: " << sum << endl;
    cout << "Average of the array: " << setprecision(2) << fixed << showpoint << avg << endl;
}
void putNums(int nums[])
{
    ofstream outFil;    // output file object
    string filNam;      // output file name
    srand(static_cast<unsigned int>(time(0))); // seed random number generator  
    int num;            // random number to be generated
    int cnt = 1;        // count of random numbers

    cout << "To generates a file of " << SIZE << " random numbers\n";
    cout << " enter your output file name: ";   // "nums.txt"
    cin >> filNam;
    outFil.open(filNam.c_str());
    if (outFil) {
        for (int k = 0; k < SIZE; ++k) {    // generate and write numbers 
            num = MIN + rand() % MAX;       // generate random number
            cout << cnt << ". " << num << endl;
            outFil << num << endl;
            nums[cnt] = num;
            ++cnt;                          // increment count of numbers
        } // endfor
    }
    else {
        cout << "Open error on file " << filNam << endl;
        exit(1);
    } // endif
    outFil.close();        // close the file
    cout << "\n -- Done - file closed! --\n\n";
}
void getNums(int nums[], int SIZE)
{
    cout << "Your " << SIZE << " number(s) are listed: \n";
    for (int cnt = 1; cnt <= SIZE; cnt++){
        cout << nums[cnt] << endl;
    }
}
int getLowest(int nums[], int size, int & smallest)
{
    smallest = 0;
    int lowest = nums[0];
    for (int cnt = 0; cnt <= SIZE; ++cnt){
        if (nums[cnt] < lowest){
            lowest = nums[cnt];
            smallest = cnt;
        }
    }
    return smallest;
}
int getHighest(int nums[], int SIZE, int & highest)
{
    highest = 0;
    int largest = nums[0];
    for (int cnt = 0; cnt < SIZE; ++cnt){
        if (nums[cnt] > largest){
            largest = nums[cnt];
            highest = cnt;
        }
    }
    return highest;
}
int getSum(int nums[], int size, int & sum)
{
    sum = 0;
    for (int cnt = 0; cnt < SIZE; ++cnt){
        sum += (nums[cnt]);
    }
    return sum;
}
double getAvg(int nums[], int size, int sum, double & avg)
{
    avg = 0;
    avg = static_cast<double>(sum) / SIZE;
    return avg;
}
11
  • Why smallest = cnt + 1 ? Commented Nov 8, 2014 at 22:23
  • 2
    return lowest, not smallest Commented Nov 8, 2014 at 22:24
  • 1
    Your API is odd. Why don't you just return the index of the least value. Commented Nov 8, 2014 at 22:24
  • 2
    You are looping over SIZE+1 values. Also SIZE != size. Commented Nov 8, 2014 at 22:27
  • 1
    This has already been invented. It's std::min_element. Commented Nov 8, 2014 at 22:35

4 Answers 4

1

You should return the lowest variable and not smallest due the fact that smallest represents the index and lowest the value.

It should be:

return lowest;
Sign up to request clarification or add additional context in comments.

6 Comments

unless he's trying to return the index of the lowest... not sure
smallest (i.e. the index) is already being returned as a reference parameter.
@markg, He is saying his got a problem with the function, so it is a battle between 2 possible values, he already returning the index, than it leaving us with value, but I got your point.
@OrelEraki... yes, and if it's the index, he needs to stop +1 in the loop...I was just adding to your answer, not dibuting its validity
Also stark gave a good answer, he starts with 0 but iterate until <= SIZE he should iterate until < SIZE.
|
1

The source code is generally good but has conceptual and technical errors, here I send you a solution running

#include <iostream>
#include <fstream>

using namespace std;

#define SIZE 500
#define MIN 1
#define MAX 10000


void putNums(int *nums)
{
    ofstream outFil;    // output file object
    string filNam;      // output file name
    srand(static_cast<unsigned int>(time(0))); // seed random number generator
    int num;            // random number to be generated

    cout << "To generates a file of " << SIZE << " random numbers\n";
    cout << " enter your output file name: ";   // "nums.txt"
    cin >> filNam;
    outFil.open(filNam.c_str());
    if (outFil) {
        for (int k = 0; k < SIZE; k++) {    // generate and write numbers
            num = MIN + rand() % MAX;       // generate random number
            cout << (k + 1) << ". " << num << endl;
            outFil << num << endl;
            nums[k] = num;
        } // endfor
    }
    else {
        cout << "Open error on file " << filNam << endl;
        exit(1);
    } // endif
    outFil.close();        // close the file
    cout << "\n -- Done - file closed! --\n\n";
}
void getNums(int *nums, int size)
{
    cout << "Your " << size << " number(s) are listed: \n";
    for (int cnt = 0; cnt < size; cnt++){
        cout << nums[cnt] << endl;
    }
}
int getLowest(int *nums, int size)
{
    int lowest = nums[0];
    for (int cnt = 1; cnt < size; cnt++){
        if (nums[cnt] < lowest){
            lowest = nums[cnt];
        }
    }
    return lowest;
}
int getHighest(int *nums, int size)
{
    int largest = nums[0];
    for (int cnt = 1; cnt < size; cnt++){
        if (nums[cnt] > largest){
            largest = nums[cnt];
        }
    }
    return largest;
}
int getSum(int *nums, int size)
{
    int sum = 0;
    for (int cnt = 0; cnt < size; cnt++)
    {
        sum += nums[cnt];
    }
    return sum;
}
double getAvg(int nums[], int size, int sum)
{
    return static_cast<double>(sum) / size;
}

int main(int argc, const char * argv[])
{

    int *nums;
    int smallest;
    int highest;
    int sum;
    double avg;

    nums = (int*)malloc(sizeof(int) * SIZE);
    putNums(nums);
    getNums(nums, SIZE);
    smallest = getLowest(nums, SIZE);
    highest = getHighest(nums, SIZE);
    sum = getSum(nums, SIZE);
    avg = getAvg(nums, SIZE, sum);
    cout << "Smallest number of array: " << smallest << endl;
    cout << " Highest number of array: " << highest << endl;
    cout << "Sum of the array: " << sum << endl;
    cout << "Average of the array: " << fixed << showpoint << avg << endl;
    free(nums);
    return 0;
}

Comments

0

You have a memory overwrite in getLowest:

 for (int cnt = 0; cnt <= SIZE; ++cnt){

In the loop, you're accessing nums[SIZE] on the last iteration, so you're accessing the array outside of its bounds.

It should be this:

  for (int cnt = 0; cnt < SIZE; ++cnt){

Comments

-1

Try this

int getLowest(int nums[])
{
   int lowest = nums[0];
   for(int idx = 0; idx < SIZE; idx++)
   {
      if(nums[idx] < lowest)
         lowest = nums[idx];
   }

   return lowest;
}

I'm assuming SIZE is a constant global and you are returning the lowest, so you don't have to pass it by reference

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.