I am having some problems with array manipulation

I am having some problems with array manipulation

Problem Description:

I am writing some code in C in which I have to find the max element of every row and then change every element of every row left of the max element of that row to the max element of the row.

I tried this:

for(i=0;i<R;i++)
    {
        MAX[i]=0;
        for(j=0;j<C;j++)
        {
            if(MAX[i]<A[i][j])
            {
                MAX[i]=A[i][j];
            }
        }
    }
    for(i=0;i<R;i++)
    {
        for(j=0;j<C;j++)
        {
           while(A[i][j]<MAX[i])
           {
                A[i][j]=MAX[i];
           }
        }
    }

}

… but of course it changes every element of every row to the max element of that row so I suspect I have to modify that block of code.
Any help would be appreciated since I am new to coding.

Solution – 1

Your indentation was confusing to say the least. I think this is what you meant you wanted to achieve but I am not sure:

for(i=0;i<R;i++){
    MAX[i]=0;
    for(j=0;j<C;j++){
        if(MAX[i]<A[i][j]){
            MAX[i]=A[i][j];
        }
    }
}
    
for(i=0;i<R;i++){
    int j = 0;
    while(A[i][j] < MAX[i]){
        A[i][j] = MAX[i];
        j++;
    }
}
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject