1

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++) {   
    }   
}
1
  • 1
    You need only one loop for (int i = 0; i < 15; i += 3). Inside that loop use numbers[i], numbers[i + 1], and numbers[i + 2] as side values. Commented Aug 2, 2020 at 10:00

1 Answer 1

1

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;     
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.