Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Easy Create a project called Inlab10 and make a class called Data. The only inst

ID: 3809903 • Letter: E

Question

Easy

Create a project called Inlab10 and make a class called Data. The only instance variable in Data is a 2D int array. Make the constructor take the number of row and columns and create the array. Make a method that prints the array. Make a method that fills the array with random ints between and including 0 and 9. Make a method that find the sum of each row in the array and returns those values in an array (i.e. an array of row sums). Do not print the sums, return an array of them. Make a method that checks in any column has the consecutive values '1 2 3' anywhere in that column. If some column does, return true. Return false if not. Make a Driver that does some simple tests of your class.

Explanation / Answer

#include<iostream>
#include<time.h>
using namespace std;

class Data
{
   int **array2D;
   int row, col;
public:
   Data(int row, int col);
   void print();
   void fillArray();
   int *sumOfArray();
   bool consecutive_value();
};

Data::Data(int r, int c)
{
   row = r;
   col = c;
   array2D = new int*[row];
   for (int i = 0; i < row; i++)
   {
       array2D[i] = new int[col];
   }
  
}

void Data::fillArray()
{

   int number;

   /* initialize random seed: */
   srand(time(NULL));

   for (int i = 0; i < row; i++)
   {
      
       for (int j = 0; j < col; j++)
       {
          
           /* generate random number between 2 and n: */
           number = rand() % 10;
           //array2D[i][j] = number;
           array2D[i][j] = number;
       }
      
   }
  

}

void Data::print()
{
   for (int i = 0; i < row; i++)
   {
       for (int j = 0; j < col; j++)
       {
           cout << array2D[i][j] << " ";
       }
       cout << endl;
   }
}

int *Data::sumOfArray()
{
   int sum ;
   int *arr;
   arr = (int*)malloc(col*sizeof(int));
   for (int i = 0; i < row; i++)
   {
       sum = 0;
       for (int j = 0; j < col; j++)
       {
           sum += array2D[i][j];
       }
       arr[i] = sum;
      
   }
   return arr;
}

bool Data::consecutive_value()
{

   for (int i = 0; i < col; i++)
   {
       for (int j = 0; j < row; j++)
       {
           if (array2D[i][j] == 1)
           {
               if (j + 2 < row && array2D[i][j + 1] == 1 && array2D[i][j + 2])
                   return true;
           }
       }
      
   }
   return false;
}

int main()
{
   const int row = 4;
   const int col = 3;
   Data test(row,col);
   test.fillArray();
   cout << "Print 2D array: " << endl;
   test.print();
   int *a;
   //print sum of array
   a = test.sumOfArray();
   cout << "Sum of rows in an array: ";
   for (int i = 0; i < row; i++)
   {
       cout << a[i] << " ";
   }
   cout << endl;
   if (test.consecutive_value())
   {
       cout << "Consecutive values 1 2 3 in the column" << endl;
   }
   else
   {
       cout << "no consecutive values 1 2 3 in the column" << endl;
   }
}

----------------------------------------------------------------------------------------------

//output

Print 2D array:
2 8 5
1 6 9
2 9 7
6 3 8
Sum of rows in an array: 15 16 18 17
no consecutive values 1 2 3 in the column

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote