How can I program this one?
what are the codes needed?
Input: 4x4 integer array
Output :
a) Difference of highest and lowest element in each column (array)
b)Average of all even nos in each row (array)
How can I program this one?
what are the codes needed?
Input: 4x4 integer array
Output :
a) Difference of highest and lowest element in each column (array)
b)Average of all even nos in each row (array)
A multidimensional array is declared like this:
char myArray[4][4];
If this is a homework assignment, you may need to spend some time with it, but I think it works:
#define RANK 4
int array[RANK][RANK] = {
{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9, 10, 11, 12},
{13, 14, 15, 16}
};
int colDifference[4];
float evenAverage[4];
void setup() {
// put your setup code here, to run once:
int i;
Serial.begin(9600);
FindColumnDifferences();
FindEvenAverages();
Serial.println("The min-max by column:");
for (i = 0; i < RANK; i++)
Serial.println(colDifference[i]);
Serial.println("The even number row average:");
for (i = 0; i < RANK; i++)
Serial.println(evenAverage[i]);
}
void FindColumnDifferences()
{
int min = array[0][0];
int max = array[0][0];
int i = 0;
int j = 0;
while (j < RANK) {
for ( ; i < RANK; i++) {
if (array[i][j] <= min) {
min = array[i][j];
}
if (array[i][j] >= max) {
max = array[i][j];
}
}
colDifference[j] = max - min;
j++;
i++;
}
}
void FindEvenAverages()
{
float sum;
float denom;
int i = 0;
int j = 0;
denom = RANK / 2.0;
for (i = 0; i < RANK; i++) { // Look through all rows
sum = 0.0;
for (j = 0; j < RANK; j += 2) {
sum += (float) array[i][j];
}
evenAverage[i] = sum / denom;
}
}
void loop() {
// put your main code here, to run repeatedly:
}
Be warned there are likely more elegant solutions.
How can I program this one?
what are the codes needed?
Input: 4x4 integer array
Output :
a) Difference of highest and lowest element in each column (array)
b)Average of all even nos in each row (array)
How can I program this one?
All by yourself. The homework hotline closed.
ronaldmill:
How can I program this one?
what are the codes needed?Input: 4x4 integer array
Create initialized array with some integer values:
int input[4][4]={
{1,2,3,4},
{1,2,3,4},
{1,2,3,4},
{1,2,3,4},
}
ronaldmill:
Output :
a) Difference of highest and lowest element in each column (array)
b)Average of all even nos in each row (array)
See duplicate thread