Stop input when i a negative number is put int an array

hey guys this is my first time using this form.
i really need help with this program.
the purpose of this program is to ask the user to input up to 100 numbers in an array and stop input when the user inputs a native number. the the program sorts the array and then finds the average. i did everything in the program i just can't figure how to make the input to stop when the user enter a negative number. I've fries while loops and do while loop but nothing works it would be great if someone can help please !!
i just set the army to 5 so i can test the program. it is supposed
to be array[100]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  #include <iostream>
#include <cmath>;
using namespace std;

/****************************
 PRTOTYPE FUNXTION
 **************************/

int findSmallestRemainingElement (int array[], int size, int index);
void swap (int array[], int first_index, int second_index);
void sort (int array[], int size);
void displayArray (int array[], int size);
int average (int array [],int size);
int sum (int array[], int size);


/****************************
 MAIN FUNCTION
 ****************************/

int main ()
{
    // DECLARE THE ARRAY
    int array[ 5 ];
    // FOR LOOP TO KEEP ASKING FOR IMPUT
    
    for ( int i = 0; i < 5; i++ )
    {
        cout << "Enter value " << i << ": ";
        cin >> array[ i ];
    }
    
    //PRINT ORHINAL ARRAY
    cout << "Original array: ";
    displayArray( array, 5 );
    cout << '\n';
    
    //SORT ORGINAL ARRAY
    sort( array, 5 );
    
    // DISPLAY SORTED ARRAY
    cout << "Sorted array: ";
    displayArray( array, 5 );
    cout << '\n';
    
    //sum of number in array
    
    int sum = 0;
    

    
    for (int j = 0; j < 5; ++j) {

        
        sum += array[j];
    }
    
    
    cout << "\n The mean is\t" << sum / 5 << "\n";
    
}
/*********************************
 SORT FUNCTION
 ********************************/
void sort (int array[], int size)
{
    for ( int i = 0; i < size; i++ )
    {
        int index = findSmallestRemainingElement( array, size, i );
        swap( array, i, index );
    }
}
/*******************************
 FIND SMALLEST NUMBER
 ******************************/


int findSmallestRemainingElement (int array[], int size, int index)
{
    int index_of_smallest_value = index;
    for (int i = index + 1; i < size; i++)
    {
        if ( array[ i ] < array[ index_of_smallest_value ]  )
        {
            index_of_smallest_value = i;
        }
    }
    return index_of_smallest_value;
    
}

/**************************
 SWAP FUNCTION
 *************************/

void swap (int array[], int first_index, int second_index)
{
    int temp = array[ first_index ];
    array[ first_index ] = array[ second_index ];
    array[ second_index ] = temp;
}

/******************************
 DIPLAY ARRAY FUNCTION
 *****************************/

void displayArray (int array[], int size)
{
    cout << "{";
    for ( int i = 0; i < size; i++ )
    {
        
        if ( i != 0 )
        {
            cout << ", ";
        }
        cout << array[ i ];
    }
    cout << "}";
}



Instead of the for() loop for reading the array values think about using a while() loop that checks to insure that the index is still in bounds, and that the value entered is not a negative number.

Also if you make a named constant for the array size it will be easier to change from 5 elements to the 100 elements.

Ok,if your problem was tht you could not add and average negative numbers,then I think Ive solved the problem.

First of all,I checked if the program ran right.
As it did so,then I thought about the problem and found out tht you hav been using int instead of unsigned int.

Yes,i dont remeber the range but i think tht unsigned int goes from -32128 to +32128( im not sure,pls check it).

I reworked your code,placing signed in every place your array[] was declared.
This is my reworked code....


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

  #include <iostream>
#include <cmath>;
using namespace std;

/****************************
 PRTOTYPE FUNXTION
 **************************/

int findSmallestRemainingElement (signed int array[], int size, int index);
void swap (signed int array[], int first_index, int second_index);
void sort (signed int array[], int size);
void displayArray (signed int array[], int size);
int average (signed int array [],int size);
int sum (signed int array[], int size);


/****************************
 MAIN FUNCTION
 ****************************/

int main ()
{
    // DECLARE THE ARRAY
    signed int array[ 5 ];
    // FOR LOOP TO KEEP ASKING FOR IMPUT

    for ( int i = 0; i < 5; i++ )
    {
        cout << "Enter value " << i << ": ";
        cin >> array[ i ];
    }

    for(int j=0;j<5; j++)
    {
    if(array[j]<=0)
     {
       cout<<"Array element "<<j<<" is either negative or zero."<<endl
           <<"Element is : "<<array[j]<<endl;
     }
    }

    //PRINT ORHINAL ARRAY
    cout << "Original array: ";
    displayArray( array, 5 );
    cout << '\n';

    //SORT ORGINAL ARRAY
    sort( array, 5 );

    // DISPLAY SORTED ARRAY
    cout << "Sorted array: ";
    displayArray( array, 5 );
    cout << '\n';

    //sum of number in array

    signed int sum = 0,avg=1;



    for (int j = 0; j < 5; ++j)
    {
      sum=sum + array[j];
    }

    avg=sum/5;

    cout << "\n The mean is\t" << avg<< "\n"
         <<"And the sum is : "<<sum<<endl;

}
/*********************************
 SORT FUNCTION
 ********************************/
void sort (signed int array[], int size)
{
    for ( int i = 0; i < size; i++ )
    {
        int index = findSmallestRemainingElement( array, size, i );
        swap( array, i, index );
    }
}
/*******************************
 FIND SMALLEST NUMBER
 ******************************/


int findSmallestRemainingElement (signed int array[], int size, int index)
{
    int index_of_smallest_value = index;
    for (int i = index + 1; i < size; i++)
    {
        if ( array[ i ] < array[ index_of_smallest_value ]  )
        {
            index_of_smallest_value = i;
        }
    }
    return index_of_smallest_value;

}

/**************************
 SWAP FUNCTION
 *************************/

void swap (signed int array[], int first_index,int second_index)
{
    int temp = array[ first_index ];
    array[ first_index ] = array[ second_index ];
    array[ second_index ] = temp;
}

/******************************
 DIPLAY ARRAY FUNCTION
 *****************************/

void displayArray (signed int array[], int size)
{
    cout << "{";
    for ( int i = 0; i < size; i++ )
    {

        if ( i != 0 )
        {
            cout << ", ";
        }
        cout << array[ i ];
    }
    cout << "}";
}



Although you hav included cmath,you havent used it.

Dont just copy paste my code and run it,learn from it and try to improve the code(like jib said earlier and more).
tanks for your help but what i need the program to do is take user input until they enter a negative number. when a negative umber is entered the program proceeds to sort the items in the array. the negative number is a signal to tell the program to start sort the number n the array
Topic archived. No new replies allowed.