Help Needed to write this program

● Read this description carefully as you proceed through each step.
● The main program must declare a 2­dimensional array: a matrix.
● You have to create 3 functions:
○ init_matrix: initialize the matrix to values between 0 and 255. You can
decide how to initialize the matrix (using random values, a mathematical
function, etc.). See help below.
○ avercomp_matrix: the goal is to go through the matrix and compare if the
average value of a 2x2 submatrix is greater than the value 122. If it is true a
counter will increase, the output must show how many cells fulfill it. The cells
can overlap.
○ trace_matrix: It calculates the trace of a matrix tr(A) = ∑Aii
, see [1], only if the
matrix is square, otherwise it returns 0.
○ See prototypes on the sample code below.
● The output should be as follows:
There is/are 1386 cells
The trace is 6916
● To give you some help, main_array.cpp could be as follows:
#include<time.h>
#include<stdlib.h>
#include<iostream>
usingnamespacestd;
constintszc=52;
constintszr=44;
constintthreshold=122;
voidinit_matrix(inta[][szc]);
intavercomp_matrix(inta[][szc],intthreshold);
inttrace_matrix(inta[][szr]);
intmain()
{
...
}

● The includes, time.h and stdlib.h, are used in my solution to use the function srand()
and rand() to obtain a random value. srand() sets the seed, and rand() returns a
random value:
srand(time(NULL));//settheseed
...
...
a[i][j]=int((double(1.0*rand()/RAND_MAX)*256));//valuebetween0and255
Please note, that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.


PS. Generation of random integers does not require floating point operations. This site has reference documention for function rand() and includes very clear examples. Note though that you should not learn to use the deprecated function rand() at all, but use the modern C++ equivalents that are declared in header random.
Last edited on
Topic archived. No new replies allowed.