-
May 31st, 2013, 06:47 PM
#1
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;
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|