• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C Programming Help

saahmed

Golden Member
I am really stuck here so just looking for some hints and advice. I dont know how to explain what my assignment is without actually posting what I have. So far I am only through the first bullet. I dont know where to go from there. I have read through the entire chapter again and I just dont know what parameters to pass to function drawImage and how to write the other two required functions. So, this is everything I have, I am not asking anyone to do this assignment for me, I simply want some pointers, no pun intended:

The Assignment
The objective of this program is to create an image (25 x 25) with a square of a user-defined size centered on the image.
The program will:
? Ask the user what size of square to draw (Maximum size of a side is 23, make sure to error check this entry)
? Call the function drawImage, included in the drawImage.h header file
? Call a function to print the image to the screen, using an * to demarcate the square
? Call a function that uses a pair of pointers to search through the image to determine where the edge of the square is located. This function should generate a new image that has only the outline of the square.
? Lastly recall the print function to print the image with the outline.

Submit the code as hw7-2.c

Hints:

1. Use these function prototypes
void printImage(int image[]);
void edgeDetection(int image[], int edge[]);

2. If using a one-dimension array to define a two-dimension image the following is a useful method for incrementing through the array: r + c*SIZE. Where r is the current row, c the current column and SIZE is the length of a row.

Below I have attached the given header file and what little I have written so far.

 
first off, I just need help with what to pass on to drawImage. It requires 4 inputs. All I have in function main is the length of the square. I dont know what to pass for the other three parameters.
 
Originally posted by: notfred
Go look in the header file and see what DrawImage takes as parameters.


I did. These are the input parameters: int image[], int size, int side, int even.

I dont know what to put in for the array, size, and even. Side shoud be just what the user inputs. I am thinking it is the same for size, but im not sure.
 
There are no comments? Who wrote the DrawImage function? Was it your professor? If so, ask him what the parameters are supposed to be.
 
Originally posted by: notfred
There are no comments? Who wrote the DrawImage function? Was it your professor? If so, ask him what the parameters are supposed to be.


This is what it says in the comments for the parameters: 1-D Array, width of array, length of square, whether the square side is even or odd length.

I dont know what the array should be and I dont know what I should put for the last parameter.


 
hint 2 tells you what size to make the 1d array.

you shouldn't be using "SIZE" as the square size since you already know the square appears within a fixed-size 25x25 image with one array element needed per pixel.

if the user asks for a 5-pixel square is each side even or odd?
 
Originally posted by: DaveSimmons
hint 2 tells you what size to make the 1d array.

you shouldn't be using "SIZE" as the square size since you already know the square appears within a fixed-size 25x25 image with one array element needed per pixel.

if the user asks for a 5-pixel square is each side even or odd?


so would I actuall put the word "even" if the number is even or "odd" if the number is odd? That is what confused me.

EDIT: I think I figured it out. 1 if true, 0 if false
 
Okay, Ive been working on it. This is what I have now. I know that the printImage function isnt working. Still thinking of the correct way to do it. So, looking for some help with that please.

 
I think ive figured out that the function drawImage is creating the square and centering it on a canvas. So I think the way to do printImage is to use a pointer. Go through each element of the array and dereference the memory location. Each location that is a 1 needs to change to a *. I think this is the correct way to do it, I just dont really know how.

Is this possible?
 
This doesn't really apply to getting the assignment to work correctly, but there are some very simple things you can do to make your program easier to use.

First off, you use a lot of "magic numbers". The assignment says that the screen is 25x25, but there is nothing special about the number 25. You can make it any size you wish. Instead of scattering dependancies of that number throughout your code, define the number somewhere and use that definition in it's place.

For instance, in main() you declare the 2D array as
int square[625];
when it would be clearer to do
const int SCREEN_SIZE= 25;
int square[SCREEN_SIZE*SCREEN_SIZE];
Then you can reuse SCREEN_SIZE anywhere in your code.

The other thing to consider is breaking down the sections of your program to handle specific tasks. Whoever wrote the assignment started you down that path by writing those two function prototypes, but there's no reason not to take this further. For instance, break out the section where you are getting input into its own function. That way everything is self contained and when you go back to make changes to it later (and you will have to make changes because you did it wrong. 😉) it is in one convenient place. A side effect of this is that you can tinker with the input code without breaking any of the code you wrote in other functions. Another is that you can then reuse that code. The last assignment you asked about on here had to do this exact same taks, ie gather input and do bounds checking. If you had broken that out you could have reused it here, saving you time and effort.

Your main function can be broken down in to this:
 
note in your program you don't test the side value until after you use it (too late).

in Kyteland example the getInput() function would need to test the value of side and force the user to enter a proper value.
 
Back
Top