CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2013
    Posts
    3

    Need Help with 2D arrays and functions

    Hey guys, I'm trying to do this problem set. You can read it here

    https://www.dropbox.com/s/uj2kd8a764...blem%20Set.pdf

    Basically I have already created the function that takes command line arguments and creates the dynamic array.

    And the array/matrix works fine. Now Im trying modify the function that finds the largest product of 4 adjacent numbers. Code for that will be posted below.

    I need help linking these two functions together so that find_greatest_product function would take user input from create_array function and would find the largest product.

    Basically what I'm asking is to help me modify it.

    Thanks in advance

    P.S We cannot use vectors for this assignment.


    Code:
    #include <iostream>
    
    #include <cstdlib>
    #include <string>
    #include <ctime>
    #include <iomanip>
    
    
    using namespace std;
    
    int ** create_array(int rows, int cols);
    void fillArray(int ** a, int rows, int cols);
    void showArray(int ** a, int rows, int cols);
    void find_greatest_product (void);
    
    
    
    int main(int argc, char * argv[])
    {
        for (int i=0; i<argc; i++)
            cout << i << " " << argv[i] << endl;
        
        
        
        int rows = atoi(argv[2]);
        int cols = atoi(argv[4]);
        
        cout << "rows = " << rows << endl;
        cout << "cols = " << cols << endl;
        
        int ** array = create_array(rows, cols); // Allocate the array
        fillArray(array, rows, cols);            // Put in some values
        showArray(array, rows, cols);            // Display the contents
        
        return 0;
    }
    
    int ** create_array(int rows, int cols)
    {
        int ** arr = new int * [rows];
        for (int i=0; i<rows; i++)
            arr[i] = new int [cols];
        
        return arr;
    }
    
    void fillArray(int ** a, int rows, int cols)
    {
        srand(time(NULL)); // Generate Random Numbers
        
        int k = 0;
        for (int i=0; i< rows; i++)
            for (int j=0; j<cols; j++)
                a[i][j] = k++;
        
    }
    
    void showArray(int ** a, int rows, int cols)
    
    
    {
        srand(time(NULL)); // Generate Random Numbers
        
        for (int i=0; i< rows; i++)
        {
            for (int j=0; j<cols; j++)
                
                cout << setw(3) << (a[i][j] =(rand() % 99) + 1) << " ";
            
            cout << endl;
        }
    }
    
    
    void find_greatest_product (void) // Function Needs To be Modified So that it takes input from create_array function and finds the largest product of four adjacent numbers
    
    {
        
        char line[10];
        int a[i][j];
        
        for(int i =0 ; i<20; i++) {
            for(int j=0; j<20; j++) {
                tmpfile.getline(line, 10, ' ');
                a[i][j] = atof(line);
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
        
        double max=1;
        double prod;
        
        // rows
        for(int i=0;i<20; i++) {
            for(int j=0; j<17; j++) {
                prod = a[i][j] * a[i][j+1] * a[i][j+2] * a[i][j+3];
                if(max < prod) {
                    max = prod;
                }
            }
        }
        
        // column
        for(int j =0; j<20;j++) {
            for(int i=0;i<17; i++) {
                prod = a[i][j] * a[i+1][j] * a[i+2][j] * a[i+3][j];
                if(max < prod) {
                    max = prod;
                }
            }
        }
        
        // diagonal right top to bottom left
        // exclude 3 from 20 as it never can make it to 4 elements
        for(int i=0; i< 17;i++) {
            for(int j=0; j<17;j++) {
                prod = a[i][j]*a[i+1][j+1]*a[i+2][j+2]*a[i+3][j+3];
                if(max < prod)
                    max = prod;
            }
        }
        
        // diagonal left top to right bottom
        for(int i=3; i< 20;i++) {
            for(int j=0; j<17;j++) {
                prod = a[i][j]*a[i-1][j+1]*a[i-2][j+2]*a[i-3][j+3];
                if(max < prod)
                    max = prod;
            }
        }
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Need Help with 2D arrays and functions

    Quote Originally Posted by Jinjaninja1 View Post
    Hey guys, I'm trying to do this problem set. You can read it here

    https://www.dropbox.com/s/uj2kd8a764...blem%20Set.pdf

    Basically I have already created the function that takes command line arguments and creates the dynamic array.
    You need to destroy the array at the end. Without that, you have a memory leak, and you also won't show your teacher you know how to dispose of the allocated memory.
    I need help linking these two functions together so that find_greatest_product function would take user input from create_array function and would find the largest product.
    Aren't you just supposed to pass the int** to the function and work with it inside that function?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    May 2013
    Posts
    3

    Re: Need Help with 2D arrays and functions

    Quote Originally Posted by Paul McKenzie View Post
    You need to destroy the array at the end. Without that, you have a memory leak, and you also won't show your teacher you know how to dispose of the allocated memory.
    Aren't you just supposed to pass the int** to the function and work with it inside that function?

    Regards,

    Paul McKenzie
    Hey Paul, thank you for the reply. Yes that is exactly what I'm supposed to do. I'm new to the whole programming area and this is the first language I'm learning. So I'm sorry if my last post didn't make any sense. I'm not so familiar with the terms.

    Anyways, can you please show me how to do it. I've been researching all day, and I can't figure out how to do that on my own. And also there might be some unnecessary code in the find_greatest_product function. I'd be glad if you can also remove that, and post the modified code below,

    Thank you for your time

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Need Help with 2D arrays and functions

    Quote Originally Posted by Jinjaninja1 View Post
    Anyways, can you please show me how to do it. I've been researching all day, and I can't figure out how to do that on my own.
    You pass the int** just like you did in the fillArray and showArray functions. You also need to pass the number of rows and columns.
    Code:
    void find_greatest_product (int** a, int rows, int cols)
    You need to get rid of that local a[][] array inside of that function, since the array is being passed to that function.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    May 2013
    Posts
    3

    Re: Need Help with 2D arrays and functions

    Quote Originally Posted by Paul McKenzie View Post
    You pass the int** just like you did in the fillArray and showArray functions. You also need to pass the number of rows and columns.
    Code:
    void find_greatest_product (int** a, int rows, int cols)
    You need to get rid of that local a[][] array inside of that function, since the array is being passed to that function.

    Regards,

    Paul McKenzie
    Hey thank you for the reply. I just got rid of the a[][]

    what do i do about?

    Code:
    char line[10];
    and

    Code:
    tmpfile.getline(line, 10, ' ');
    Can you also show me how to pass int** and where to insert cout statements so that the function will output the max product based on the user input?

    Thanks once again

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Need Help with 2D arrays and functions

    Code:
    int rows = atoi(argv[2]);
    int cols = atoi(argv[4]);
    What happens if argc < 5? You haven't tested for the error conditions mentioned in the assignment.

    Your fillArray sets the initial values. But your showArray also sets values in the array! Shouldn't showArray just show the values and not also change values?

    Code:
    tmpfile.getline(line, 10, ' ');
    This will read a line from the file stream associated with tmpfile. But you don't open a file anywhere! From where did you get this code for find_greatest_product? Its based upon a 20x20 array read from a file - whereas for your program the size of the array is entered on the command line and fillArray sets the values of the array rather than reading from a file! You need to get rid of this code that reads from a file and change the loop conditions to refer to the rows and cols variables passed to the function as indicated in Paul's post.

    where to insert cout statements so that the function will output the max product based on the user input?
    When the function has finally calculated the maximum value!
    Last edited by 2kaud; June 1st, 2013 at 07:04 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured