Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Anybody can help a C++ beginner?

jcytnjcytn Member Posts: 3
[b][red]This message was edited by jcytn at 2002-9-28 10:34:4[/red][/b][hr]
Source codes (for beginers) needed 4 this qt. b4 27-Sept-02. TQ.
-Write a program that inputs a series of integers & passes them one at a time to function even, which uses the modulus operator to determine whether an integer is even. The function should take an integer argument & return true if the integer is even & false otherwise.-

garbled codes (many attempts) - beginner (only 4 lesson)

#include

int main()

int Function1 (int y)

if (y % 2 == 0)
return 1;
else
return 0;

while (x != -1){
value Function1(x)
}
if (value == 1)
cout << "This is an even number" << endl;
else
cout << "This is an odd number" << endl;
return 0
}

Comments

  • j_dotj_dot Member Posts: 6
    [b][red]This message was edited by j_dot at 2002-9-24 16:53:4[/red][/b][hr]
    What do you want help on? Sounds pretty straight forward...I don't think anyone is interested in actually [italic]doing[/italic] your homework, so why don't you post what you have and ask a more specific question.


    : Source codes (for beginers) needed 4 this qt. b4 27-Sept-02. TQ.
    : -Write a program that inputs a series of integers & passes them one at
    : a time to function even, which uses the modulus operator to determine
    : whether an integer is even. The function should take an integer
    : argument & return true if the integer is even & false otherwise.


  • QaneQane Member Posts: 91
    [purple]
    I know this is an old post but I was bored.
    [/purple]

    [code]
    #include

    using namespace std;

    void numFunc(int num, char var);

    int main()
    {
    char var[] = {"abcd"};
    int a, b, c, d;
    cout << "Enter four whole numbers. (n n n n): ";
    cin >> a >> b >> c >> d;
    int nums[4];
    nums[0] = a;
    nums[1] = b;
    nums[2] = c;
    nums[3] = d;

    for (int i = 0; i < 4; i++)
    {
    numFunc(nums[i], var[i]);
    }
    return 0;
    }

    void numFunc(int num, char var)
    {
    if (num % 2 == 0)
    {
    cout << var << " is even
    ";
    }
    else
    {
    cout << var << " is not even
    ";
    }
    }
    [/code]
  • ksysioksysio Member Posts: 10
    : [b][red]This message was edited by jcytn at 2002-9-28 10:34:4[/red][/b][hr]
    : Source codes (for beginers) needed 4 this qt. b4 27-Sept-02. TQ.
    : -Write a program that inputs a series of integers & passes them one at a time to function even, which uses the modulus operator to determine whether an integer is even. The function should take an integer argument & return true if the integer is even & false otherwise.-
    :
    : garbled codes (many attempts) - beginner (only 4 lesson)
    :
    : #include
    :
    : int main()
    :
    : int Function1 (int y)
    :
    : if (y % 2 == 0)
    : return 1;
    : else
    : return 0;
    :
    : while (x != -1){
    : value Function1(x)
    : }
    : if (value == 1)
    : cout << "This is an even number" << endl;
    : else
    : cout << "This is an odd number" << endl;
    : return 0
    : }
    :

    hi,
    think the first of Your problems is the syntax..:-); to define a function in C++ You actually need to write it outside the special main() function which is commonly used by an OS as the startpoint of Your program, so:

    int Function1 (int y)
    {
    if (y % 2 == 0)
    return 1;
    else
    return 0;
    }
    int main()
    {
    /* the code for getting the x's value here */
    if( Function(x) )
    {
    cout << "This is an even number" << endl;
    } else
    {
    cout << "This is an odd number" << endl;
    }
    return 0
    }
    You could also write it much nicer by using some language specific features (they are as simple as efficient, btw), so:

    #include <stdio.h>
    int Function(int y)
    {
    return (y % 2);
    }
    int main()
    {
    int x;
    do
    {
    printf("enter a number
    ");
    scanf("%d", &x);
    printf( "
    the number entered (%d) is %s
    ", x, (Function(x) ? "even" : "odd") );
    } while( x != -1 ) /* seems you use it as the end-program marker */
    }

    would do the work;
    there is nothing bad in using io-streams though, of course - You will get the same effect with them too, just wanted to show You an alternative..

    int main()
    {
    int x;
    do
    {
    cout << "enter a number" << endl;
    cin >> x;
    cout << endl << "the number entered " << x << " is " << (Function(x) ? "even" : "odd") << endl;
    } while( x != -1 ) /* seems you use it as the end-program marker */
    }

    As You can probably see, with the cout/cin streams used the code looks little ugly and less readable (well in this particular case of course)..
    As a programmer You always will choose the one method suitting best to the
    needs of the very moment of the thing You are actually programming so
    so don't hesitate to look at things from different views.. :-)
    regards,
    ksysio
  • Justin BibJustin Bib USAMember Posts: 0

    ____ { http://forcoder.org } free ebooks and video tutorials about ( Visual Basic .NET, R, Java, Perl, JavaScript, Delphi, Python, Go, Scratch, C++, Objective-C, Ruby, Assembly, PL/SQL, C#, Swift, MATLAB, Visual Basic, PHP, C Transact-SQL, VBScript, Rust, F#, D, Prolog, Scala, Scheme, Lisp, Awk, Ada, LabVIEW, SAS, FoxPro, Alice, Lua, Fortran, Hack, Dart, Bash, Crystal, Julia, Erlang, ABAP, Kotlin, COBOL, Logo, Apex, ML, Clojure ) _______

Sign In or Register to comment.