Assistance needed on a code in my assignment.

Hi,
I extremely need guidance on this! I shown what I have so far below.
My assignment states:


Look up (or derive yourself) the formula for the area of a segment in terms of the chord length and the diameter, and write a C++ program to ask for the chord length in inches and print out the percentage of a 14” pizza cut off. Running your program should look like this:

Pizza diameter is 14”.
Enter chord length in inches: 0
That cuts off 0% of the pizza.
Enter chord length in inches: 14
That cuts off 50% of the pizza.
Enter chord length in inches: 10.17
That cuts off 10% of the pizza.

Use while(cin >> chord) so the program will keep reading and calculating until end

So I kinda of started it but I am really new to C++.
I need extra tips or knowledge with commands and compiling the area or making an int for area so i can run the program so here what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include "std_lib_facilities_4.h"

  int main() {

//A = r2arcsin(0.5c / r) - 0.5c*sqrt(r2 - c2 / 4)

        int area = r^2*asin*(0.5c / r) - 0.5c*sqrt(r^2 - (c^2 / 4))
	string chord 
	cin >> chord

	while (cin >> chord)

	cout << "Enter chord length in inches: " << endl;
	cout << "That cuts out " << chord  "% of the pizza." << endl;


}


That's what I have so far, I know I am missing bunch of stuff, but i have no idea what chord does, or how to set up my area, now the area i found was the ones in the comment in the code. ALSO we are using the "std_lib_facilities" because the that's what the author of the book and my instructor wants us to use.
EDIT: corrected...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

int main()
{
    double diameter  = 14, chord;
    cout << "Pizza diameter is " << diameter << " inches.";
    
    cout << "Enter chord length in inches: ";

    while(cin >> chord)
    {
       // area = plug in diamater and chord to find area;

       // convert area into percentage (total area of a 14'' pizza = 49*pi square inches)

       // cout << "That cuts out " << percentage << " percent of the pizza. "

        cout << "Enter chord length in inches: ";

       
    }

}
Last edited on
What is chord? Perhaps: http://planetcalc.com/1421/

The while is a control structure. See http://www.cplusplus.com/doc/tutorial/control/

There is no implicit multiplication, so 0.5c must be 0.5 * c

C++ does not have operator for power. The ^ does something entirely different. See http://www.cplusplus.com/doc/tutorial/operators/
There is a function pow() (read its documentation), but power of two is a simple r*r


EDIT: @Arslan7041:
That has bad things in it.
Why should the diameter and chord be integers? The data example shows 10.17 as input for chord.

What are the lines 11 and 22? The line 13 does input as instructed.

The comment on line 24 ... line 13 reads into int variable. Any input that does not convert to int, "q" for example, will set the input stream into error state. While that definitely interrupts the loop, any further input would require clearing the error first. Educational, yes. Probably won't affect this program, but the surprise will hit in future assignments. Input is not as "simple" as it seems.
Last edited on
Thank you @Arslan7041 and @keskiverto

I know the area formula is messed up in putting it C++ and thanks to the resources i have an idea of what to do.
But I am still having trouble with area part, the way i set up my int of the area above, is that the right way to do it or i need multiple of ints for each numbers? THX
I suggest you to look what standard library has to offer: http://en.cppreference.com/w/cpp/numeric/math

Especially those:
http://en.cppreference.com/w/cpp/numeric/math/sqrt
http://en.cppreference.com/w/cpp/numeric/math/asin

I suggest to move logic for calculating segment area to function:
1
2
3
4
5
6
7
8
9
10
double segment_area(double chord, double radius)
{
    //calculations here. Your formula is correct, here it is again:
    //http://puu.sh/k5Dow/43de6113e2.png
}

double circle_area(double radius)
{
    //You will need it later, for percentage calculations.
}
the way i set up my int of the area above, is that the right way to do it or i need multiple of ints for each numbers?
Use a double to represent area. To translate your formula to C++, do what keskiverto said: change x^2 to x*x and change 0.5X to 0.5*X
Thank you! @MiiNiPaa and @dhayden
EDIT: @Arslan7041:
That has bad things in it.
Why should the diameter and chord be integers? The data example shows 10.17 as input for chord.

What are the lines 11 and 22? The line 13 does input as instructed.

The comment on line 24 ... line 13 reads into int variable. Any input that does not convert to int, "q" for example, will set the input stream into error state. While that definitely interrupts the loop, any further input would require clearing the error first. Educational, yes. Probably won't affect this program, but the surprise will hit in future assignments. Input is not as "simple" as it seems.


Ah, I see. Thank you for pointing out my mistakes.

I guess changing the data type of 'diameter' and 'chord' to string would make the quit option more viable.

Or better yet, leave the data types as doubles, and use -1 to break out of the loop.



Last edited on
Topic archived. No new replies allowed.