15,746,302 members
See more:
i found maximum value using array. now i want to replace that maximum value with 99 and display series again.for example i enter :
1
2
5
4
3
maximum values will be 5
now i want to replace 5 with 99
and should display the series like this..
1
2
99
4
3
here is the program-----------------------
---------------------------------------------
C#
#include<stdio.h>
int main()
{
int n=99,a[5],max,i;
printf("Enter 5 Values :");
for(i=0;i<=4;i++)
{
scanf("\n%d",&a[i]);
}
max=a[0];
for(i=1;i<=4;i++)
{
if(max<a[i])
{
max=a[i];

}

}
printf("\nMaximum no is :%d\n",max);

n=max;

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

printf("\n%d",a[i]);
}
return 0;
}

Posted

## Solution 1

Simple: store an index in your first loop which reflects the position of the largest element.
So, when you initialize the max, you set your maxIndex to zero.
When you find a bigger value, you set max to the value, and maxIndex to the index of that value.
Then, outside the loop, you set the array element at the maxIndex to your new value.

But since this smells of your homework, I'll leave the code to you!

Stefan_Lang 4-Sep-13 8:37am
My 5, especially for _not_ providing the actual code ;-)
OriginalGriff 4-Sep-13 8:55am
:laugh:
How do you describe an algorithm so they stand a chance of it working, without giving code?
(especially when it's only three lines to be added?) ;)
nv3 4-Sep-13 11:44am
Well done!
ruzin 4-Sep-13 22:22pm
please provide code so that i can understand better......
OriginalGriff 5-Sep-13 1:45am
My friend, this is three lines of code.
I know that you have all the knowledge, because the various instructions are all used in your existing code, albeit in slightly different form.
A little gentle thinking about the instructions I gave you and the task you have to perform should make it abundantly clear.

To be honest, if you can't convert my instructions to code with something this trivial, then programming is perhaps not for you, and you should consider changing courses....