Loops question

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Loops question
# 1  
Old 02-06-2014
Question Loops question

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

When the code is run only the first loop is utilized and it skips the following when it should move on after the first loop commands are not met



2. Relevant commands, code, scripts, algorithms:
while loop, else if


3. The attempts at a solution (include all code and scripts):
Code:
/*
*File: circles_solutions.cpp
*Created by: Robert Marsch
*Created on: 2/3/2014
*Synopsis: To input the coordinates of a circle and radii of three circles and *report the location of the query point relative to the circles.
*/

#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
  int xa, xb, xc, xq, xy, ya, yb, yc, yq, ra, rb, rc;
  int distancex1, distancey1, distancex2, distancey2, distancex3, distancey3;
  int d1, d2, d3;
  int k;

  cout << "Enter x and y coordinates of circle A (2 values): ";
  cin >> xa >> xb;
  cout<< "Enter radius of circle A: ";
  cin >> ra;

  cout << "Enter x and y coordinates of circle B (2 values): ";
  cin >> xb>> yb;
  cout << "Enter radius of circle B: ";
  cin >> rb;

  cout << "Enter x and y coordinates of circle C (2 values): ";
  cin >> xc >>  yc;
  cout << "Enter radius of circle C: ";
  cin >> rc;

  cout << "Enter x and y coordinate of query point (2 values): ";
  cin >> xq >> yq;
 
  distancex1 = (xq - xa);
  distancey1 = (yq - ya);
  distancex2 = (xq - xb);
  distancey2 = (yq - yb);
  distancex3 = (xq - xc);
  distancey3 = (yq - yc);

  d1 = sqrt(pow(distancex1,2)+pow(distancey1,2));
  d2 = sqrt(pow(distancex2,2)+pow(distancey2,2));
  d3 = sqrt(pow(distancex3,2)+pow(distancey3,2));

  

  while (int k=1)
 
    if(d1 && d2 && d3 <= ra && rb && rc)
    {
      cerr << "Circles A B and C contain point (" << xq << " ," << yq << "). " << endl;
      exit (10);
    }

    else if (d1 && d3 <= ra && rc && d2 >= rb)
    {
      cerr << "Circles A and C contain point (" << xq << " ," << yq << "). " << endl;
      exit(20);
    }

    else if (d3 >= rc && d2 && d2 <= ra && rb)
    {
      cerr << "Circles A and B contain point (" << xq << " ," << yq << "). " << endl;
      exit(30);
    }

  else if (d1 >= ra && d2 <= rb && d3 <= rc)
    {
      cerr << "Circles B and C contain point (" << xq << " ," << yq << "). " << endl;
      exit(40);
    }

  else if (d2 && d3 >= rb && rc && d1 <= ra)
    {
      cerr << "Circle A contains point (" << xq << " ," << yq << "). " << endl;
      exit(50);
    }

  else if (d1 && d3 >= ra && rc && d2 <= rb)
    {
      cerr << "Circle B contains point (" << xq << " ," << yq << "). " << endl;
      exit(60);
    }

  else if (d1 && d3 >= ra && rc && d2 <= rb)
    {
      cerr << "Circle C contains point (" << xq << " ," << yq << "). " << endl;
      exit(70);
    }
   
  else if (d1 && d2 && d3 >= ra && rb && rc)
    {
      cerr << "No circle contains point (" << xq << " ," << yq << "). " << endl;
      exit(80);
    }
 
return 0;
 
}


4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Ohio State, Columbus, OH, USA, Sharief, CSE1222

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Last edited by TwistedKitty; 02-06-2014 at 10:07 PM.. Reason: Add CODE tags.
# 2  
Old 02-06-2014
Well I only see one loop...

First you request and obtain some values. This code is not in a loop and will only happen one time. Then we encounter while (int k=1) which is an infinite loop. It will never stop. That is sometimes ok, but probably not a great idea here. Normally a while loop would have braces to show the statements it is controlling. You have no braces, that is legal, so only one statement is controlled by your infinite loop. It is a lengthy if statement with many "else if" causes.

I guess one of the tests tested true. For each test you output something and then call exit(). A call to exit() terminates the program. So if you see any output, we expect the program to hit exit() and well, exit.

Suppose none of the tests were true. Then your infinite loop would run the same tests on the same data. If none of the tests were true the first time, they will still not be true the second time. Or the third time. Or the fourth time.... This is not a good thing.

Your infinite loop will never finish. But if it did, you would execute your "return" statement. Returning from main() is legal and will also terminate the program. Right now you can't reach that return statement.

Fair warning, I'm tired and I just glanced at your code. I haven't tried it. I could be wrong. But I don't think so. Good night... going to bed. Smilie
This User Gave Thanks to Perderabo For This Post:
# 3  
Old 02-08-2014
Code:
int k=1;
while (k==1)

{
 
    if(d1 && d2 && d3 <= ra && rb && rc)
    {
      cerr << "Circles A B and C contain point (" << xq << " ," << yq << "). " << endl;
      exit (10);
    }

    else if (d1 && d3 <= ra && rc && d2 >= rb)
    {
      cerr << "Circles A and C contain point (" << xq << " ," << yq << "). " << endl;
      exit(20);
    }

    else if (d3 >= rc && d2 && d2 <= ra && rb)
    {
      cerr << "Circles A and B contain point (" << xq << " ," << yq << "). " << endl;
      exit(30);
    }

  else if (d1 >= ra && d2 <= rb && d3 <= rc)
    {
      cerr << "Circles B and C contain point (" << xq << " ," << yq << "). " << endl;
      exit(40);
    }

  else if (d2 && d3 >= rb && rc && d1 <= ra)
    {
      cerr << "Circle A contains point (" << xq << " ," << yq << "). " << endl;
      exit(50);
    }

  else if (d1 && d3 >= ra && rc && d2 <= rb)
    {
      cerr << "Circle B contains point (" << xq << " ," << yq << "). " << endl;
      exit(60);
    }

  else if (d1 && d3 >= ra && rc && d2 <= rb)
    {
      cerr << "Circle C contains point (" << xq << " ," << yq << "). " << endl;
      exit(70);
    }
   
  else if (d1 && d2 && d3 >= ra && rb && rc)
    {
      cerr << "No circle contains point (" << xq << " ," << yq << "). " << endl;
      exit(80);
    }
 
 }

The loop does not exit because k is never changed (or referenced) inside the loop. Your code had several problems. Note the read areas. You MUST COMPILE with WARNINGS enabled! Correct code has ZERO warnings. At a minimum, assuming the logic is correct. The code you posted could not have compiled without warnings. You still need to set k to zero somewhere in the loop code.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Help with loops?

I'm trying to understand better the while and until loops, can someone help me with this example? #!/bin/bash # Listing the planets. for planet in Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto do echo $planet # Each planet on a separate line. done echo; echo for... (3 Replies)
Discussion started by: jose2802
3 Replies

2. Homework & Coursework Questions

If and Loops

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: In this script you will take a directory as input from the user and change the end of line sequence from a Unix... (1 Reply)
Discussion started by: Pcarson
1 Replies

3. Shell Programming and Scripting

Loops

Hi All, I am very new to Shell scripting. I read basic scripting manual. But i didn't understand the code. Please tell the meaning of the below code: while getopts "F:f:R:r:C:c:" opt 2>/dev/null do case ${opt} in F|f) FREQUENCY_MODE=$OPTARG;; ... (3 Replies)
Discussion started by: pdathu
3 Replies

4. UNIX for Dummies Questions & Answers

Bourne-sh (not bash) question about nested loops and sed

Here's the input: alpha, numeric or alphanumeric string ("line 1 string") numeric string ("line 2 string") numeric string ("line 3 string") numeric string ("line 4 string") ... where - each numeric string is in a pattern that can be matched with RE but - there can be any number of... (2 Replies)
Discussion started by: uiop44
2 Replies

5. Shell Programming and Scripting

loops

Hi All I have some directories on our server which are containing .csv files. i need to print value of cell "B2" from those csv files. Please advise. I have tried head command as example: head -2 */Book_Collection_Report_1_-_Collection_Requests_trials.csv | sed -n "3p" | awk -F","... (4 Replies)
Discussion started by: yash1978
4 Replies

6. Shell Programming and Scripting

Help with loops

I am trying to do ftp of some crt files that have spaces in the file names. I have to do approximately 4500 files ftp'ed and then check whether the trasnfered file size matches the source files. This has to be in a loop I mean 1. ftp the file, check for file size whether the source and target... (0 Replies)
Discussion started by: dsravan
0 Replies

7. Shell Programming and Scripting

Help with the 2 for loops

#!/bin/bash IFS=$'\n' A= a c b t g j i e d B= t y u i o p counter=0 found="" for i in $(cat $A) do for j in $(cat $B) do if then found="yes" fi done if then (1 Reply)
Discussion started by: vadharah
1 Replies

8. Shell Programming and Scripting

ksh question, loops

i want to add about 60 printers using a ksh script. i am having trouble though, i am reading the input from the hosts file and using the lpadmin command to add like so: lpadmin -p -v /dev/null -m netstandard -o dest= i want printername and ipaddy to come from the hosts file, i am having... (1 Reply)
Discussion started by: BG_JrAdmin
1 Replies

9. UNIX for Dummies Questions & Answers

While Loops

I'm trying to create a loop that will prompt the user for 15 values, not forcing them to enter all 15. If the user enters through one or more of the prompts the null value needs to be converted to 0, otherwise set the parameter = to the value entered: ex. Please enter file no #1: 17920 ... (4 Replies)
Discussion started by: vdc
4 Replies

10. UNIX for Dummies Questions & Answers

While loops

Hi all, I am an amateur k shell scripter and came across something I need clarification on. while ; do various commands done What is the "" condition testing for in the above while loop? THx, Bookoo (5 Replies)
Discussion started by: bookoo
5 Replies
Login or Register to Ask a Question