Need Help with 2D dynamic arrays and finding largest product of any four adjacent numbers in the array

Pages: 123
Hey. Thank you so much, you've been very helpful. That wan instantaneous fix.

The Matrix works fine. But Still don't know how to deal with CMD arguments (Error handling Section) and Finding the largest product. It's like I know what to do but I don't know how to express it. Basically what I'm trying to say is that I went over all the posts you sent me. Tried to do functions on my own. But it is intimidating

Can you please help me with those two functions. This homework assignment is due tomorrow and I'm overwhelmed.

Please Help

Thanks
You already have a function that creates an array. You just need to modify it to return the array, so that main can use the array whenever main needs to call other functions. Earlier I suggested doing something like making a function that can print the array. I shall give you an example here. Give me about an hour to get it ready, because I am about to eat dinner.
Hey thank you so much. Please take your time. Well I just realized that the number of rows and cols has to be entered as a cmd argument. i.e string.

"./greatest_prod_prog –rows 20 –cols 20"

I don't think we have to present the user to enter rows and cols separately.

then with the help of arg v and arg c I have to print out the error messages in the Error Handling Section.

But I don't know how to write the code or a function for this.

Then we can move on to finding the largest product.

Thank you for helping me out.
I have pasted something I did several years ago in an introductory class that shows how to read an array in one function and then print it in another.
The same technique can be used in your program, but with an added function that can find the largest product of adjacent entries in an array.
http://pastebin.com/xi1reeyd

Information on argc and argv is linked in my earlier post. I trust you to read that information on your own and make sense of it.
For convenience here is that link again:
http://www.cs.hmc.edu/~geoff/classes/hmc.cs070.200401/notes/command-args.html
Last edited on
Hey thanks. Yes I read it over and over. But I don't know how to implement that in my program.

I don't know how to use arg v and arg c in the error handling part.

Can you please help me out with the code? In a way that it relates to my program. a

Can you also modify my code so it it reads user's preferred rows and cols as a cmd argument?

I really don't understand and I can't come up with it on my own just by reading something. :(

Please help

Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdlib.h>

int main(int argc, char * argv[])
{
   //check to see that argc is correct
   //if it is not correct, print error and return 0
   //if it is correct then you would need to check each string from argv
   //argv[0] is the name of the command typed
   //argv[1] is the first argument
   //argv[2] is the second argument
   //argv[3] is the third argument... etc.
   return 0;
}


So if you were to type: int kappa = atoi(argv[2]);
That would take the second argument and convert it to an integer and store it to a variable called kappa.
Hey thank you. but I have a hard time writing the conditions (for the commented lines) I know what to print out but how do I use if else statements? I mean how do i write the conditions in C++. that is the major problem I have. Not knowing how to write. I'm really sorry for all this trouble.

I've got an exam tomorrow and also this is due tomorrow. Can you write the actual code for me?

I owe you big time.
Maybe you could check out the tutorial. I suggest paying closer attention in lecture and reading all the assigned reading in the course. Read extra if you have to until you understand it. There are several C++ tutorials on the Internet.
http://www.cplusplus.com/doc/tutorial/

If I was to simply write the code for you I would be depriving you of learning this.
Last edited on
Hey, I'd really love to learn these stuff by myself and actually understand them. But the problem is I have very limited time to finish this and prepare for my test tomorrow.

I totally understand where you're coming from and really do appreciate that you think that way. I know this is asking too much, but as of now I really don't have any other choice.

If you have time please help me out with the code?? I really don't want to get a low grade.

THank you
I know this is asking too much, but as of now I really don't have any other choice.


You could be working on it instead of spending time trying to convince other people to do the work.

You could've worked on it when you still had time to do it.

You could've respected your educational institution and not conspired to cheat your way out of a low grade.

Let's not pretend you don't have a choice.
Summer classes are fast paced. Welcome to university level. I highly recommend reading more of the course material or tutorials before typing any more programs. I wish you luck on your exam tomorrow. Try to get a good night sleep. Maybe your professor will be generous enough to accept a late submission? I recommend visiting either the professor or one of the TAs as soon as you have trouble on the next assignment. When you have no code at all and are completely lost, they have the ability to sit with you in person and explain things really well. This forum on the other hand is for help with any problems that can be stated as specific questions. Anyway, I do wish you luck in your studies.
I initially posted this on Friday and the homework was actually assigned on Thursday.

Posting your assignment with no attempt to even start it is not likely to garner help. 3 days later, you worked up 20ish lines of code. Still, not showing much effort.

When I said "You could've worked on it when you still had time to do it" the emphasis should've been: "You could've worked on it when you still had time to do it."

"I asked for someone else to supply with me with code a week ago" is not work.
are you this jobless?
@ kevinkjt2000

Hey thanks for everything. One last thing. Can you please help me modify/relate find_greatest_product function to create_array function?



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 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;
 }
 }
 }
kevinkjt2000 has already given you a link to a thread about creating multidimensional dynamic arrays.
it is not about creating the array. Array works fine. I need help with linking finding_greatest_production function to create_array function. greatest product function is supposed to find the largest product of any four adjacent numbers in the array. That's what I need help with.
Your code does not even compile.

You don't create proper array; your create_array() does have a local variable, but you cannot use that anywhere else because it is destroyed when the function call ends.

In other words: your Array does not "work fine" -- it does not do what it is supposed to do.
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
#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] << " "; 
            
        
            
        }
       
        
    }


try that portion and let me know.If you think it is not right, what do you suggest I do?
Read the thread about dynamic arrays.

cire did already tell you that this is not legal C++:
1
2
3
int num;
cin >> num;
int arr[num];

I'm new to coding. So I don't know if it is illegal or not, because i don't have experience at all. SO why don't you show me how to do it?

I'm supposed to get the user input via command line argument.
Pages: 123