What should be at line number 3 to get the total sum of array "sum" ?
public int totalsum( int[] sum ){
int a, b= 0 ;
//which 'for' loop should be here(line :3)
{
b += sum[ a++ ] ;
}
return b ;
}
Which 'for' loop should be at line number 3 to calculate total sum of the array
"sum" :
1. for( int a = 0 ; a< sum.length ; )
2. for( a= 0 ; a< sum.length ; )
3. for( a = 0 ; a< sum.length ; a++ )
4. for( a = 1 ; i <= sum.length ; a++ )
(2)
In 1st option, variable a is declared again i.e. duplicate variable.
In 3rd option, due to extra loop iterator "a++" , the summation would not be correct.
In 4th option. due to initialization a=1 + due to extra loop iterator "a++" , the summation would not be correct.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.