Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C++ help please...logic on how to delete the smallest number does not make sense?

ok so i wrote this program that gives me a bunch of numbers at random and sorts the numbers but now i have to get rid of the smallest number... how do i do that?

here is my code so far

#include <iostream>

#include <cstdlib> //for rand()

#include <ctime> //for time()

using namespace std;

void printarr(int* ,int); //Function prototypes

void sort(int* , int);

double mean(int *, int);

int main()

{

//declare the array

int n;

cout << "Enter the number of elements: ";

cin >> n;

int* a = new int[n];

// puts randoms numbers into the array

for( int i = 0; i < n; ++i)

*(a+i) = (rand() + time(0)) % 100;

cout << "Original array: " << endl;

printarr (a,n);

//Sort it

sort (a,n);

cout << "Sorted array: "<< endl;

printarr(a,n);

//Find the mean

cout << "The mean of the numbers = " << mean (a, n) << endl;

system("PAUSE");

return 0;

}

void sort (int* x, int n)

{

int temp;

int smallest; //location of current smallest element

//find the smallest thing in x[i + 1] to x[n - 1]

for (int i = 0; i< n; ++i)

{

smallest = i;

for (int j = i+1; j < n; ++j)

if (*(x+j) < *(x+smallest))

{

smallest = j;

}

temp = *(x+i);

*(x+i) = *(x+smallest);

*(x+smallest) = temp;

}

}

void printarr (int* a, int n)

{

for(int i = 0; i < n; ++i)

cout << *(a+i) << " ";

cout << endl;

}

double mean (int* x, int n)

{

double sum =0;

for (int i = 0; i < n; ++i)

sum += *(x+i);

return sum / n;

}

Update:

I understand wat ur saying but i dont know how to ignore an element of the array

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    What is meant by ignoring an element of the array would be changing your for loops in the printarr function from:

    for(int i = 0; i < n; ++i)

    to:

    for(int i = 1; i < n; ++i)

    Since you have sorted the array, the first element, at index 0, would be ignored.

    You can do it a harder way if you want.

    Have another function that will do the following:

    for (int i = 0; i < n - 1; i++)

    {

    *x[i] = *x[i + 1];

    }

    This will make every element one place down in the array. In other words, the element that was at index 1 is now at index 0. The element at index n is now at index n - 1, including still being at index n.

    So after this function is called, make n = n - 1, as you do not want to print out the same element twice (at the end of the array).

    Then call your other functions as normal.

  • Sayee
    Lv 4
    1 decade ago

    Easiest way that I can think of is to use another array of numbers. After sorting, copy all elements from 1 to max into new array (ignoring 0th i.e. 1st element which will the smallest)

  • 5 years ago

    Well even though i am a girl i don't get why she couldn't be ur friend and what u said sounded like she flirted big time and just well...it is called a flirt and run. sorry she was a jerk to tell ya to delete your number.

Still have questions? Get your answers by asking now.