GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Oct-2009, 08:43
TitanGuy TitanGuy is offline
New Member
 
Join Date: Oct 2009
Posts: 29
TitanGuy is on a distinguished road

Combining While Loops and If Statements


Please help. New assignment and I am totally lost on this. I was able to complete my last assignment, so thank you for everyone's help.

PLease see attached assignment.

Thank you in advance.
Attached Images
File Type: pdf Assignment_17.pdf (72.7 KB, 14 views)
  #2  
Old 18-Oct-2009, 09:13
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,031
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: New assignment - Lost on.


Show us your work so far.
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 18-Oct-2009, 11:58
TitanGuy TitanGuy is offline
New Member
 
Join Date: Oct 2009
Posts: 29
TitanGuy is on a distinguished road

Re: New assignment - Lost on.


Here is what I have so far, I am stuck with the "control "z"" break ordeal, and when the number is "out of range", how do you keep it from counting the out of range number in the equation.
Also, if you don't know how many numbers is going to be included, how do you do the The range is the difference between the lowest and the highest number?
CPP / C++ / C Code:
int main()
{
double n, count=0, sum=0;
for (;;)
{
	cin >> n;
	if (n <0 || n >100) cout << "out of range; ignored." << endl;
	if (n <0 || n >100) cin >> n;
	if (n <= 0) break;
	++count;
	sum += n;
}
cout << "The average is " << float (sum)/count << endl;
}

  #4  
Old 18-Oct-2009, 12:47
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: New assignment - Lost on.


Quote:
Originally Posted by TitanGuy
..."control "z"" break ordea
With most compilers on Windows platforms a "Ctrl-z" input from the console gives an end-of-file condition for the cin stream. See Footnote.

The cin stream can be tested for "eof" or other error conditions and the loop can be made dependent on the results.

Here's a way to create a loop that terminates whenever a console "eof" character is entered. In fact the loop terminates whenever anything other than a numeric value is entered by the user:
CPP / C++ / C Code:
    double x;


    while (cin >> x) { // the condition is false if eof or anything non-numeric is entered

        //
        // Do whatever you need to do inside the loop
        //

    }

Quote:
Originally Posted by TitanGuy
.
and when the number is "out of range", how do you keep it from counting the out of range number in the equation.
You write a program that doesn't count anything when it's out of range:

CPP / C++ / C Code:
    int num;
    double x;


    num = 0;
    while (cin >> x) {
        if ((x < 0) || (x > 100)) {
            cout << "Value " << x 
                 << " is out of range, and is ignored." << endl;
        }
        else {
            ++num;
            // do whatever you need to do with valid values
        }
    }
    cout << "The number of valid values is " << num << endl;
    //
    // Do whatever you need to do after the loop.
    //

Quote:
Originally Posted by TitanGuy
.
Also, if you don't know how many numbers is going to be included, how do you...
I would do it the same way that I would if I knew in advance how many numbers there would be:

Code:
1. Before you enter the loop: Declare variables to hold minimum and maximum values, say xmin and xmax, respectively 2. The first time through the loop: Set xmin and xmax both equal to the current input value 3. Other times through the loop: If the current value is less than xmin, set xmin equal to the current value If the current value is greater than xmax, set xmax equal to the current value.


Regards,

Dave


Footnote:

On Linux systems or other systems using the Bash shell, Ctrl-d is the console "eof" character. Ctrl-z is intercepted by the shell before it gets to your program and causes the process to go to background mode, so the assignment simply could not be carried out without a lot of other system-dependent stuff (setting the console to raw mode, for example).

I think it's important for people to understand facts like this: defining behavior from console programs is operating-system dependent, compiler-dependent, and, in fact, shell-dependent. But I guess that's too much information for a beginning program class. Anyhow, I just thought I would mention it. Some day, some way, this may mean something to you, and, in fact may be important. Or, maybe, not. Oh, well...
Last edited by davekw7x : 18-Oct-2009 at 13:28.
  #5  
Old 18-Oct-2009, 13:58
TitanGuy TitanGuy is offline
New Member
 
Join Date: Oct 2009
Posts: 29
TitanGuy is on a distinguished road

Re: New assignment - Lost on.


I am still confused. I made the changes you said, and it still doesn't work. Also, I don't get your last statement at all concerning the min and max
  #6  
Old 18-Oct-2009, 14:16
TitanGuy TitanGuy is offline
New Member
 
Join Date: Oct 2009
Posts: 29
TitanGuy is on a distinguished road

Re: New assignment - Lost on.


Here is what I have now, I still have the problem with the
" if you don't know how many numbers is going to be included, how do you do the The range is the difference between the lowest and the highest number?"



CPP / C++ / C Code:
#include <iostream>

using namespace std;
//Write a program that reads a series of numbers (doubles) from the user, then prints the mean and the
//range.
//Notes:
//• You do not know ahead of time how many numbers will be in the list.
//• When you want to stop entering numbers, enter control-z.
//• The range is the difference between the lowest and the highest number.
//• The numbers will be in the range of 0.0 to 100.0. Ignore any numbers outside of this range.
int main()
{
double n, count=0, sum=0;
for (;;)
{
	if( cin.eof( ) ) break;
	while (cin >> n)
		if (n <0 || n >100){
			cout << "out of range; ignored." << endl;
		}
		else {
			++count;
			sum += n;
		}
}
cout << "The average is " << float (sum)/count << endl;

}
  #7  
Old 18-Oct-2009, 19:37
TitanGuy TitanGuy is offline
New Member
 
Join Date: Oct 2009
Posts: 29
TitanGuy is on a distinguished road

Re: New assignment - Lost on.


Anyone? Need help please.
  #8  
Old 18-Oct-2009, 20:21
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: New assignment - Lost on.


Quote:
Originally Posted by TitanGuy
Anyone? Need help please.
Give us time. We have lives outside of this board...

Read the first number.
Set min and max to this value.
Then read the rest of the numbers replacing min/max as necessary.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #9  
Old 18-Oct-2009, 20:39
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: New assignment - Lost on.


Quote:
Originally Posted by TitanGuy
Here is what I have now
I showed you how to make a loop. What the heck do you think your other stuff does? See Footnote.
CPP / C++ / C Code:
#include <iostream>

using namespace std;

int main()
{
    double n, count = 0, sum = 0;
    while (cin >> n) {
        if (n < 0 || n > 100) {
            cout << "out of range; ignored." << endl;
        }
        else {
            ++count;
            sum += n;
        }
    }
    cout << "The average is " << float (sum) / count << endl;

    return 0;
}
Quote:
Originally Posted by TitanGuy
Also, I don't get...

Test the program and make sure the loop works the way you expect it to work.

Then go back to my previous post.

Write code that implements step 1.
Write code that implements step 2.
Write code that implements step 3.

Code for steps 2 and 3 go inside the loop at the part where the current input has been validated. By "first time" I mean to do step 2 if count is equal to zero. This sets xmax and xmin equal to the first valid value.

If count is not equal to zero, then do the comparisons to see whether xmin has to be changed and whether xmax has to be changed.

Regards,

Dave

Footnote: The program that you posted works for me (with various compilers from Microsoft, Borland and GNU) on my Windows xp workstation as long as I follow instructions. (That is, I enter a bunch of numbers and then ctrl-z, it gives the expected results.) My example in this post just eliminated the superfluous, useless stuff that you put around the part that you need. In fact, the superfluous useless stuff that you added can make the program get "hung up" if the user enters a non-numeric something. That does not happen with my example. I'm not sure what, exactly, your assignment was.

Anyhow...

What compiler are you using? What operating system? What, exactly, doesn't work? In other words, what input did you give it and what did the program do? (Or, what did it not do that it should have done?)
Last edited by davekw7x : 18-Oct-2009 at 21:21.
  #10  
Old 18-Oct-2009, 21:29
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: New assignment - Lost on.


Quote:
Originally Posted by WaltP
Give us time....
Indeed, in some quarters it is considered downright rude to bump one's thread. (Also not very effective in getting a more timely response, I'm thinking.)

Oh, wait...It's even in the Guidelines: for posting requests for help


Quote:
Originally Posted by WaltP
8: Don't bump your thread. Most of us have lives outside of the forum...

I knew that sounded familiar! (Yes, I did read the Guidelines, once upon a time, and I appreciate your efforts.)

Oh, well...


Regards,

Dave
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Circular Linked Queue Copy Constructor and Assignment Operator Not Working? wc3promet C++ Forum 1 06-Oct-2008 03:00
Assignment Operator and Pointer Peter_APIIT C++ Forum 14 25-Sep-2007 05:36
double concatenated dynamic list - different type assignment warning dlp C Programming Language 11 01-Jun-2007 07:59
Am I reading this assignment correctly? earachefl C++ Forum 2 09-May-2006 09:39
Not sure I understand this assignment.... earachefl C++ Forum 7 05-May-2006 16:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:13.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.