Arrays

Discussion in 'C' started by chemr2, Mar 22, 2009.

  1. chemr2

    chemr2 New Member

    Joined:
    Feb 16, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    Hello, I am to write a program that will send three one dimensional arrays to a function. Two of the arrays have 10 values and one has room set aside for 10 values to be placed into it. The function will take the corresponding elements from each array,multiply them together and put the result into the array with space for 10 values. I am having a hard time figuring out how I can get them to multiply each element and then place them into the array. My guess would be a for loop, or maybe a couple of for loops. Any help appreciated.

    Code:
    #include<iostream>
    using namespace std;
    double calc_volts();
    
    int main()
    {
        double volts[10];
        double current[10] = {10.62 ,14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.8};
        double resistance[10] = {4.0, 8.5, 6.0, 7.35, 9.0, 15.3, 3.0, 5.4, 2.9, 4.8};
       
        calc_volts(volts);
        calc_volts(current);
        calc_volts(resistance);
        
        
        system("pause");
        return 0;
    }
    double calc_volts()
           
           int i;
           
           
           for (i=0;i<????;i++)
           {
               volts[i] = current[i] * resistance[i];
           }
           
           
           
    return volts[i];
     
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Pass them in as parameters:
    Code:
    void calc_volts(double *volts,double *current,double *resistance);
    int main()
    {
        double volts[10];
        double current[10] = {10.62 ,14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.8};
        double resistance[10] = {4.0, 8.5, 6.0, 7.35, 9.0, 15.3, 3.0, 5.4, 2.9, 4.8};
       
        calc_volts(volts, current, resistance);
        
        return 0;
    }
    
    Then just replace ???? with 10 and that should do the trick. You might want to display the results somewhere as well.

    calc_volts() doesn't need to return anything but it will need to take the three arrays as parameters/arguments.
     
  3. chemr2

    chemr2 New Member

    Joined:
    Feb 16, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    Ok thanks that makes sense. I actually do need to return the array that was filled and display the results in main(). If it's returning an array I can still use double for the return since all the values in the array will be doubles correct? If not how would I go about returning the array itself.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    When you modify volts[n] in the array, because you have passed a pointer to the array you're directly modifying the array volts[] in main itself.

    For example:
    Code:
    void func(int *y)
    {
      y[0]=5; // modifies x in main
    }
    main()
    {
      int x[2];
      x[0]=0;
      func(x);
      printf("%d\n",x[0]); // will print 5, not 0
    }
    
    Didn't test this but it should compile and run.
     
  5. chemr2

    chemr2 New Member

    Joined:
    Feb 16, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    Ok thanks for all your help.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice