I have two arrays declared as fMarksL4[6]
and marksL4[12]
. I have taken the input from user to all values of marksL4[]
and want to assign them to fMarksL4[]
as following. Is there a simple way to do this using a loop?
fMarksL4[0] = (marksL4[0] + marksL4[1]) / 2;
fMarksL4[1] = (marksL4[2] + marksL4[3]) / 2;
fMarksL4[2] = (marksL4[4] + marksL4[5]) / 2;
fMarksL4[3] = (marksL4[6] + marksL4[7]) / 2;
fMarksL4[4] = (marksL4[8] + marksL4[9]) / 2;
fMarksL4[5] = (marksL4[10] + marksL4[11]) / 2;
Yes, notice that you are accessing the marksL4
at double (and double plus one) the current fMarksL4
index. So you could loop like
for (int i = 0; i < fMarksL4.length; i++) {
int j = i * 2;
fMarksL4[i] = (marksL4[j] + marksL4[j + 1]) / 2;
}
Yes. You can do this using loops
void solve(){
int j=0;
for(int i=0;i<6;i++){
fMarksL4[i] = (marksL4[j] + marksL4[j+1])/2
j=j+2;
}
}
This loop should work
for(int i=0; i< fMarksL4.length ; i++){
fMarksL4[i] = (marksL4[2 * i] + marksL4[2 * i + 1])/2;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.