Help with arrays!!

I need help with arrays. My assignment is use a 3D array to create an image. Use an array of the form arr[300][200][3]. For this homework, you need to use an array of the form arr[3][400][500]. It will have 400 rows and 500 columns. You need to first initialize the array as described in class and after that write the array to a ppm image file. When viewed using Gimp, it should display USM. The letters would be 200 pixels high and 100 pixels wide each. Use at least 15 pixels for the width of the stroke. Use different colors for each letter and a different color for the background.

This is what I have so far. I am a beginner and trying to figure all of this out so I know that I have a lot wrong.


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void initialize(unsigned char a[][200][3], int nrows, int ncols, int nc);
void printAll(unsigned char a[][200][3], int nrows, int ncols, int nc);
void writeAll(unsigned char a[][200][3], int nrows, int ncols, int nc);
void writeAllB(unsigned char a[][200][3], int nrows, int ncols, int nc);


int main()
{
unsigned char arr[300][200][3];
initialize(arr, 300, 200, 3);
writeAll(arr, 300, 200, 3);
writeAllB(arr, 300, 200, 3);
return 0;
}

void initialize(unsigned char a[][200][3], int nrows, int ncols, int nc)
{
for (int row = 0; row < nrows/2; row++)
{
for (int col = 0; col < ncols/2; col++)
{
a[row][col][0] = 25;
a[row][col][1] = 120;
a[row][col][2] = 77;
}
}
for (int row = nrows/2; row < nrows; row++)
{
for (int col = 0; col < ncols/2; col++)
{
a[row][col][0] = 255;
a[row][col][1] = 255;
a[row][col][2] = 0;
}
}
for (int row = 0; row < nrows/2; row++)
{
for (int col = ncols/2; col < ncols; col++)
{
a[row][col][0] = 255;
a[row][col][1] = 0;
a[row][col][2] = 255;
}
}
for (int row = nrows/2; row < nrows; row++)
{
for (int col = ncols/2; col < ncols; col++)
{
a[row][col][0] = 0;
a[row][col][1] = 255;
a[row][col][2] = 255;
}
}

}

void printAll(unsigned char a[][200][3], int nrows, int ncols, int nc)
{

for (int row = 0; row < nrows; row++)
{
for (int col = 0; col < ncols; col++)
{
cout << a[row][col][0] << " " << a[row][col][1] << " " << a[row][col][2] << " ";
}
cout << endl;
}
cout << endl;

}


void writeAll(unsigned char a[][200][3], int nrows, int ncols, int nc)
{
string name = "out.ppm";
//ofstream fout;
//fout.open(name);
ofstream fout("out.ppm");
fout << "P3" << endl;
fout << ncols << " " << nrows << endl;
fout << 255 << endl;
for (int row = 0; row < nrows; row++)
{
for (int col = 0; col < ncols; col++)
{
fout << static_cast<unsigned int>(a[row][col][0]) << " " << static_cast<unsigned int>(a[row][col][1]) << " " << static_cast<unsigned int>(a[row][col][2]) << " ";
}
fout << endl;
}
fout << endl;
fout.close();
}


void writeAllB(unsigned char a[][200][3], int nrows, int ncols, int nc)
{
ofstream fout("outB.ppm", ios::out | ios::binary);
fout << "P6" << endl;
fout << ncols << " " << nrows << endl;
fout << 255 << endl;
for (int row = 0; row < nrows; row++)
{
for (int col = 0; col < ncols; col++)
{
fout << a[row][col][0] << a[row][col][1] << a[row][col][2];
}
}
fout.close();
}
Lol. Glad to see I'm not the only one confused about this assignment. Everyone else in our class seems to be. I'd be interested to see if anyone's gotten farther than this.
I really dont understand how he expects us to do this without giving us a little more information than he has provided.

If everyone in the whole class is confused then the teacher has failed to explain him/her-self. You should all confront the teacher and ask for clarification.

If you are expected to fill a 3d array containing 3 2d arrays of 400x500, and this data is to contain the pixels for the 3 letters: "U", "S", "M", then it would appear that each of the array items of 400x500 is each a letter; "U", "S", "M". Thus each letter is made up of a 400x500 matrix.

1) Write a function that will accept a char* and creates a 400x500 matrix representing the letter in the arg to the function.

2) Iterate through the array; three iterations, and on each iteration call the function that will "print" the "letter" from the array item.

I mabe completely wrong, but that is my observation from the information provided.
Last edited on
Topic archived. No new replies allowed.