Help With Parameters

This is a discussion on Help With Parameters within the C++ Programming forums, part of the General Programming Boards category; Hi, i am trying to write function called Add and write a main program to test it. iwant the function ...

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Help With Parameters

    Hi, i am trying to write function called Add and write a main program to test it. iwant the function should take two integer parameters and return an integer result.

    here is the code i have so far but i cant seem to pass them through.

    Code:
    #include <iostream>
    
    using namespace std;
    int Add(int a, int b);
    int main()
    {
    
    }    
    int Sum()
    {    
        int a;
        int b;    
        cout << "Enter the number: ";
        cin >> a;
        cout << "Enter another number: ";
        cin >> b;
        Add(a,b);
        return 0;
    }
    
    int Add(int a,int b)
    {
        Sum = Add(a,b);
        return 0;
    }
    can anyone help?

  2. #2
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    actually i think i got it.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int Number(int a, int b);
    
    int main()
    {
    
    cout <<"Number = " << Number(8,2) <<endl;
    
    system ("pause");
    }
    
    int Number(int a, int b)
    {
    return a,b;
    }
    the only prob now is that it out puts just "2" and not both "2" and "8"
    Last edited by peckitt99; 10-26-2006 at 01:30 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> return a,b;
    The function should probably return the result of adding the numbers.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >the only prob now is that it out puts just "2" and not both "2" and "8"
    If you want to add them, then you would do:
    Code:
       return a + b;
    If you want to subtract them, then:
    Code:
       return a - b;
    The idea is: perform some operation on the arguments and return the result.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    the only prob now is that it out puts just "2" and not both "2" and "8"
    you can only return one value, so it would probably only return the first value passed to 'return', in your case just 2. you could return a struct that contains these 2 values but you may not be familiar with them. check out the tutorials on cplusplus.com

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Actually
    operator , returns the last value past to it (read manual)

    so
    Code:
    a, b
    is equal b

  7. #7
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    yeah thanks i managed to get that bit working now.

    this is anothe part of this code that i need to add on, i dont expect anyone to do this but can someone explain what is being asked with this...

    Code:
    write another function called Add which takes three 
    1-dimensional arrays of integers, of the same length as parameters.  
    The first two arrays are added member by member and the result is returned in the third array.  
    The length of the arrays is passed in ArrLen.  
    
    Add(A,B,Result,ArrLen);
    would be a great help thanks

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    ok begin by writing the function signature and as much of its body as you can. the signature is something like 'int Number(int a, int b)' in your previous post

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    The function should except two arrays as input
    For example
    Code:
    A={1,2,3,5,10}
    B={5,6,7,8,-1}
    /*Its length */
    ArrLen=5
    And fill the third array with the sums of elements according to the received data
    In the sample above the result should be
    Code:
    Result={6,8,10,13,9}

  10. #10
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    just tied adding 2 arrays together but come up with a long number and not them both added together.

    this is the code i have

    Code:
    int main()
    {
    
    int a[5]={1,2,3,5,10};
    int b[5]={5,6,7,8,-1};
    int ArrLength=5;
    
    ArrLength = a[5]+b[5];
    
    cout <<ArrLength;
    
    system("pause");
    
    }

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,632
    > ArrLength = a[5]+b[5];
    That's a fence post error (assuming arrays start at 1). A five element array extends from 0 to 4.

    > ArrLength
    If you wanted the size of the array, dividing the size of the pointer by the size of one element usually works.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    you cant just add two arrays together like how you did and expect to get the answer it may yield.

    use a loop to go through the elements (a[0]-a[4]) and add them up (c[x] = a[x] + b[x])

  13. #13
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    i have finally got the array to add together, but i am having trouble passing them all for example i need to output it using i code line similar to

    Code:
    Sum = Add(a,b,ArrLength);
    but i am unsure how to do this.

    this is the code i have so far

    Code:
    #include <iostream>
    
    using namespace std;
    
    int Number(int a, int b);
    int Add();
    
    int main()
    {
    Add();
    cout <<"Sum = " << Number(8,2) <<endl;
    
    Sum = Add(a,b,ArrLength);
    
    system ("pause");
    }
    
    int Number(int a, int b)
    {
    return a+b;
    }
    
    int Add()
    {
    int a[5]={1,2,3,5,10};
    int b[5]={5,6,7,8,-1};
    int ArrLength[5];
    int i;
    
        for (i=0; i<5; i++)
        {
        ArrLength[i]= a[i] + b[i];
        cout <<ArrLength[i];
        }
           return ArrLength[5];
    }
    Can anyone see where i am going wrong?
    Last edited by peckitt99; 10-29-2006 at 04:45 AM.

  14. #14
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    Quote Originally Posted by peckitt99
    i have finally got the array to add together, but i am having trouble passing them all for example i need to output it using i code line similar to

    Code:
    Sum = Add(a,b,ArrLength);
    but i am unsure how to do this.

    this is the code i have so far

    Code:
    #include <iostream>
     
    using namespace std;
     
    int Number(int a, int b);
    int Add();
     
    int main()
    {
    Add();
    cout <<"Sum = " << Number(8,2) <<endl;
     
    Sum = Add(a,b,ArrLength);
     
    system ("pause");
    }
     
    int Number(int a, int b)
    {
    return a+b;
    }
     
    int Add()
    {
    int a[5]={1,2,3,5,10};
    int b[5]={5,6,7,8,-1};
    int ArrLength[5];
    int i;
     
        for (i=0; i<5; i++)
        {
        ArrLength[i]= a[i] + b[i];
        cout <<ArrLength[i];
        }
           return ArrLength[5];
    }
    Can anyone see where i am going wrong?
    YES...and YES
    1) your Add() function is declaring local variables...they will die at the end of the scope...so you will have junk values or get a compiler error when you try to refer to them in other functions
    2) Why are you returning ArrLength[5] when the index value runs from 0-4...and you are not returning the array you are supposedly returning a int value only
    3) uh yea...
    nextus, the samurai warrior

  15. #15
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    Code:
    #include <iostream>
    
    using namespace std;
    
    int Number(int, int);
    int Add(int*, int*, int*);
    
    int main()
    {
    	int a[5] = {1,2,3,5,10};
    	int b[5] = {5,6,7,8,-1};
    	int ArrLength[5];
    	
    	cout << "Sum = " << Number(8,2) << endl;
    
    	int sum = Add(a,b,ArrLength);
    	cout << "Sum of both arrays together is " << sum << endl;
    
    	system ("pause");
    }
    
    int Number(int a, int b)
    {
    	return a+b;
    }
    
    int Add(int* a, int* b, int* ArrLength)
    {
    	int sum = 0;
        for(int i=0; i<5; i++)
        {
    		ArrLength[i]= a[i] + b[i];
    		sum += ArrLength[i];
        }
    
    	return sum;
    }
    nextus, the samurai warrior

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-21-2008, 06:04 AM
  2. function with variable number of parameters
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 03:35 PM
  3. Additional parameters for operator delete
    By darksaidin in forum C++ Programming
    Replies: 0
    Last Post: 09-21-2003, 11:46 AM
  4. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 02:13 AM
  5. command-line parameters.
    By Tombear in forum C Programming
    Replies: 2
    Last Post: 10-28-2001, 07:40 AM

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