#include <iostream> #include <cassert> using namespace std; double findMax(doubl
ID: 3762144 • Letter: #
Question
#include <iostream>
#include <cassert>
using namespace std;
double findMax(double a[ROWS][COLS])
{
int biggest = a[0][0];
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
if (a[i][j] > biggest) biggest = a[i][j];
}
}
return biggest;
}
int main(){
const int ROWS = 4;
const int COLS = 3;
double a[][];
assert()
}
I need help writing assertions this is what I have so far. Here are the instructions for this assignment:
Implement a function called findMax that determines the maximum value in a 2-dimensional array. The declaration of the function is given above as well as the variables ROWS and COLS. Provide test code that defines an array of size ROWS by COLS and that uses an assert statement to check that the function returns the correct value.
Explanation / Answer
#include <iostream>
#include <cassert>
const int ROWS = 4;
const int COLS = 3;
using namespace std;
double findMax(double a[ROWS][COLS])
{
int biggest = a[0][0];
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
if (a[i][j] > biggest) biggest = a[i][j];
}
}
return biggest;
}
int main(){
double a[ROWS][COLS];
for(int i=0;i<ROWS;i++){
for(int j=0;j<COLS;j++){
a[i][j]=i+j;
}
}
cout<<findMax(a);
assert(findMax(a)!=5);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.