ARRAY ELEMENTS

Hello people;

Please does any one know how to return the location of the maximum and minimum elements in an array from a function to the main. I mean the locations not the values. Like if the maximum and minimum are the third and fifth elements respectively in the array(of course entered by the user) it returns the both locations to the main.

Thank you.(please i need urgent help)
Lots of ways. Why don't you give it a try first and post your attempt if you can't get it to work.
CAPS URGENT HOMEWORK EXAM!
I've been looking at my code for a long time now and i don't know where to start. I only know how to return values(can post that one if you want). but not locations in array.....!!

You can also give me some clues or the full solution(preferably)(that's because i have exams 2moro) :)

let me ask you the question how you traverse through an array/how you iterate all the positions?...

for example you have an array of 10 chars char array[10]; ... how do You let the users enter characters into it?...
To enter characters here i think you'll use;

cin.get(array,10).
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
#include <iostream>

using namespace std;
//the trick is in sending min_place and max_place as refferences in that way if u change them in the function they are automaticli
//changed in the main program, after that it is just finding the max and min elements and writing the value of i-the counter to the max_place and min_place
//and adding one because the array counter starts at 0 and not from 1
void MinAndMax(int array[],int array_lenght, int &min_place,int &max_place){

    int min(array[1]);
    int max(array[1]);

    for(int i=0; i<array_lenght; i++){
        cout << array[i] << "   ";

        if(array[i]>max){
        max=array[i];
        max_place=i+1;
       }

        if(array[i]<min){
        min_place=i+1;
        min=array[i];
        }
    }
}


int main(){

    int array[10]={23,54,28,64187,5,8,3920,6547,6895,18};

    int min_place,max_place;
    MinAndMax(array,10,min_place,max_place);

    cout << "The maximum element is in place : " << max_place << "\nand the minimum is in place " << min_place << endl;

    return 0;
}
Topic archived. No new replies allowed.