Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

1 solution

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!
 
Share this answer
 
Comments
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....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900