Command Line Input and Dynamic Arrays

Hey guys, i'm trying to do this problem set: https://www.dropbox.com/s/uj2kd8a764v7txy/Problem%20Set.pdf

I need help with modifying "find_greatest_product" function so that it is related to create_array function. It has to take user input for rows and cols from create array function and do the calculation to find the largest product. I also need help with Error Handling Part. I need to take user input as a command line argument.


Thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <iomanip>



using namespace std;

void create_array( void );
void find_greatest_product (void);


int main(int argc, char * argv[]){
    
    
    cout << "WELCOME!" << endl;

    create_array();
    
    find_greatest_product();
    
    
    return 0;
}


void create_array( void ) // Function that creates the Array
{
    int col , row;
    cout << "Columns\n> " << flush;
    cin >> col;
    cout << "Rows\n> "<< flush;
    cin >> row;
    int array[row][col];
    
    srand(time(NULL)); // Generate Random Numbers

    
    for( int i = 0; i <row; ++i, cout << endl )
        
        for( int j = 0; j <col; ++j )    {
            
             cout << setw(3) << (array[i][j] = (rand() % 100) + 1) << " ";
            
            
           
           // array [j][i]= (rand() % 100) + 1;
          
           // cout << array[j][i] << " "; 
            
        
            
        }
       
        
    }



void find_greatest_product (void) // Function Needs To be Modified So that it takes input from create_array function

 {
 char line[10];
 double array[j][i];
  
 for(int i =0 ; i<20; i++) {
 for(int j=0; j<20; j++) {
 myfile.getline(line, 10, ' ');
 arr[i][j] = atof(line);
 cout<<arr[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 = arr[i][j] * arr[i][j+1] * arr[i][j+2] * arr[i][j+3];
 if(max < prod) {
 max = prod;
 }
 }
 }
 
 // column
 for(int j =0; j<20;j++) {
 for(int i=0;i<17; i++) {
 prod = arr[i][j] * arr[i+1][j] * arr[i+2][j] * arr[i+3][j];
 if(max < prod) {
 max = prod;
 }
 }
 }
 
 // diagonal right top to bottom left
 
 for(int i=0; i< 17;i++) {
 for(int j=0; j<17;j++) {
 prod = arr[i][j]*arr[i+1][j+1]*arr[i+2][j+2]*arr[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 = arr[i][j]*arr[i-1][j+1]*arr[i-2][j+2]*arr[i-3][j+3];
 if(max < prod)
 max = prod;
 }
 }
 }
Last edited on
Line 36: int array[row][col]; Invalid C++. You cannot create arrays like that.
Also this is local variable which will be destroyed when function ends.

When using C++, use C++ containers instead of raw arays. Vectors for example.
we have not learned vectors. please read the problem set
I read the problem. You need to pass command-line parameters to your program, such as -rows 10 -cols 5 and dynamically allocate an array based upon those values.

Here's a start, but it needs to properly validate the parameters and output required error messages if the parameters are not valid etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <iomanip>
#include <cstdlib>

    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);

int main(int argc, char * argv[])
{
    for (int i=0; i<argc; i++)
        cout << i << " " << argv[i] << endl;

    // Should do proper validation, but for now, 
    // just assume values are where we want them

    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)
{
    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)
{
    for (int i=0; i< rows; i++)
    {
        for (int j=0; j<cols; j++)
            cout << setw(4) << a[i][j] ;
        cout << endl;
    }
}
hey, thank you so much. I tried compiling the code, this is the output I got.



0 /Users/user1/Library/Developer/Xcode/DerivedData/Matrices-cvwscdrjogajmkadciipaxzuxnrr/Build/Products/Debug/Matrices
rows = 0
cols = 0



It wouldn't let me input -rows and -cols. Do you know why?

Thanks
The parameters need to be entered on the command line. If you use an IDE then there may be a place to enter the run-time parameters.

Typically you go to the command line and type the name of the program in order to run it. In this case you simply need to type the name of the program followed by the parameters.
Hey, thank you for the information.

I tried that on G++ too. We usually ssh into our college server and then run programs. So if I were to test this on a remote server (G++ Compiler) how would I do it on a mac? Open terminal>Then?

Thank you for your time
I'm glad that was at least some help.
Unfortunately I can't help with the mac (maybe someone else can?)

Anyway, it looks as though the name of your program is "Matrices".
So from the command line, either type in the full path and program name followed by the parameters, or navigate to the directory where the program is located, and just type the program name followed by the parameters.
Last edited on
hey thank you so much. your code was immensely helpful. Finally I'm in the right track. Can you help me out with the "Error handling" part that prints out errors based on invalid entries?

And also the second part of the problem set is to write a function that finds the largest product of four adjacent numbers. This is the code I have for that. Can you help me modify and link this to the code you provided. so it can take user input from your code, display the matrix and out put the largest product? Thank you

THis is the code for finding the largest product:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
void find_greatest_product (void) // Function Needs To be Modified So that it takes input from create_array function

 {
 char line[10];
 double arr[20][20];
  
 for(int i =0 ; i<20; i++) {
 for(int j=0; j<20; j++) {
 myfile.getline(line, 10, ' ');
 arr[i][j] = atof(line);
 cout<<arr[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 = arr[i][j] * arr[i][j+1] * arr[i][j+2] * arr[i][j+3];
 if(max < prod) {
 max = prod;
 }
 }
 }
 
 // column
 for(int j =0; j<20;j++) {
 for(int i=0;i<17; i++) {
 prod = arr[i][j] * arr[i+1][j] * arr[i+2][j] * arr[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 = arr[i][j]*arr[i+1][j+1]*arr[i+2][j+2]*arr[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 = arr[i][j]*arr[i-1][j+1]*arr[i-2][j+2]*arr[i-3][j+3];
 if(max < prod)
 max = prod;
 }
 }
 }
If you are having trouble writing a function that can accept arguments, you need to read more from your textbook or online sources such as the tutorial on this site. I do hope that you can just use the code that Chervil very generously provided you. If you cannot use that code as an example to write a function on your own that can accept an array... (I quote from the other forum that you made for this same exact homework assignment)
If you cannot read that, then you could not read it even if we would retype it.
Last edited on
so how do I modify the above function, so it accepts arguments from the create_array function?

I read the textbook ,It doesn't explain much about CMD arguments. It mostly explains about a palindrome checker program.
sorry Jinjaninja and chervil, since I'm learning C + +, and I try to get me to the bones carrying traces proposals, I would not have mistranslated the track exercise, but there was no talk of finding the greatest product of four elements, one adjacent to the next, without bias direction in search of the maximum value of the product, including the four elements?
I understand from your code that you consider only the predetermined directions ...

... or I mistranslated the track?
Last edited on
Topic archived. No new replies allowed.