Thread: Basic prog help- passing parameters

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    25

    Basic prog help- passing parameters

    I'm writing a function that takes a type double array[2][5] and stores the values in another double array, then print both out. I've been stuck on this for a while, here's my code, and the error is:

    copy1-1.cpp:10:7: note: candidate function not viable: no known conversion from 'double' to 'double (*)[5]' for 1st argument
    void funct (double A[2][5],int r,int c)
    ^

    Code:
    #include <iostream>using namespace std;
        
        void funct(double[2][5],int,int);
        int i,j,r,c;
        int n = 1;
        double A[2][5] = {{1.00,2.03,3.2,3,2.4},{.34,1.35,6.54,6,4.5}};
        double B[2][5];
    
    
        void funct (double A[2][5],int r,int c)
        {
        for (i=0;i<r;i++)
        {
        for(j=0;j<c;j++)
        B[i][j] = A[i][j];
        }
        }
        int main ()
        {
        funct(A[1][4],2,5);
        for (i=0;i<r;i++)
        {
        for(j=0;j<c;j++)
        cout << "A=" << A[i][j] << "\t" << "B =" << B[i][j];
        }
        return 0;
        }
    Any help appreciataed

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    You need to pass address of an array, not value of its single element.
    Code:
    funct(A,2,5);

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    25
    We can't use pointers if that's what you mean..

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    You probably can use pointers, except that you are not supposed to use pointer syntax to access the elements of the array. If so, then DRK's advice should work for you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    25
    OK, thanks, my for loops still aren't being reached, I'll spend some more time on it, thanks again.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The for loop are being reached. The problem is that you have multiple distinct variables named r and c, and treating them as if they are equivalent. The r and c inside funct() are not the same ones being used by main().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic C program, using global parameters
    By dsured in forum C Programming
    Replies: 5
    Last Post: 11-01-2010, 01:31 PM
  2. Replies: 12
    Last Post: 01-02-2009, 07:24 AM
  3. need help with a basic prog (reading input files)
    By BoLoB in forum C++ Programming
    Replies: 3
    Last Post: 09-13-2007, 03:35 PM
  4. Visual basic prog in trouble!!
    By beely in forum Windows Programming
    Replies: 1
    Last Post: 11-21-2002, 06:46 AM
  5. visual basic prog
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2002, 02:13 AM