9 Replies - 629 Views - Last Post: 23 October 2012 - 10:36 AM Rate Topic: -----

#1 ZacCarlson   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Counting from zero to a number and a number to zero function question

Posted 22 October 2012 - 07:04 PM

I'm copying and pasting the part of my assignment I would like help on below. You will also see my code. My program runs fine. The only problem I'm having trouble with is the numbers. I'm assuming it is with my "--" and "++"?
Hopefully the additional numbers on the left don't cause confusion. They are automatically put into my program as I write it. If it is a problem, let me know and let me know how to change it. Thanks:

In this exercise, you will be writing and using three functions. The first counts from 0
to the parameter and prints the numbers out in a single line. The second function
counts down to 0 from the parameter. The last function should count between the
two parameters. If the first number is smaller than the second number, the function
should count up. If the first number is larger, the function should count down.
Once all three functions are written, write a main function that prompts the user for
two positive numbers, then uses the first for the count up and count down functions
and both numbers for the count between function. After doing this once, the program
should ask the user if they would like to quit or re-run the program.


 #include<iostream>
  8
  9 using namespace std;
 10
 11 int down ( int a ) {
 12    int z;
 13
 14    for ( z = 0; z <= a; z++ ) {
 15       cout << z;
 16       return (z);
 17    }
 18 }
 19 int up ( int a){
 20    int z;
 21
 22    for ( z = a; z >= 0; z-- ){
 23       cout << z;
 24       return (z);
 25    }
 26 }
 27 int count ( int a, int B)/>{
 28    int z;
 29
 30    if ( a < b ){
 31       for ( z = a; z <= b; z++){
 32          cout << z;
 33          return (z);
 34       }
 35    }
 36    else if ( a > b ){
 37       for ( z = a; z >= b; z--){
 38          cout << z;
 39          return (z);
 40       }
 41    }
 42 }
 43 int main (){
 44    char answer;
 45
 46    cout << "Do you want to continue (Y or N)?" << endl;
 47    cin >> answer;
 48
 49    if ( answer == 'N' || answer == 'n') return 0;
 50
 51    do{
 52       int a, b, upUp, downDown, countCount;
 53
 54       cout << "Enter one positive number: " << endl;
 55       cin >> a;
 56       cout << "Enter a second positive number: " << endl;
 57       cin >> b;
 58       downDown = down (a);
 59       cout << downDown << endl;
 60       upUp = up (a);
 61       cout << upUp << endl;
 62       countCount = count(a, B)/>;
 63       cout << countCount << endl;
 64       cout << "Do you wan to continue (Y or N)?" << endl;
 65       cin >> answer;
 66       if ( answer == 'N' || 'n') return 0;
 67    }
 68
 69    while ( answer!= 'N' || 'n' );
 70    return 0;
 71 }



Is This A Good Question/Topic? 0
  • +

Replies To: Counting from zero to a number and a number to zero function question

#2 Rahul69   User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 35
  • Joined: 10-October 12

Re: Counting from zero to a number and a number to zero function question

Posted 22 October 2012 - 08:44 PM

Dude ur code has quite a heck of problems :wheelchair: .Firstly regarding the line numbers: You must be using EDitor like(Visual studio), so while copying code u copy line no's too, the solution to this is to open .CPP file in notepad and then copy.
Regarding ur code:
In line 21(27) int count ( int a, int B){ u should use b instead of B as C++ is case sensitive.
In Functions: U are returning values, but there is no need as u are printing result in the function itself, so make all function of void type and Dont print values in main function in this code


In line 40(46) cout << "Do you want to continue (Y or N)?" << endl;
Don't take choice outside the while loop as it is required to be taken again and again unless the user says no.

Make all these corrections and see if it works well
PS: Hope I can help a lot!! :clap: :angel: :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#3 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Counting from zero to a number and a number to zero function question

Posted 22 October 2012 - 09:42 PM

The return statment
Was This Post Helpful? 0
  • +
  • -

#4 ZacCarlson   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: Counting from zero to a number and a number to zero function question

Posted 22 October 2012 - 11:16 PM

For the b part, it must have been a copy typo because the actual code has a "b". The print statement you made makes sense and I fixed it as you can see but it still does not work. I'll give an example of the output it should spit out. I'm guessing it should have a counter of some sort? Can somebody give me an example? Also, the function is supposed to continue to ask if they want to calculate new values until the user says no. My way works with no, but if you say Y, it also ends after the first run through. When you explain, please speak in c++ for dummies type of way.

Example Output:
Please enter 2 ints.

3 9
0 1 2 3
3 2 1 0
3 4 5 6 7 8 9

or

10 6
0 1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1 0
10 9 8 7 6



#include<iostream>

using namespace std;

int down ( int a ) {
    int z;

    for ( z = 0; z <= a; z++ ) {
       return (z);
    }
 }
 int up ( int a){
    int z;

    for ( z = a; z >= 0; z-- ){
       return (z);
    }
 }
 int count ( int a, int B)/>{
    int z;

    if ( a < b ){
       for ( z = a; z <= b; z++){
          return (z);
       }
    }
    else if ( a > b ){
       for ( z = a; z >= b; z--){
          return (z);
       }
    }
 }
 int main (){
    char answer;

    cout << "Do you want to continue (Y or N)?" << endl;
    cin >> answer;

    if ( answer == 'N' || answer == 'n') return 0;

    do{
       int a, b, upUp, downDown, countCount;

       cout << "Enter one positive number: " << endl;
       cin >> a;
       cout << "Enter a second positive number: " << endl;
       cin >> b;
       downDown = down (a);
       cout << downDown << endl;
       upUp = up (a);
       cout << upUp << endl;
       countCount = count(a, B)/>;
       cout << countCount << endl;
       cout << "Do you want to continue (Y or N)?" << endl;
       cin >> answer;
       if ( answer == 'N' || 'n') return 0;{
    }
    }
     while ( answer!= 'N' || 'n' );
     return 0;
 }


Was This Post Helpful? 0
  • +
  • -

#5 Xupicor   User is offline

  • Nasal Demon
  • member icon

Reputation: 457
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

Re: Counting from zero to a number and a number to zero function question

Posted 23 October 2012 - 02:16 AM

I'd suggest you think about some less confusing variable and function names. ;)
Another thing is std::endl - you can use "text\n" to add new-line character, which will be faster to write if you already are outputting a string, and may actually result in better performance, since endl also flushes the stream.

While it's not wrong to have multiple exit-ways out of a function (return statements) you'd do good to try to have only one. Not to say you should do it no matter what, readability should be your first rule. ;)

while ( answer!= 'N' || 'n' );

This is valid code (for the compiler), but does something different than you think. 'n' will always result in true. You want
do { ... } while (answer != 'N' && answer != 'n');
Notice && instead of || and think about it - why would I change it?

Also, this code just at the end of the loop is useless:
if ( answer == 'N' || 'n') return 0;{
    }


Even if it was proper - your loop already checks that, what's the point?

This post has been edited by Xupicor: 23 October 2012 - 09:01 AM

Was This Post Helpful? 1
  • +
  • -

#6 ZacCarlson   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: Counting from zero to a number and a number to zero function question

Posted 23 October 2012 - 08:28 AM

OK. But also, help me with understanding the number part I asked about. How would I do it etc?
Was This Post Helpful? 0
  • +
  • -

#7 Xupicor   User is offline

  • Nasal Demon
  • member icon

Reputation: 457
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

Re: Counting from zero to a number and a number to zero function question

Posted 23 October 2012 - 09:26 AM

There's this thing, you can call your variables seemingly whatever you like, but meaningful names will save you a lot of time when you revisit the code. (What was that a variable? And d?) ;)

All that said, however, by convention, when people name their iterators and have no other useful names, they tend to call them i, j, k, l... Depending on the loop "depth" level. Of course if you traverse an array you may want to use x, y, z, etc. Point is, i will be readily read out as an iterator variable, it rather as an iterator object of some standard library collection, etc. Even though these are single-letter variable names, if they're not misused - they are readable.

Using z is not wrong, but at least for me, and I guess not only for me, wouldn't be obvious as a "top level loop iterator" in longer code.

With C++11 we also tend to get into the naming habit of languages that already had foreach a long ago.
//C++11:
for (auto name: names) { }
for (auto car: cars) { }



Now, more on topic.

This is C++, so instead of:
int z;
for ( z = 0; z <= a; z++ ) { }
you can do:
for (int z = 0; z <= a; z++) { }
The difference is that in the second snippet z only lives in the loop body. If you don't understand that, then read about scope.

But to the point, you're not listening to us. At least not to jjl. ;) Read trough the link and tell us what's wrong in this picture:

int z;
for ( z = 0; z <= a; z++ ) {
    return (z);
}

Was This Post Helpful? 0
  • +
  • -

#8 ZacCarlson   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: Counting from zero to a number and a number to zero function question

Posted 23 October 2012 - 10:00 AM

Xupicor or DIC head, however you want to be called, the second name (nick name) goes with your attitude. These past two replies to help me have been a little bit better but advice from somebody who's probably older than you, learn how to speak to somebody when you try to help them. You did help me with the loop part and you do make sense. I did look at the link for the return statement and will work on it later. I'll keep you posted if there are any more issues. Thanks.
Was This Post Helpful? -4
  • +
  • -

#9 Xupicor   User is offline

  • Nasal Demon
  • member icon

Reputation: 457
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

Re: Counting from zero to a number and a number to zero function question

Posted 23 October 2012 - 10:14 AM

I don't think I deserved such a response, but if you feel so please do report my post. There's a button right there at the bottom of the post.

I may have added a bit much of - how should I call it - information on the side, not necessarily being the immediate solution of your problem, but I'd think these would actually be informative and useful for unseasoned programmer.

If you feel like you were treated badly by me, I'm sorry. I certainly didn't mean it.
Was This Post Helpful? 1
  • +
  • -

#10 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Counting from zero to a number and a number to zero function question

Posted 23 October 2012 - 10:36 AM

Quote

but advice from somebody who's probably older than you, learn how to speak to somebody when you try to help them


I think you need to learn to talk to people that are trying to help you. Calling someone names does not indicate "age" in any way to me, more like immaturity if you ask me. So before this goes any further I'm going to close this thread. If you want to appeal this closure feel free to report this post.

Jim
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1