简体   繁体   中英

How to create right nested loops?

An array is given for 15 values. Take the absolute values for every three numbers in the array, going one after the other, and calculate the area of a triangle with sides, the values of which correspond to the taken numbers. Create a new array in which to enter the values of the resulting areas. Display all areas on the screen:

I know how to calculate the areas, etc. But I don't know how to organize right nested loops.

Could you explain it?

My example:

int numbers[15]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 3; j++) {   
    }   
}

It can be done using a single loop. I think if you reviewed my solution, then you can do it using nested loop.

double ans[10];
for(int i=0, j=0;i<15;i+=3, j++){
    double s=(double)(numbers[i]+numbers[i+1]+numbers[i+2])/2;
    double area = sqrt(s*(s-numbers[i])*(s-numbers[i+1])*(s-numbers[i+2]));
    ans[j]=area;
}
for(int i=0;i<5;i++) cout<<ans[i]<<endl;     

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM