-
Notifications
You must be signed in to change notification settings - Fork 0
/
max_element_positions.c
64 lines (48 loc) · 1.07 KB
/
max_element_positions.c
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
//=======================================================================
// Copyright (c) 2014 Kiriakos Velissariou
// Distributed under the MIT License.
// (See accompanying file LICENSE or copy at
// http://opensource.org/licenses/MIT)
//=======================================================================
#include<stdio.h>
void main(void)
{
int length, i, element, max, j, indexes[50], list[50];
printf("Give length of list: ");
scanf("%d", &length);
for (i = 0; i < length; i++)
{
printf("Give element: ");
scanf("%d", &element);
list[i] = element;
}
max = list[1];
/* find maximum value */
for (i = 0; i < length; i++)
{
if (list[i] > max)
max = list[i];
}
/* find positions of maximum value*/
j = 0;
for (i = 0; i < length; i++)
{
if (list[i] == max)
{
indexes[j] = i;
j +=1;
}
}
printf("The maximum element is %d and is found at the following locations: ", max);
for (i = 0; i < j; i++)
{
if (i == j - 1)
{
printf("%d.", indexes[i]);
}
else
{
printf("%d, ", indexes[i]);
}
}
}