0

I'm taking my first ever programming class and most of it is self taught. So please bear with me.

After the users input numbers, such as 10, 20, 100, 3, 4 there should be an output to read "the index of the largest elmeent in list1 array is _ The largest element in list1 array is 100" "

Here is my code so far. I know how to get the output of the largest array but not the first line or how to name my array. Thanks so much in advance.

#include<stdio.h>
int main()
{
int i;
float arr[5];

printf("Please enter five numbers:\n ");

for (i = 0; i < 5; ++i)
{

    scanf_s("%f", &arr[i]);
}

for (i = 1; i < 5; ++i)
{
    if (arr[0] < arr[i])
        arr[0] = arr[i];
}
printf("Largest element = %.2f", arr[0]);

return 0;
}
1
  • It is either int main(void) or int main(int argc, char **argv), not int main(). Commented Oct 1, 2018 at 3:54

4 Answers 4

2

This code here will give you the biggest element is the array but it also changes the order in which the elements were entered. Instead of taking the biggest element to the 0th position of the array,you can simply use an integer to store the index of the biggest element.Your program can be modified like this :

#include<stdio.h>
int main()
{
int i,temp=0;
float arr[5];

printf("Please enter five numbers:\n ");

for (i = 0; i < 5; ++i)
{

    scanf_s("%f", &arr[i]);
}

for (i = 1; i < 5; ++i)
{
    if (arr[temp] < arr[i])
    temp=i;
}
printf("Largest element = %.2f", arr[temp]);
printf("Index = %d",temp);

return 0;
}

Hope the answer was useful.

Sign up to request clarification or add additional context in comments.

Comments

1

Your approach to find the max element in an array is fine. But it alters the original array. If it okay with you, then go ahead with it, else if you would like to keep the array in tact as well as find the max element with the index, then check the code which I have attached below. If you intend to keep the methodology same, then just add a variable, which you should update as and when you find a higher number, as follows:

int max_index;       //declare before
if (arr[0] < arr[i]) {
     max_index  = i;
     arr[0] = arr[i];
}

If you are interested to keep the array unchanged and find out the max element with its index, the code would be as follows:

#include<stdio.h>
int main()
{
int i, max_index;
float arr[5], max;

printf("Please enter five numbers:\n ");

for (i = 0; i < 5; ++i)
{

    scanf("%f", &arr[i]);
}
max  = arr[0];//start off assuming that the 1st element is the max
for (i = 0; i < 5; i++)//now compare it with the rest of the array, updataing the max all along
{
    if (arr[i] > max) {
        max = arr[i];
        max_index = i;
    }
}
printf("Largest element = %.2f at index %d", max, max_index);

return 0;
}

Hope this helps.

5 Comments

Are you aware that max_index = i; is not part of the if ?
Hi, this code keeps giving me max index is 4 even when that is not correct
Nevermind, the code is missing a curly bracket in the if loop. thank you!
Oh I'm sorry guys. I think I overlooked that part. Thanks for pointing out the error... I've corrected it now.
It would be better if you initialized max_index.
1

You know, that you can get the element if you know its index. This means, you can store the index of the "known biggest" element, and you can refer to the biggest element using the stored index.

int imax = 0;
for (i = 1; i < 5; ++i)
{
    if (arr[imax] < arr[i])
        imax = i;
}
printf("Largest element = %.2f at index %i\n", arr[imax], imax);

.

2 Comments

As op is learn C, it could be good to use size_t instead of int for index.
@Stargateur Yeah I agree, but it looks like OP is at the beginning, better not complicate things.
0

enter image description here

use this code in place of last loop

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.