QuestionQuestion

Files to submit: all .c and .h files that make up your program paint.out. A Makefile to compile those files into paint.out

Requirements
Program must compile with both -Wall and -Werror options enabled
Submit only the files requested
Use doubles to store real numbers
Print all doubles to 2 decimal points unless stated otherwise
NEW: You must submit at least two .c files and one .h file

Restrictions
No global variables may be used
Your main function may only declare variables and call other functions
NEW: You must use at least 1 struct in your solution

Description
For your final project you will be implementing a text based version of paint called paint.out. This program exercises all of the topics that we have covered in class except for recursion and has an extreme emphasis on pointers and realloc.

Additional Details
Command Line Arguments
Your program should accept optional command line parameters specifying the size of the canvas to create
The first parameter is the number of rows
The second parameter is the number of columns
Since the number of rows and columns are optional your program should be able to be called like
./paint.out
./paint.out num_rows num_columns
Either both the number of rows and columns should be specified or neither should
The number of rows and columns should both be positive integers >= 1
If no command line arguments are given, an incorrect number of command line arguments are given, or their are incorrect values for any of the command line arguments the canvas should default to be 10 X 10

Commands
Your program must be capable of executing the following commands. The bolded letter inside the parenthesis is the letter used to specify the command(q)uit
Cease the execution of the program and free any dynamically allocated memory that is in use (h)elpDisplay the help information
I've already implemented that prints the help information so you can just copy it to your code and then call it from there (w)rite start_row start_column end_row end_columnDraw a line from start_row, start_column to end_row, end_column
Horizontal lines are drawn using -
Vertical lines are drawn using |
Right diagonal lines are drawn using
Left diagonal lines are drawn using /
If the line you are drawing intersects another line a + should be displayed at the intersection.
The exception to this if a line drawn using the same direction overlaps another line going the same direction
For example a horizontal line overlapping a horizontal line should continue to be drawn using -
Lines should be able to be drawn from either direction. For example a horizontal line should be able to be drawn as either
Left to right: w 0 0 0 5
Right to left: w 0 5 0 0
Lines that are one cell big should be drawn using -
If the coordinates given do not form a straight line you should inform the user and not draw the line (e)rase row colErase the character at row col reverting it back to a blank space
Blank spaces are represented as a * to help us better visualize them (r)esize num_rows num_cols
Resize the canvas to be num_rows by num_cols big
The smallest a board can be resized to is 1 X 1
New rows are added to the top of the canvas
New columns are added to the right of the canvas
New rows/columns are empty
The canvas can be resized to be smaller than it is
If it is the lower left portion of the canvas is what is kept
(a)dd [r | c] position
Add a new row or column at the specified position
r stands for row and c stands for column so
A command to add a new row at position 5 looks like: add r 5
A command to add a new column at position 7 looks like: add c 7
Valid values for position are between 0 and num_rows/num_cols + 1 based on whether a row or column is specified
If adding a row, the row at that position and all rows above are moved up 1
If addina column, the column at that position and all columns to the right are moved to the right by 1
(d)elete [r | c] position
Delete a row or column at the specified position
Valid values for position are between 0 and the number of rows/columns
If deleting a row, all the rows above the one deleted are shifted down by 1
If deleting a column, all the columns to the right of the one deleted are shifted to the left by 1
(s)ave file_name
Save the current canvas to the file named file_name
How you save the file is up to you
(l)oad file_name
Load a canvas from the file with file_name
The structure of these files is the one that you came up with in save
Input
Input will not always be valid.
If input is invalid you should inform the user and then ask for another command
Hints
This is a really big program and you are going to want to break it down into many functions.
Trying to do too much in one function will lead lots of confusion and make the problem even harder
In total my answer consisted of about 857 lines of C code broken up into 43 functions contained in 7 files
I used 4 structs and 1 enum in my answer
Take this problem apart piece by piece and test each piece as you add it in.
This will help a lot in finding bugs and smooth out the development of your program
First start with creating and displaying your canvas
After you get that working add in commands one by one
This will involve reading the command
Then executing the command
You might try adding in commands in the following order
quit
help
write
add
delete
resize
save
load
Proper use of structs can greatly simplify this problem
Example
User input has been underlined to help you differentiate between what is user input and what is program output. You do not have to underline anything.
./paint.out
9 * * * * * * * * * *
8 * * * * * * * * * *
7 * * * * * * * * * *
6 * * * * * * * * * *
5 * * * * * * * * * *
4 * * * * * * * * * *
3 * * * * * * * * * *
2 * * * * * * * * * *
1 * * * * * * * * * *
0 * * * * * * * * * *
0 1 2 3 4 5 6 7 8 9
Enter your command: w 0 0 9 9
9 * * * * * * * * * /
8 * * * * * * * * / *
7 * * * * * * * / * *
6 * * * * * * / * * *
5 * * * * * / * * * *
4 * * * * / * * * * *
3 * * * / * * * * * *
2 * * / * * * * * * *
1 * / * * * * * * * *
0 / * * * * * * * * *
0 1 2 3 4 5 6 7 8 9
Enter your command: w 9 0 0 9
9 * * * * * * * * /
8 * * * * * * * / *
7 * * * * * * / * *
6 * * * * * / * * *
5 * * * * / * * * *
4 * * * * / * * * *
3 * * * / * * * * *
2 * * / * * * * * *
1 * / * * * * * * *
0 / * * * * * * * *
0 1 2 3 4 5 6 7 8 9
Enter your command: w 4 2 4 4
9 * * * * * * * * /
8 * * * * * * * / *
7 * * * * * * / * *
6 * * * * * / * * *
5 * * * * / * * * *
4 * * - - + * * * *
3 * * * / * * * * *
2 * * / * * * * * *
1 * / * * * * * * *
0 / * * * * * * * *
0 1 2 3 4 5 6 7 8 9
Enter your command: w 4 6 4 1
9 * * * * * * * * /
8 * * * * * * * / *
7 * * * * * * / * *
6 * * * * * / * * *
5 * * * * / * * * *
4 * - - - + + - * * *
3 * * * / * * * * *
2 * * / * * * * * *
1 * / * * * * * * *
0 / * * * * * * * *
0 1 2 3 4 5 6 7 8 9
Enter your command: w 8 6 0 6
9 * * * * * * * * /
8 * * * * * | * / *
7 * * * * * | / * *
6 * * * * * + * * *
5 * * * * / | * * *
4 * - - - + + + * * *
3 * * * / * * + * * *
2 * * / * * * | * *
1 * / * * * * | * *
0 / * * * * * | * *
0 1 2 3 4 5 6 7 8 9
Enter your command: a r 3
10 * * * * * * * * /
9 * * * * * | * / *
8 * * * * * | / * *
7 * * * * * + * * *
6 * * * * / | * * *
5 * - - - + + + * * *
4 * * * / * * + * * *
3 * * * * * * * * * *
2 * * / * * * | * *
1 * / * * * * | * *
0 / * * * * * | * *
0 1 2 3 4 5 6 7 8 9
Enter your command: a c 6
10 * * * * * * * * * /
9 * * * * * * | * / *
8 * * * * * * | / * *
7 * * * * * * + * * *
6 * * * * / * | * * *
5 * - - - + + * + * * *
4 * * * / * * * + * * *
3 * * * * * * * * * * *
2 * * / * * * * | * *
1 * / * * * * * | * *
0 / * * * * * * | * *
0 1 2 3 4 5 6 7 8 9 10
Enter your command: d r 10
9 * * * * * * | * / *
8 * * * * * * | / * *
7 * * * * * * + * * *
6 * * * * / * | * * *
5 * - - - + + * + * * *
4 * * * / * * * + * * *
3 * * * * * * * * * * *
2 * * / * * * * | * *
1 * / * * * * * | * *
0 / * * * * * * | * *
0 1 2 3 4 5 6 7 8 9 10
Enter your command: d c 5
9 * * * * * | * / *
8 * * * * * | / * *
7 * * * * * + * * *
6 * * * * * | * * *
5 * - - - + * + * * *
4 * * * / * * + * * *
3 * * * * * * * * * *
2 * * / * * * | * *
1 * / * * * * | * *
0 / * * * * * | * *
0 1 2 3 4 5 6 7 8 9
Enter your command: r 4 5
3 * * * * *
2 * * / * *
1 * / * * *
0 / * * * *
0 1 2 3 4
Enter your command: e 1 1
3 * * * * *
2 * * / * *
1 * * * * *
0 / * * * *
0 1 2 3 4
Enter your command: r 7 7
6 * * * * * * *
5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * / * * * *
1 * * * * * * *
0 / * * * * * *
0 1 2 3 4 5 6
Enter your command: s ex.txt
6 * * * * * * *
5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * / * * * *
1 * * * * * * *
0 / * * * * * *
0 1 2 3 4 5 6
Enter your command: r 1 1
0 /
0
Enter your command: l ex.txt
6 * * * * * * *
5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * / * * * *
1 * * * * * * *
0 / * * * * * *
0 1 2 3 4 5 6
Enter your command: q

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#include <stdlib.h>
#include <stdio.h>

#include "Panel.h"

Position aPosition(int r, int c) {
    Position p;
    p.c = c;
    p.r = r;
    return p;
}

/**
*
* @param row
* @param col
* @return pointer to a panel
*/
Panel * newPanel(int row, int col) {
    Panel * p;
    char ** cells;
    // default dimension will be used
    // when input is illegal
    if (row < 1 || col < 1) {
       row = DEFAULT_SIZE;
       col = DEFAULT_SIZE;
    }

    p = malloc(sizeof (Panel));
    p->row = row;
    p->col = col;

    // create row
    cells = (char **) calloc(row, sizeof (char*));
    int i;
    int j;
    for (i = 0; i < row; i++) {
       // create array of column in row
       cells[i] = (char *) calloc(col, sizeof (char));
       for (j = 0; j < col; j++) {
            // initialize them to *
            cells[i][j] = EMPTY;
       }
    }
   
    // mark the address of the array
    p->cells = cells;
    return p;
}

/**
* free all allocated memory of the board
* @param p
*/
void freePanelCells(Panel * p) {
    int i;
    for (i = 0; i < p->row; i++) {
       free(p->cells[i]);
    }
    free(p->cells);
}

/**
* free all allocated memory
* @param p
*/
void freePanel(Panel * p) {
    freePanelCells(p);
    free(p);
}

/**
* print to screen
* @param p
*/
void displayPanel(Panel * p) {
    int i;
    int j;
    int row = p->row - 1;
    int col = p->col;
    char ** cells = p->cells;
    for (i = row; i >= 0; i--) {
       //printf("%-2d", i);
       printf("%d", i);
       for (j = 0; j < col; j++) {
            //printf("%3c", cells[i][j]);
            printf("%c", cells[i][j]);
       }...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$60.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats