I have this math problem which requires to find if a point lies in all the circles... I have almost all the code but it has one problem that is it checks which point lies in each circle and not if one point lies in all circles.. I think I need to do a minor change but I can't figure it out...
#include<iostream>
#include<stdlib.h>
#include<math.h>
int main()
{
using namespace std;
system("chcp 1251");
int k, m, c, ic;
float x[12], y[12], r[12], xp[12], yp[12], d;
do
{
cout << "Enter how many circles (1..12): ";
cin >> k;
}
while (k < 1 || k > 12);
for (c = 0; c < k; c++)
{
cout << "Enter coordinates of circles № " << 1 + c << endl;
cout << "x= "; cin >> x[c];
cout << "y= "; cin >> y[c];
cout << "r= "; cin >> r[c];
}
do
{
cout << "Enter how many points (1..20): ";
cin >> m;
} while (m < 1 || m > 20);
cout << "Enter coordinates of points:" << endl;
for (c = 0; c < m; c++)
{
cout << "Point № " << 1 + c << endl;
cout << "x= "; cin >> xp[c];
cout << "y= "; cin >> yp[c];
}
for (c = 0; c < k; c++)
{
cout << "Points in circle № " << 1 + c << ": ";
for (ic = 0; ic < m; ic++)
{
d = sqrtf((x[c] - xp[ic]) * (x[c] - xp[ic]) + (y[c] - yp[ic]) * (y[c] - yp[ic]));
if (d < r[c] )
cout << 1 + ic << " ";
}
cout << endl;
}
system("pause");
return 0;
}
I think I just need to change the if.. so if anyone can help me I would appreciate.
after all if you allow to enter 20 point your arrays xp and yp should have a size of 20, another advice if it's not a school work is using the structers, for solving your problem this is the code for fix it
bool b;
for (ic = 0; ic < m; ic++)
{
b=true;
for (c = 0; c < k; c++)
{
d = sqrtf((x[c] - xp[ic]) * (x[c] - xp[ic]) + (y[c] - yp[ic]) * (y[c] - yp[ic]));
b=b&&(d < r[c]);
}
if(b)
cout << "This point lies "<< 1 + ic << " ";
cout << endl;
}
Testing one point against many circles:
bool inCircle=true;
for (c = 0; c < k; c++)
{
d = sqrtf((x[c] - xp[ic]) * (x[c] - xp[ic]) + (y[c] - yp[ic]) * (y[c] - yp[ic]));
if (d > r[c])
{
inCircle = false;
cout << "the point is outside circle " << c << endl;
break; // this is not strictly needed
}
}
if(inCircle)
cout << "the point is inside all of the circles" << endl;
Once you have this working perfectly, you can put a loop around it to test many points.
Related
This code solves the program : Create an algorithm in the form of a flowchart, write and debug the task using recursive and ordinary functions. Corresponding results.
What is dot used for here "x = (1. / 2) * (f + (a / f));" ?
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int k, e, x, a, d, n, f, s;
d = 1;
do {
e = 1;
do {
cout << "Press 1 to use recurrent solution method, press 2 to use nun recurrent solution
method" << endl;
cin >> k;
if (k == 1 || k == 2) e = 2;
} while (e == 1);
switch (k)
{
case 1:
{
cout << "Enter the numder a:" << endl;
cin >> a;
cout << "Enter the numder n:" << endl;
cin >> n;
x = (1. / 2)*(1 + a);
f = x;
s = f;
for (int i = 1; i < n; i++)
{
x = (1. / 2) * (f + (a / f));
s += x;
f = x;
}
cout << "Result: " << s << endl;
}
break;
case 2:
{
cout << " Enter the numder a:" << endl;
cin >> a;
x = sqrt(a);
cout << "Result: " << x << endl;
}
break;
}
cout << "Press 1 to repeat!" << endl;
cin >> d;
} while (d == 1);
}
The dot in "x = (1. / 2) * (f + (a / f));" is used for assuring that the division is interpreted as a float division instead of an integer division, so the result of (1/2) is 0.5 instead of 0.
I am working on a program in which I have a function and I am hoping to time it. Currently, I considered clock_t in the ctime module to be useful, but when I entered the code for timing, I got (I used Dev-C++ to edit) "permission denied". Below is my code:
#include <iostream>
#include <vector>
#include <ctime>
// Initialization
const int maxN = 99999;
int tokens[maxN] = {}; // All the tokens at mission 1
bool use[maxN] = {}; // Status of the tokens (used or not used)
int bag[maxN]; // Bag to put the tokens into
using namespace std;
int ansIdx = 1; // Answer number
char menu() {
// Print menu
cout << "0: terminate\n";
cout << "1: mission 1 - permutations from 1 ~ N\n";
cout << "2: mission 2 - permutations from input\n";
// Ask for input
cout << "Please input a choice (0 ~ 2): ";
char choice;
cin >> choice;
// Valid command?
if (choice >= '0' and choice <= '2') {
return choice;
}
return '\0';
}
int permutations(int depth, int n, int l) {
if (depth == n) {
// Arranged all elements, so cout each of the elements.
cout << "[" << ansIdx << "] ";
for (int i = 0; i < n; i++) cout << bag[i] << " ";
cout << "\n";
ansIdx++;
return l;
}
for (int i = 0; i < n; i++) {
// Else, for each UNUSED element at this layer:
if (use[i] == 0) {
// Put it into bag[i].
bag[depth] = tokens[i];
// Set the status of it used.
use[i] = 1;
// Call recursion.
permutations(depth + 1, n, l+1);
// Backtrack.
use[i] = 0;
}
}
}
int main() {
// Print menu.
cout << "PERMUTATION GENERATOR\n";
char executionMode;
while (executionMode != '0') {
executionMode = menu();
while (executionMode == '\0') {
executionMode = menu();
}
int layers;
clock_t start;
double ms;
switch (executionMode) {
case '1':
cout << "Enter N: ";
int N;
cin >> N;
for (int i = 0; i < N; i++) {
tokens[i] = i + 1;
}
start = clock();
layers = permutations(0, N, 0);
ms = ((double)(clock() - start)) / CLOCKS_PER_SEC;
cout << "L = " << layers << "\n";
cout << "Mission 1: " << ansIdx - 1 << " permutations\n";
cout << "Used: " << ms << "ms\n";
ansIdx = 1;
break;
case '2':
int M = 0;
while (M < 2 or M > 9) {
cout << "Enter length of input (2 ~ 9): ";
cin >> M;
}
for (int i = 0; i < M; i++) {
cout << "Enter a number: ";
cin >> tokens[i];
}
start = clock();
layers = permutations(0, M, 0);
ms = ((double)(clock() - start)) / CLOCKS_PER_SEC;
cout << "L = " << layers << "\n";
cout << "Mission 2: " << ansIdx - 1 << " permutations\n";
cout << "Used: " << ms << "ms";
ansIdx = 1;
break;
}
}
return 0;
}
Can anyone help me? Any suggestions are appreciated.
I am trying to make this shape from the code below. I'm confused as to how to make it print the 2nd row, second to last star without it skipping and printing the extra space before printing the star. Once that is figured out would the bottom half, when the stars expands back out, would the code be similar to the top half? I have tried a couple combinations of code between c and r but I have been stuck with what I currently.
---------------------- //row 0
* *| //row 1
* * * *| //row 2
* * * * * *|
* * * * * * * *|
* * * * * * * * * *|
* * * * * * * * * * *|
* * * * * * * * * *|
* * * * * * * *|
* * * * * *|
* * * *|
* *|
----------------------
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main() {
cout << "Enter a positive odd number less than 40: ";
int num = 0;
int z = 1;
for (int a = 0; a < 3; ++a)
{
cin >> num;
if (num < 38 && num > 0 && num % 2 == 1)
{
cout << "Thank you!" << endl << endl;
for (int r = 0; r < num; ++r) //outer loop/rows
{
for (int c = 0; c < num; ++c) //inner loop/columns
{
if (r == 0) cout << "--"; //top of square
else if (c >= r + r - c && c < num - 1)
cout << " ";
//else if (c == num - 1) cout << "*|";
else if (r == num - 1) cout << "--"; //bottom of square
else if (c == num - 1) cout << "*|"; //right side of square
else if (r > c) cout << "* ";
}
cout << endl;
}
break;
}
else cout << "Please enter a positve odd number that is less than 40!" << endl;
}
cout << endl;
}
I just took two variables left=0 & right=num-1 and increased left & decreased right till r<=num/2, after that i reversed the process,when the col <= left or col >=right I printed *.
I hope it will be easy to understand.
Here is the code:
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main() {
cout << "Enter a positive odd number less than 40: ";
int num = 0;
int z = 1;
for (int a = 0; a < 3; ++a)
{
cin >> num;
if (num < 38 && num > 0 && num % 2 == 1)
{
cout << "Thank you!" << endl << endl;
int left=0,right=num-1;
//for printing top line
for(int i = 0; i < num; i++) cout<<"- ";
cout<<"-"<<endl;
for (int r = 0; r < num; ++r) //outer loop/rows
{
//printing columns
for(int c = 0; c < num; c++)
{
if(c <= left || c >= right)
cout<<"* ";
else
cout<<" ";
}
if(r >= num/2) //checking for half of the rows
{
left--;right++;
}
else
{
left++;right--;
}
cout<<"|"<<endl;
}
//for printing last additional line
for(int i = 0; i < num; i++) cout<<"- ";
cout<<"-"<<endl;
break;
}
else cout << "Please enter a positve odd number that is less than 40!" << endl;
}
cout << endl;
}
This approach does it the math way.
Furthermore it draws a full frame with plus-chars at the edges.
Give it a try.
#include <iostream>
#include <cmath>
using std::cout; using std::cin; using std::endl;
int main() {
cout << "Enter a positive odd number less than 40: ";
int num = 0;
int z = 1;
for (int a = 0; a < 3; ++a) {
cin >> num;
if (num < 40 && num > 0 && num % 2 == 1) {
cout << "Thank you!" << endl << endl;
int center = ceil(num / 2.0);
for (int r = 0; r <= num+1; ++r) { //outer loop/rows
for (int c = 0; c <= num+1; ++c) { //inner loop/columns
if (r == 0 || r == num+1) {
if (c == 0 || c == num+1)
cout << "+"; // corner
else
//top or botton of square between corners
if (c == center)
cout << "-";
else
cout << "--";
}
else if (c == 0 || c == num+1) {
cout << "|"; // left or right frame
} else {
// inner part
if ((center-std::abs(center-r)) >= center-std::abs(center-c))
if (c < center)
cout << "* ";
else if (c > center)
cout << " *";
else
cout << "*";
else
if (c == center)
cout << " ";
else
cout << " ";
}
}
cout << endl;
}
} else
cout << "Please enter a positve odd number that is less than 40!" << endl;
}
cout << endl;
}
Just another way (with some more user input checking):
#include <iostream>
#include <string>
#include <limits>
#include <sstream>
using std::cout;
using std::cin;
using std::string;
const auto ssmax = std::numeric_limits<std::streamsize>::max();
const int max_dim = 40;
const int max_iter = 3;
int main() {
cout << "Enter a positive odd number less than " << max_dim << ": ";
int num = 0, counter = 0;
while ( counter < max_iter ) {
cin >> num;
if ( cin.eof() )
break;
if ( cin.fail() ) {
cout << "Please, enter a number!\n";
cin.clear();
cin.ignore(ssmax,'\n');
}
if ( num < max_dim && num > 0 && num % 2 ) {
cout << "Thank you!\n\n";
//top line
string line(num * 2, '-');
cout << line << '\n';
for ( int r = 0, border = num - 1; r < num; ++r ) {
cout << '*';
for ( int c = 1; c < num; ++c ) {
if ( (c > r && c < border) || (c < r && c > border) )
cout << " ";
else
cout << " *";
}
// right border
cout << "|" << '\n';
--border;
}
//bottom line
cout << line << '\n';
++counter;
} else {
cout << "Please, enter a positive odd number that is less than 40!\n";
}
}
cout << std::endl;
}
Or my favorite:
// top line
string line = string(num * 2, '-') + '\n';
cout << line;
// inside lines
int r = 0, border = ( num - 1 ) * 2;
string inside = string(border + 1, ' ') + "|\n";
// top
while ( r < border ) {
inside[r] = '*';
inside[border] = '*';
r += 2;
border -= 2;
cout << inside;
}
// center line
inside[r] = '*';
cout << inside;
// bottom
while ( border > 0 ) {
inside[r] = ' ';
inside[border] = ' ';
r += 2;
border -= 2;
cout << inside;
}
//bottom line
cout << line;
Hello Im new to c++ and im working on a project
and i keep getting this error when im compiling
main.cpp:3:1: error: expected unqualified-id before ‘do’
do
^
here's the code
int a,b,i,j,sum=0;
do
{ cout << "Enter a number: ";
cin >> a;
if (a < 4 || a > 1000000)
{ cout << "Input must be between 4 and 1000000 inclusive." << endl;
}
}while (a < 4 || a > 1000000);
do
{ cout << "Enter a second number: ";
cin >> b;
if (b < 4 || b > 1000000)
{ cout << "Input must be between 4 and 1000000 inclusive." << endl;
}
}while (b < 4 || b > 1000000);
if (a > b)
{ int hold;
hold = b;
b = a;
a = hold;
}
cout << "The prime numbers between " << a << " and " << b << " inclusive are: " << endl;
//int sum;
for (i = a; i <= b; i++)
{
for (j = 2; j <= i; j++) // Changed the < to <=, and got rid of semicolon
{
if (!(i%j)&&(i!=j)) break;
if (j==i)
{
cout << i << endl;
sum += i;
cout << sum ;
}
}
}
Much like said before, your code does need to be in a function. Try wrapping it with
int main () {
//your code here
}
I am trying to make a program that calculates the baseball player averages , and keeps asking until it reaches the user set amount of players. I have to then output the data into a graph that has the players batting average with a * marking where it falls into.
This is what I have so far:
#include <iostream>
#include <string>
using namespace std;
class BattingInfo
{
public:
char fname[25];
char lname[25];
float hits;
float battimes;
float games;
float tgames;
float average;
float averaget;
float gaverage;
};
bool getd(BattingInfo& bi);
void displayd(BattingInfo& bi);
int main()
{
int players;
BattingInfo bi[9999];
cout <<"Please input the number of players:\n";
cin >> players;
int index;
index = 0;
while (getd(bi[index])&& index < players - 1)
{
index++;
}
cout << "\n\nData Entries: \n";
for (int i = 0; i <= index; i++)
{
displayd(bi[i]);
}
}
bool getd(BattingInfo& bi)
{ cout <<"Please input the Players first name:\n";
cin >> bi.fname;
cout <<"Please input the Players last name:\n";
cin >> bi.lname;
cout << "Please input the number of number of successful hits of the player.\n";
cin >> bi.hits;
cout << "Please input the numer of times at bat.\n";
cin >> bi.battimes;
cout << "Please input the number of games the player participated in.\n";
cin >> bi.games;
cout <<"Please input the total numer of games the player could have batted in.\n";
cin >> bi.tgames;
system("cls");
return true;
}
void displayd(BattingInfo& bi)
{
bi.average = bi.hits/bi.battimes;
bi.gaverage = bi.games/bi.tgames;
bi.averaget = bi.average*bi.gaverage;
cout <<"Batting Average: " << bi.averaget << endl;
cout << ".000 - .099";
if (bi.averaget > .0 && bi.averaget < .1)
{ cout << "*";}
cout << "\n.100 - .199";
if (bi.averaget > .099 && bi.averaget < .2)
{ cout << "*";}
cout << "\n.200 - .299";
if (bi.averaget > .199 && bi.averaget < .3)
{ cout << "*";}
cout << "\n.300 - .399";
if (bi.averaget > .299 && bi.averaget < .4)
{ cout << "*";}
cout << "\n.400 - .499";
if (bi.averaget > .399 && bi.averaget < .5)
{ cout << "*";}
cout << "\n.500 - .599";
if (bi.averaget > .499 && bi.averaget < .6)
{ cout << "*";}
cout << "\n.600 - .699";
if (bi.averaget > .599 && bi.averaget < .7)
{ cout << "*";}
cout << "\n.700 - .799";
if (bi.averaget > .699 && bi.averaget < .8)
{ cout << "*";}
cout << "\n.800 - .899";
if (bi.averaget > .799 && bi.averaget < .9)
{ cout << "*";}
cout << "\n.000 - .999";
if (bi.averaget > .899 && bi.averaget < .1)
{ cout << "*";}
cout << "\n1";
if (bi.averaget > .999)
{ cout << "*\n";}
system("\nPAUSE");
system("cls");
}
It kind of works, but the graph is outputted for each player (with only 1 players average on it), I need it to output for the whole team on one graph. Im not sure how to do that.
Here is a very basic graph that displays values from left to right. You could use this as a base and alter it to display your data. If this isn't what you are looking for, you might want to use a proper GUI library.
Note my use of vector<> instead of arrays. Don't use arrays unless you have to. Use vector.
I'm guessing this might be a school project, so finishing this yourself would probably be best.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void display_as_graph(vector<int>& numbers)
{
char space = ' ';
for(int i = 0; i < numbers.size(); i++)
{
for(int num_spaces = 0; num_spaces < numbers[i]; num_spaces++)
{
cout << space;
}
cout << "x\n";
}
}
int main()
{
vector<int> numbers;
numbers.push_back(4);
numbers.push_back(6);
numbers.push_back(8);
numbers.push_back(3);
display_as_graph(numbers);
string wait;
cin >> wait;
return 0;
}