Array Logic

Guys, can i ask for your help?, i just want to know on how this code works.

Objective: "Write a program that asks the user to type 10 integers of an array. The program must output the largest element in the array, and the index at which that element was found."

Can anyone explain on how the logic works when getting the largest element of the array?

I would really appreciate your help :D.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 #include <iostream>
using namespace std;

const int N=10;
int main()
{
    int t[N],i,index;

    for(i=0;i<N;i++)
    {
        cout << "Type an integer";
        cin >> t[i];
    }
    index=0;
    for(i=1;i<N;i++)
	if(t[index]<t[i])
            index=i;

    cout << "The greatest element of the array is: "
         << t[index] << " (index " << index << ")" << endl;
    return 0;
}
http://www.cplusplus.com/forum/general/51452/

Edit : I'll explain more soon

Edit: Looks like you've marked this as solved so, glad the linked helped you if it did.
Last edited on
Topic archived. No new replies allowed.