2

I'm pretty new to C++, and I need help figuring out the code for dropping the lowest value of a randomly generated set of numbers. Here is my code so far:

   //Create array and populate the array with scores between 55 and 10
//  Drop lowest Score 

#include <iostream>
#include <cstdlib>//for generating a random number
#include <ctime>
#include <iomanip>
#include <algorithm>
#include <vector>

using namespace std;


//function prototype
int *random (int);


int main()
{   int *numbers; //point to numbers
    //get an array of 20 values
    numbers = random(20);
    //display numbers
    for (int count = 0; count < 20; count++)
        cout << numbers[count] << endl;
    cout << endl;


system("pause");
    return 0;
}

//random function, generates random numbers between 55 and 100 ??

int *random(int num)
{   int *arr; //array to hold numbers
    //return null if zero or negative
    if (num <= 0)
        return NULL;
    //allocate array
    arr = new int[num];
    //seed random number generator
    srand(time (0));
    //populate array
    for (int count = 0; count < num; count++)
        arr[count] = (rand()%(45) +55);
    //return pointer

    //
    return arr;
}

For this piece of code, how would I sort or find the lowest score to drop it after the function returns the random numbers?

  int main()
    {   int *numbers; //point to numbers
        //get an array of 20 values
        numbers = random(20);
        //display numbers
        for (int count = 0; count < 20; count++)
            cout << numbers[count] << endl;
        cout << endl;


    system("pause");
        return 0;
    }

Your suggestions are appreciated!

Kara
  • 6,115
  • 16
  • 50
  • 57
user1745511
  • 67
  • 1
  • 5
  • 7

6 Answers6

4

In general, to find the lowest value in an array, you can follow this psuedo-algorithm:

min = array[0] // first element in array
for (all_values_in_array)
{
    if (current_element < min)
        min = current_element
}

However, you can't "drop" a value out of a static array. You could look into using a dynamic container (eg. vector), or swapping the lowest value with the last value, and pretending the size of the array is 1 less. Another low level option would be to create your own dynamic array on the heap, however, this is probably more complicated than you are looking for.

Using an vector would be much easier. To drop the lowest element, you just have to sort in reverse order, then remove the last element. Personally, I would recommend using a vector.

Community
  • 1
  • 1
2

The obvious approach to find the smallest element is to use std::min_element(). You probably want to use std::vector<T> to hold your elements but this isn't absolutely necessary. You can remove the smallest value from an array like this:

if (count) {
    int* it = std::min_element(array, array + count);
    std::copy(it + 1, array + count--, it);
}

Assuming you, reasonable used std::vector<int> instead, the code would look something like this:

if (!array.empty()) {
    array.erase(std::min_element(array.begin(), array.end()));
}
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
0

First find the index of the lowest number:

int lowest_index=0, i;
for (i=0; i<20; i++)
    if (arr[i]<arr[lowest_index])
        lowest_index=i;

Now that we know the index, move the numbers coming after that index to overwrite the index we found. The number of numbers to move will be 19 minus the found index. Ie, if index 2 (the third number, since the first is at index 0) is lowest, then 17 numbers comes after that index, so that's how many we need to move.

memcpy(&arr[lowest_index],&arr[lowest_index+1],sizeof(int)*(19-lowest_index))

Good luck!

Mikael Lindqvist
  • 775
  • 5
  • 21
0

Sort the array ascending.
The lowest value will be at the beginning of the array.

Or sort the array descending and remove the last element.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

Further to what others have said, you may also choose to use something like, perhaps a std::list. It's got sorting built-in, also offering the ability to define your own compare function for two elements. (Though for ints, this is not necessary)

First, I typically typedef the vector or list with the type of the elements it will contain. Next, for lists I typedef an iterator - though both of these are merely a convenience, neither is necessary.

Once you've got a list that will holds ints, just add them to it. Habit and no need to do otherwise means I'll use .push_back to add each new element. Once done, I'll sort the list, grab the element with the lowest value (also the lowest 'index' - the first item), then finally, I'll remove that item.

Some code to muse over:

#include <cstdio>
#include <cstdlib>
#include <list>


using namespace std;

typedef list<int> listInt;
typedef listInt::iterator listIntIter;

bool sortAsc(int first, int second)
{
    return first < second;
}

bool sortDesc(int first, int second)
{
    return first > second;
}

int main (void)
{
    listInt mList;
    listIntIter mIter;
    int i, curVal, lowestScore;

    for (i=1; i<=20; i++)
    {
        curVal = rand()%45 + 55;
        mList.push_back(curVal);
        printf("%2d. %d\n", i, curVal);
    }
    printf("\n");

    mList.sort();
//    mList.sort(sortAsc);  // in this example, this has the same effect as the above line.
//    mList.sort(sortDesc);

    i = 0;
    for (mIter=mList.begin(); mIter!=mList.end(); mIter++)
        printf("%2d. %d\n", ++i, *mIter);
    printf("\n");

    lowestScore = mList.front();
    mList.pop_front();
    printf("Lowest score: %d\n", lowestScore);

   return 0;
}

Oh, and the choice to use printf rather than cout was deliberate too. For a couple of reasons.

  1. Personal preference - I find it easier to type printf("%d\n", someVar); than cout << someVar << endl;
  2. Size - built with gcc under windows, the release-mode exe of this example is 21kb. Using cout, it leaps to 459kb - for the same functionality! A 20x size increase for no gain? No thanks!!

Here's an std::list reference: http://www.cplusplus.com/reference/stl/list/

enhzflep
  • 12,927
  • 2
  • 32
  • 51
0

In my opinion the most optimal solution to your problem would be to use a linked list to store the numbers, this way you can use an algorithm with complexity O(N) = N to find the smallest element in the list, it is a similar finding method given by user1599559 or Mikael Lindqvist, you only need stored together with the minimum value the pointer to the Item(ItemX) in the linked list that store it, then to eliminate Item X just tell Item X - 1 points to Item X + 1 and free memory allocated by Item X