Alternative way for void type help!

This is a discussion on Alternative way for void type help! within the C++ Programming forums, part of the General Programming Boards category; Well...This is my code. The question for the tutorial is Rewrite main so that it tests all the number from ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    65

    Alternative way for void type help!

    Well...This is my code. The question for the tutorial is

    Rewrite main so that it tests all the number from 2 to 20 and prints out the results, each on a separate line. (Hint: use for loop, with i running from 2 to 20.

    Code:
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    void num(int n);
    
    int main()
    {
        int m;
        num(m);
    
        return 0;
    }
    
    void num(int n)
    {
        int i;
        int m = 20;
        for(i = 2; i <= m; i++)
            cout<< i << endl;
    }
    I know it does not do what is asked. But here I use void type, and my question if it is possible to use the type integer or something else to do this exercise?
    Last edited by kenryuakuma; 09-26-2008 at 07:58 AM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    The program does not do what is asked.
    It starts at 1 not 2,
    It does not 'test' the numbers in any way,
    It does not output each one on a seperate line.

    Your code has unnecessary variables which it ignores and then uses a hard coded values.

    Your question doesn't make sense. There's nothing wrong with a function returning void. Until you can get it straight in your own mind why you would want to do otherwise, and can then ask a complete question that actually makes sense, it does not make sense to change it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    Oop! Sorry and forget to re-read the question before doing the exercise, and I suppose to change the value before I posted here.

    But anyway, how can you test the number? This is what baffles me.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    I don't know what "test the number" actually means. Got any better description?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    Well...I have been following through the tutorial book as this is an prerequisite before you really go into programming, and hence the question above is derived from the book. I am now learning function through the book but it doesn't really give a clear description or explanation about functions. For instance, the different name for the parameter that I just asked for help. It just gives some of the example and a little info about how the functions work.

    The above question is a complete question from the book and I guess "test the number" means to test 2 - 20 to see if they are integer by using a function.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Is that the main you're supposed to rewrite? (I'm starting to grasp at straws here.)

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    Quote Originally Posted by tabstop View Post
    Is that the main you're supposed to rewrite? (I'm starting to grasp at straws here.)
    I guess yet? Could you help by giving an example?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kenryuakuma View Post
    I guess yet? Could you help by giving an example?
    That's very hard based on your description of the problem. Testing if a number is an integer when the variable is an int in the first place is pretty meaningless - it always is.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by kenryuakuma View Post
    I guess yet? Could you help by giving an example?
    He wanted to know if you copied the code you posted from the tutorial book. You said you're supposed to rewrite main. But the main that's there doesn't make any sense at all. It would be strange if that appeared in your tutorial book.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    My assumption is that if you go back to the previous sentence / paragraph / page / exercize etc, it will be obvious in what way you're supposed to be testing the numbers. Perhaps you're supposed to test if each is a multiple of 3 or something?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    iMalc is correct and I sometimes overlook the questions. I think I will read the chapter again and if there is anything I get stuck with, I will post the problem here.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    Ok here is the code...I am not really sure what I am suppose to test or how to start by rewriting the main().

    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    // Function must be declared before being used.
    int prime(int n);
    
    int main(){
             int i;
    
    // Set up an infinite loop; break if user enters 0.
    // Otherwise, evaluate n from prime-ness.
    while(1){
             cout << "Enter a number (0 to exit)";
             cout << "and press ENTER: ";
             cin >> i;
             if(i == 0)
                  break;
             if(prime(i))
                  cout << i << " is prime" << endl;
             else
                  cout << i << " is not prime" << endl;
             }
              return 0;
    }
    
    // Prime number function. Test divisors from 
    // 2 to sqrt of n. Return false if a divisor
    // found; otherwise, return true.
    int prime(int n){
             int i;
             for(i = 2; i <= sqrt((double) n); i++)
                          if(n % i == 0)
                                return false;         // n is not prime.
             }
              return true;    // If no divisor found, n is prime.
    }
    Here I found it ambiguous. Why? I am not sure if I have to have the user input. And how should I start?

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So you should rewrite the program so that it tests each number from 2 to 20 to see if it is a prime or not.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    Based on the question, should I print all the result from 2 to 20. For instance, something 2 is a prime, 3 is a prime and fourth is not a prime and 5 is a prime.....Is it what the book asking, meaning gotta list all numbers from 2 to 20 and state each number is a prime or not prime?

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Since this is not a formal test, the exact output is up to you. You can only write the numbers that are primes, or you can state for each number whether it's a prime. As long as the computation is correct, it's fine. You're the only one who ultimately cares about the code.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  3. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:35 PM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21