Change elements in an array

Hey guys, for class we're supposed to create a program to manipulate the elements of the array. I have this code so far but I still need four more functions.
One function to search the array for a specific element and returns the position in the array.
The second functions to replace all instances of a specific element and replaces all instances of a specific element with a new user inputted value.
The third is supposed to be a bool function to receive the position of the array to modify and assign the new value to that position.
The last function is supposed to be another bool function to clear either a single element or every element in the array.

I was wondering if you guys could help me out with these four functions in any way cause I'm totally lost.

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
#include <iostream>
using namespace std;

int array [5];
int size = 5;
void input (int num);
void display (int num);
int statistics ( int &max, int &min, int &avg, int &sum);
int countNum (int &count, int &match, int num);


main () {
int num, min, max, avg, sum, count, match, seek;

        input (num);
        display (num);
        statistics (max, min, avg, sum);

                cout << "The min is: " << min << endl;
                cout << "The max is: " << max << endl;
                cout << "The avg is: " << avg << endl;
                cout << "The sum is: " << sum << endl;

                cout << "What number would you like to count? ";
                cin >> match;
                        countNum (count, match, num);
                cout << count << endl;

}


void input (int num) {

        for (int num = 0; num < size; num++) {
                cout << "Enter element " << num+1 << ": ";
                cin >> array [num];
        }

}


void display (int num) {

        cout << "You entered: \n";
        cout << "Postion \t Value \n";
        cout << "--------------------" << endl;
        for (int num = 0; num < size; num++) {

                cout << num+1 << "\t \t" << array [num] << endl;
}
}


int statistics (int &max, int &min, int &avg, int &sum) {

        max = array [0];
        min = array [0];
        avg = array [0];
        sum = array [0];

        for (int num = 0; num <size; num++) {

                if (min > array[num])
                        min = array[num];
                else if ( max < array [num])
                        max = array [num];

                        sum += array [num+1];
                        avg=sum/size;
        }
}


int countNum (int &count, int &match, int num) {

count = 0;
        for (num = 0; num < size; ++num) {
                if (array[num] == match)
                        ++count;
        }
}
Last edited on
Let's look at the first:

One function to search the array for a specific element and returns the position in the array.


How do you search through an array? How would know what index you are at when you found it?

Second:

The second functions to replace all instances of a specific element and replaces all instances of a specific element with a new user inputted value.


Similar to the last one. How might you change the value in the array once you found it?
Please edit your first post and put the code in code tags. Highlight the code and click the <> button on the right of the edit window.

This:
sum += array [num+1];
should be sum += array [num];
Also you should wait until the end of the loop before computing avg, just for efficiency.
I was thinking of using a for loop to search through the array and then using an if statement to see if the user inputted element matches an element of the array. But I'm not sure how to store and then replace it.

Also we're not supposed to have cin or cout statements in our functions (aside from getting and displaying the data) only in our main functions.
So I figured out the first two functions but am still not sure hoe to deal with the last two bool functions.
Topic archived. No new replies allowed.