Assume pixels is a 3x3 2D floating point array double[3][3] . What is the output
ID: 3738450 • Letter: A
Question
Assume pixels is a 3x3 2D floating point array double[3][3] . What is the output of the
following code? (note that the fill function is defined on the next page)
int main(void) {
fill(3.7);
print(true);
fill(8.9);
print(false);
fill(2);
print(true);
}
double getPixel(int x, int y, double pad) {
if (x >= sizeX || x < 0 || y < 0 || y >= sizeY) {
return pad;
}
return pixels[x][y];
return 0;
}
void print(bool raw) {
for (int i = 0; i < sizeX; i++) {
for (int j = 0; j < sizeY; j++) {
if (raw == true) {
std::cout<<std::setw(3)<<getPixel(i, j)<<" ";
} else {
std::cout<<std::setw(3)<<int(getPixel(i, j))<<" ";
}
}
std::cout<<std::endl;
}
std::cout<<std::endl<<std::endl;
}
void fill(double value) {
for (int i = 0; i < sizeX; i++) {
for (int j = 0; j < sizeY; j++) {
pixels[i][j] = value;
}
}
}
Explanation / Answer
Hi there,
The program provided has some compilation errors. I have fixed them and providing the complete code.
Also In the end is the compile time errors without the changes I have done along with the output of compilable code.
#include "iostream"
#include "conio.h"
#include <iomanip>
// since pixels[3][3] is conditioned to be an array with 3*3 elements, sizeX & sizeY will be 3 each
// So assuming following:
#define sizeX 3
#define sizeY 3
double pixels[3][3];
// Adding declarations of the functions defined after main, So that main function can call them without compile time errors.
/** added pad = 10.0 as default value. Since provided program does not call the method with 3 aruments.
The value can be anything. I assumed it to be 5 .**/
double getPixel(int x, int y, double pad = 5.0);
void print(bool raw);
void fill(double value);
int main(void)
{
fill(3.7);
print(true);
fill(8.9);
print(false);
fill(2);
print(true);
}
double getPixel(int x, int y, double pad)
{
if (x >= sizeX || x < 0 || y < 0 || y >= sizeY)
{
return pad;
}
return pixels[x][y];
return 0;
}
void print(bool raw)
{
for (int i = 0; i < sizeX; i++)
{
for (int j = 0; j < sizeY; j++)
{
if (raw == true)
{
std::cout<<std::setw(3)<<getPixel(i, j)<<" "; /**setw(3) method sets the width parameter of the output/input stream to exactly 3. **/
}
else
{
std::cout<<std::setw(3)<<int(getPixel(i, j))<<" ";
}
}
std::cout<<std::endl;
}
std::cout<<std::endl<<std::endl;
}
void fill(double value)
{
for (int i = 0; i < sizeX; i++)
{
for (int j = 0; j < sizeY; j++)
{
pixels[i][j] = value;
}
}
}
/*****************
OutPut:
3.7 3.7 3.7
3.7 3.7 3.7
3.7 3.7 3.7
8 8 8
8 8 8
8 8 8
2 2 2
2 2 2
2 2 2
****************/
/*********************************************************************************
Your program output without above assumptions will be as below
File.cpp(101): error C2065: 'sizeX' : undeclared identifier
File.cpp(101): error C2065: 'sizeY' : undeclared identifier
File.cpp(108): error C2065: 'sizeX' : undeclared identifier
File.cpp(109): error C2065: 'sizeY' : undeclared identifier
File.cpp(111): error C2660: 'getPixel' : function does not take 2 arguments
File.cpp(113): error C2660: 'getPixel' : function does not take 2 arguments
File.cpp(121): error C2065: 'sizeX' : undeclared identifier
File.cpp(122): error C2065: 'sizeY' : undeclared identifier
**********************************************************************************/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.