COP 3223 Assignment 1 to 4 solutions

$100.00

Original Work ?

Download Details:

  • Name: Assignments-w18m2i.zip
  • Type: zip
  • Size: 109.77 KB

Category: You will Instantly receive a download link upon Payment||Click Original Work Button for Custom work

Description

5/5 - (1 vote)

COP 3223 Assignment 1 (Parts A, B, and C)

PART  A (25 points)

 

Objectives

  1. To give students practice at typing in, compiling and running simple programs.
  2. To learn how to read in input from the user.
  3. To learn how to use assignment statements and arithmetic expressions to make calculations

 

Introduction: Programmers for a Better Tomorrow

Programmers for a Better Tomorrow is an organization dedicated to helping charities, medical societies, and scholarship organizations manage various tasks so that they can focus on making the world a better place!  They have asked you and your classmates to help them develop some new programs to benefit their organizations.

 

Problem: Knitting for Fun and Non-Profit (knit.c)

One of the programs that Programmers for a Better Tomorrow supports knits hats, sweaters, and blankets for people who may not have enough to keep them warm.  These may be sent to other places in the country or overseas.

 

In this program, you will ask the user how many balls of yarn they have on hand and how many yards of yarn each ball contains.  Then, based on the total number of yards the user has, your program can determine how many hats OR blankets can be made.

 

It takes 220 yards to make a hat and 460 yards to make a sweater.  If the user has 500 total yards, they would be able to make two hats or one sweater.  Your program should print this information for the user.

 

Input Specification

  1. The number of balls of yarn the user has, n, where n is an integer greater than zero.
  2. The number of yards in each ball, y, where y is an integer greater than zero.

 

Output Specification

Output the result using the format below:

 

You can make X hats or Y sweaters.

 

Output Sample

Below are some sample outputs of running the program. Note that these samples are NOT a comprehensive test. You should test your program with different data than is shown here based on the specifications given above.

 

In the sample run below, for clarity and ease of reading, the user input is given in italics while the program output is in bold. (Note: When you actually run your program no bold or italics should appear at all. These are simply used in this description for clarity’s sake.)

 

Sample Run #1

How many balls of yarn do you have?

 6

How many yards are in each ball of yarn?

100

 

You can make 2 hats or 1 sweaters.

 

Sample Run #2

How many balls of yarn do you have?

 5

How many yards are in each ball of yarn?

50

 

You can make 1 hats or 0 sweaters.

 

Sample Run #3

How many balls of yarn do you have?

 3

How many yards are in each ball of yarn?

425

 

You can make 5 hats or 2 sweaters.

 

 

Deliverables

One source files – knit.c – is to be submitted over WebCourses.

 

Restrictions

Although you may use other compilers, your program must compile and run using DevCpp. So,

Remember to add the stdlib and pause lines. Your program should include a header comment with the following information: your name, course number, section number, assignment title, and date. Also, make sure you include comments throughout your code describing the major steps in solving the problem.

 

Grading Details

Your programs will be graded upon the following criteria:

 

1) Your correctness

 

2) Your programming style and use of white space. Even if you have a plan and your program works perfectly, if your programming style is poor or your use of white space is poor, you could get 5% deducted from your grade.

 

3) Compatibility – You must submit C source files that can be compiled and executed in a standard C Development Environment.  If your program does not compile, you will get a sizable deduction from your grade.

 

 

 

 

PART  B (35 points)

 

Objectives

  1. To give students practice at typing in, compiling and running simple programs.
  2. To read in input from the user, use assignment statements and arithmetic expressions to make calculations, and
  3. To learn how to use an if-statement.

 

Introduction: Programmers for a Better Tomorrow

Programmers for a Better Tomorrow is an organization dedicated to helping charities, medical societies, and scholarship organizations manage various tasks so that they can focus on making the world a better place!  They have asked you and your classmates to help them develop some new programs to benefit their organizations.

 

Problem: Knitting for Fun and Non-Profit –TWO (knittwo.c)

The left over pieces from each batch needs to be minimized. So, when a batch of yarn is sent over, it is important to calculate the left-over portion for the hats and the left-over portion for the sweaters. On comparing the two left-over portions, if they are equal, a message should be printed to this effect, otherwise print out the item that will leave the smaller left-over portion.

 

Input Specification

  1. The number of balls of yarn the user has, n, where n is an integer greater than zero.
  2. The number of yards in each ball, y, where y is an integer greater than zero.

 

Output Specification

Depending on  the left-over quantity,  the program should say one of the following statements:

 

You can make X hats with zz yards  left-over.

Or

You can make Y sweaters with zz yards left-over.

Or

You can make X hats or Y sweaters. In both cases,  the same amount of zz  yards is left-over.

 

Output Sample

Below are some sample outputs of running the program. Note that these samples are NOT a comprehensive test. You should test your program with different data than is shown here based on the specifications given above.

 

In the sample run below, for clarity and ease of reading, the user input is given in italics while the program output is in bold. (Note: When you actually run your program no bold or italics should appear at all. These are simply used in this description for clarity’s sake.)

 

Sample Run #1

How many balls of yarn do you have?

 6

How many yards are in each ball of yarn?

100

 

You can make 1 sweaters with 140 yards left-over.

 

Sample Run #2

How many balls of yarn do you have?

 3

How many yards are in each ball of yarn?

425

 

You can make 5 hats with 175 yards left-over.

 

Sample Run #3

How many balls of yarn do you have?

 51

How many yards are in each ball of yarn?

100

 

You can make 23 hats or 11 sweaters. In both cases, the same amount of 40 yards is left-over.

 

 

Deliverables

One source files – knittwo.c – is to be submitted over WebCourses.

 

Restrictions

Although you may use other compilers, your program must compile and run using DevCpp. Your program should include a header comment with the following information: your name, course number, section number, assignment title, and date. Also, make sure you include comments throughout your code describing the major steps in solving the problem.

 

Grading Details

Your programs will be graded upon the following criteria:

 

1) Your correctness

 

2) Your programming style and use of white space. Even if you have a plan and your program works perfectly, if your programming style is poor or your use of white space is poor, you could get 5% deducted from your grade.

 

3) Compatibility – You must submit C source files that can be compiled and executed in a standard C Development Environment.  If your program does not compile, you will get a sizable deduction from your grade.

 

 

 

PART  C (40 points): Repeating the Program in part  B several times

 

Objectives

  1. To give students practice at typing in, compiling and running simple programs.
  2. To read in input from the user, use assignment statements and arithmetic expressions to make calculations, to learn how to use an if-statement, and
  3. To learn how to use a simple for-loop.

 

In this part, you must embed all of Part B in a FOR-Loop that runs N times.

At the start of Part C,  prompt for an integer value of N, read it in and run the loop. So, if N is typed in at the prompt as 2,  your solution to Part B will first run once, prompting for

Numballs and Numyards, and will give its result; then, it will prompt a second time

and read in a set of two values (Numballs and Numyards)  and give a (second) result.

To limit the difficulties with the number of asterisks printed, you should assume that the user will type in a single digit for N.

 

 

Sample Run #1

Homany times do you wish to run the program?

2

 

This is Run 1

*************

 

How many balls of yarn do you have?

 6

How many yards are in each ball of yarn?

100

 

You can make 1 sweaters with 140 yards left-over.

 

This is Run 2

*************

 

How many balls of yarn do you have?

 3

How many yards are in each ball of yarn?

425

 

You can make 5 hats with 175 yards left-over.

 

 

 

Deliverables

Submit your program called loopknit.c, submit it over WebCourses.

 

Note: do NOT submit .exe files; and do NOT submit .cpp files; ONLY submit .c files.

 

Restrictions and Grading Details: Same as for Parts A and B.

COP3223 Intro to C – Programming Assignment 2

Problem A: Roller Coaster Design (coaster.c) (30 points)

Every great theme park has a signature roller coaster. In designing a roller coaster, we must decide how many trains of cars need to be placed on the track. It turns out that no more than 25% of the track can be occupied with these trains to adhere to safety regulations. Thus, if the track is 1000 feet long, and a train is 42 feet long, then up to 5 trains can fit on the track. (To see this, note that 5 trains have a total length of 210 feet, and this value is 21%, less than or equal to 25%, of the total track length of 1000 feet. Note that 6 trains would take up 252 feet, or 25.2% of the track, which is too much.)

 

In a train of cars, the first car is 10 feet long and all subsequent cars are 8 feet long. All cars seat up to 4 people. Since each of these values is constant, please use the following constants to store them:

 

#define FIRST_CAR_LENGTH 10

#define NORMAL_CAR_LENGTH 8

#define CAR_CAPACITY 4

 

For this program, the user will enter the total length of the track and the maximum length of the trains for the track. It is assumed that the trains formed will be as long as possible. For example, if the user enters 30 for the maximum length of the train, then the actual trains will have three cars and be of  length 26, since a four car train would exceed 30 feet. Your program should calculate the number of people that can be supported on the track at one time. (Note: It may be the case that more people can be supported by making a shorter train than possible, but for this particular assignment, maximize the size of each train.)

 

Input Specification

  1. The total length of the track will be a positive integer (in feet) less than 10000.
  2. The maximum length of a train will be a positive integer in between 10 and 100.

 

Output Specification

Output the maximum number of passengers on the roller coaster at any one time with a single statement of the following format:

 

Your ride can have at most X people on it at one time.

 

Early Output Samples

At first, two sample outputs of running the program are included below. Note that these samples are NOT a comprehensive test. You should test your program with different data than is shown here based on the specifications given above.

 

Sample Run #1

What is the total length of the track, in feet?

1000

What is the maximum length of a train, in feet?

42

Your ride can have at most 100 people on it at one time.

 

(Note: Each train has 5 cars on it, and each car has 4 people, at most. Thus, 20 people can sit in one train. As previously established, 5 trains is the maximum for this track, so the maximum number of people this roller coaster can support is 100.)

 

Sample Run #2

What is the total length of the track, in feet?

1000

What is the maximum length of a train, in feet?

49

Your ride can have at most 100 people on it at one time.

 

 

Sample Run #3

What is the total length of the track, in feet?

1000

What is the maximum length of a train, in feet?

18

Your ride can have at most 104 people on it at one time.

 

 

 

Sample Run #4

What is the total length of the track, in feet?

1000

What is the maximum length of a train, in feet?

59

Your ride can have at most 112 people on it at one time.

 

 

 

 

Problem B: Roller Coaster Redesign (coasterTwo.c)(50 points)

Your boss has noticed that maximizing the length of the train does not always maximize the number of passengers. She’s come up with a great idea for you to improve your program, so that you can calculate the actual maximum number of passengers the roller coaster can support. Her idea is as follows:

 

Let’s say the maximum length of a train is 55. Then, we can simply start by trying out a train of length 10 (one car), and seeing how many passengers such a train would support. Then, we can try a train of length 18 (two cars) and recalculate the number of passengers this design would support. If this is better than the best design we’ve seen so far, simply save this new value. Continue in this fashion, until we’ve tried all possible trains. In this situation, we would try trains of lengths 10, 18, 26, 34, 42, and 50. (We stop here because the next train, of length 58, would be too long.) In each of these candidate lengths, compute the following quantity: total rollercoaster passengers divided by the total length of all the trains. Store each  value in an array, so you will be storing as many values as there are lengths. Finally, compute the average of those values, and print it out as a final answer.

 

The user will input the same information as was inputted in Part A.

Note that this time you will not be inputting the number N, and you will not print the message about the surplus. This time, however, your program should output the actual number of maximum people the ride can support, the number of cars per train that achieves this maximum, and the final average of the passengers/length ratio.

 

Do the problem in small stages. First, ensure that you are getting the correct enumeration of the various train-lengths, then (for each train lengthcalc) compute how many trains can fit on the track; that gives the number of people on the track simultaneously, check if this beats the max-so-far, if so, replace max. For each train length choice, you need to compute the ratio of people to cumulative car length, and (it is a good idea to) store this in a one-dimensional array that can store decimal values. Finally, compute the average of the ratios, and print that out.

 

 

Input Specification

  1. The total length of the track will be a positive integer (in feet) less than 10000.
  2. The maximum length of a train will be a positive integer in between 10 and 100, inclusive.

 

Output Specification

The output should consist of three lines. The first line outputs the maximum number of passengers on the roller coaster at any one time with a single statement of the following format:

 

Your ride can have at most X people on it at one time.

 

The second line should output the number of cars in the train that achieves this maximum with a single statement of the following format:

 

This can be achieved with trains of Y cars.

 

Note: If there are multiple ways in which the maximum number of people can be supported, output the smallest number of cars that achieves this maximum. (Thus, if both 2 cars and 4 cars lead to 100 people on the ride and this is the maximum, then your program should output 2 cars.)

 

The third line should simply output the average value of people to length, as:

 

AVG Ratio: XXXX.XXX

 

 

Output Samples

Two sample outputs of running the program are included below. Note that these samples are NOT a comprehensive test. You should test your program with different data than is shown here based on the specifications given above.

 

 

 

Sample Run #1

What is the total length of the track, in feet?

1000

What is the maximum length of a train, in feet?

42

Your ride can have at most 112 people on it at one time.

This can be achieved with trains of 4 cars.

AVG Ratio: 0.451

 

 

(Note: The maximum is achieved when each train has 4 cars on it, and each car has 4 people, at most. Thus, 16 people can sit in one train. This train has a length of 34 feet, and since 34 feet x 7 = 238 feet, which is less than 25% of the total track length, this means that exactly 7 trains can be placed on the track at the same time. Thus, 16 people/train x 7 trains = 112 people total.)

 

Sample Run #2

What is the total length of the track, in feet?

4025

What is the maximum length of a train, in feet?

89

Your ride can have at most 480 people on it at one time.

This can be achieved with trains of 6 cars.

AVG Ratio: 0.467

 

Problem C: Practice Strings  (lastnames.c)(10 points)

 

Read in n, then n lastnames, and check to see if the first in the list is ever repeated again.

 

Sample Run #1

Enter n, followed by n Last names (each last name must be a single word):  

5 Reagan Bush Clinton Bush Obama

First name in list is not repeated.

 

Sample Run #2

Enter n, followed by n Last names (each last name must be a single word):

4 Bush Clinton Bush Obama

First name in list is repeated.

 

 

Part D: Embed all the above Parts in a menu (menu.c) (10 points)

 

Write this similar to bankinclass.c

 

Deliverables

Four source files:

 

1) coaster.c, for your solution to problem A

2) coasterTwo.c for your solution to problem B

3) lastnames.c for your solution to problem C

            4) menu.c for your solution to part D.

 

All files are to be submitted over WebCourses2, Canvas.

 

Restrictions

Although you may use other compilers, your program must compile and run using Dev C++. Your programs should include a header comment with the following information: your name, course number, section number, assignment title, and date. Include comments throughout your code describing the major steps in solving the problem.

 

Grading Details

Your programs will be graded upon the following criteria:

 

1) Your correctness.

 

2) Your programming style. Even if your program works perfectly, if your programming style is poor, you could get 5% deducted from your grade.

 

3) Compatibility to Dev C++ (in Windows).  If your program does not compile in this environment, you will get a sizable deduction from your grade.

COP 3223 Introduction to C – Programming Assignment 3

Objectives

 

  1. To reinforce your knowledge of ‘for’ loops and ‘while’ loops
  2. To utilize strings (character arrays) in C
  3. To implement logic in a program that is menu-based and is the seed for a complex, practical software system.

 

Problem: Food Bank Program

 

A Food Bank is a non-profit organization which distributes food items to people in need during local emergency food programs. People come in to a Food Bank and make their donations, and others might come in to make a request for any food item they want.

 

A Food Bank Program is used to keep track of the stock the Food Bank has (i.e., the donations), keep a record of requests that come in, fulfilling requests and printing the status report which consists of the amount of stock present in the food bank and the requests that are not yet fulfilled.

 

The program would require you to make two tables: Donations Table and Request Table. Each table will have two attributes: the Inventory Type, which is what type of food item it is, e.g., Milk, Meat, etc., and the Amount, which is the amount of that food item, e.g., 20, 30, etc. Note: For simplification, no units for amounts are considered.

 

Functionality Specification

 

  1. Add donations and requests to the tables.
  2. For any new donation that comes in, check if any donation is present in the donations table with same inventory type; if there is one then increase its amount by the new donation amount, and do not add a new entry in the donations table.  If not, then add a new entry in the donations table for that donation.
  3. Remove a request from the request table when it is fulfilled.
  4. If for a request the request amount is greater than the amount present, then partially fulfill that request by the amount present. And do not delete that request from the request table; just reduce its amount by the amount that is fulfilled.
  5. If in the process of fulfilling the request, the amount present for any donation becomes zero, then remove that donation from the donations table.
  6. Print the status report, containing stock currently available and requests that are not yet fulfilled.
  7. When the option is to Fulfill a Request, you are to only process the one request that is at the top of the Request Table (the oldest request). Thus, if it cannot be fulfilled, it will stall the system, until someone donates enough, so it can be satisfied later.

 

Input/Output Specification

 

At the beginning, the program should present the user with the available options, and ask for his/her preference. Based on the preference, the program should perform the corresponding task.

 

The user will have five choices:

  1. Enter a Donation
  2. Enter  a Request
  3. Fulfill the earliest Request
  4. Print status report
  5. Exit

 

When the user selects ‘1’, the program should ask the user for the inventory type and amount of the donation to be made. When the donation has been added to the donations table, the program should notify the user by printing out “Donation Added”.

 

When the user selects ‘2’, the program should ask the user for the inventory type and amount of the request to be made. When the request has been added to the requests table, the program should notify the user by printing out “Request Added”.

 

When the user selects ‘3’, the program should print “Request Fulfilled” if the request is completely fulfilled, print “Cannot be Fulfilled” if there is no donation of that type, and print

“Partially Fulfilled” if the donation type exists, but cannot completely satisfy the request.

If a request is fulfilled, it should be deleted from the Requests Table; if a donation is fully

used up in satisfying a request, the Donation Table entry should be deleted.

 

When the user selects ‘4’, the program should print the current Donations Table and the Requests Table.

 

When the user selects ‘5’, the program should print “Thank You for running the software. Bye for now”, and then the program exits.

 

 

Implementation Hints

 

If we assume that the maximum number of donated types at any time is 100, and that the

Maximum length of a description of the type is 19, then, our tables can be declared as:

char donations_inv_type[100][20];

int donations_amount[100];

You will need to have variables to keep track (count) of the total donated types, and total requests, in the two tables.

 

Use function ‘strcmp’ to compare if two strings are equal. Remember that when the two strings are equal this function returns 0.

 

Use function ‘strcpy’ to copy from one string variable to another.

 

While removing the request from the requests table once it is fulfilled, you will have to shift up the requests that are below that request in the request table, to take the place of the removed request. The same applies when you remove a donation, if the donation amount becomes zero.

 

For every new donation you will have to check if the inventory type of that donation is present in the donations table or not. For this, use the ‘strcmp’ function.

For a new request that comes in, you do NOT have to perform the check for whether any request is present in the request table with the same inventory type. Just add a new line for each new request in the requests table, as every request corresponds to a different family.

 

Assume that all inputs are perfectly correct, so you don’t have to do any error checking on the input.

 

Output Sample

 

Welcome to the Food Bank Program

 

  1. Add a donation
  2. Add a request
  3. Fulfill a request
  4. Print status report
  5. Exit

 

Enter your choice:_1_

 

Enter inventory type: MILK

Enter the amount: 20

 

Donation Added!

Press any key to continue . . .

 

Welcome to the Food Bank Program

 

  1. Add a donation
  2. Add a request
  3. Fulfill a request
  4. Print status report
  5. Exit

 

Enter your choice:_2_

 

Enter inventory type: MILK

Enter the amount: 5

 

Request Added!

Press any key to continue . . .

 

Welcome to the Food Bank Program

 

  1. Add a donation
  2. Add a request
  3. Fulfill a request
  4. Print status report
  5. Exit

 

Enter your choice:_1_

 

Enter inventory type: FRUIT

Enter the amount: 10

 

Donation Added!

Press any key to continue . . .

 

Welcome to the Food Bank Program

 

  1. Add a donation
  2. Add a request
  3. Fulfill a request
  4. Print status report
  5. Exit

 

Enter your choice:_1_

 

Enter inventory type: MILK

Enter the amount: 5

 

Donation Added!

Press any key to continue . . .

 

Welcome to the Food Bank Program

 

  1. Add a donation
  2. Add a request
  3. Fulfill a request
  4. Print status report
  5. Exit

 

Enter your choice:_4_

 

Printing the Donations Table

 

MILK 25

FRUIT 10

 

Printing the Requests Table

 

MILK 5

 

Press any key to continue . . .

 

Welcome to the Food Bank Program

 

  1. Add a donation
  2. Add a request
  3. Fulfill a request
  4. Print status report
  5. Exit

 

Enter your choice:_3_

 

——– Fulfilling Requests——–

 

Request Fulfilled

 

Press any key to continue . . .

 

Welcome to the Food Bank Program

 

  1. Add a donation
  2. Add a request
  3. Fulfill a request
  4. Print status report
  5. Exit

 

Enter your choice:_4_

 

Printing the Donations Table

 

MILK 20

FRUIT 10

 

Printing the Requests Table

 

 

Press any key to continue . . .

 

Welcome to the Food Bank Program

 

  1. Add a donation
  2. Add a request
  3. Fulfill a request
  4. Print status report
  5. Exit

 

Enter your choice:_5_

 

 

Thank You for using the software. Bye for now.

 

Press any key to continue . . .

 

References

 

Notes: Arrays, 2D Arrays, Strings

 

 

Deliverables

 

One source file: foodbank.c, for your solution to the given problem.

 

All files are to be submitted over WebCourses2/Canvas. (Do NOT submit .cpp files!!!)

 

 

Restrictions

 

Although you may use other compilers, your program must compile and run using Dev C++. Please use Dev C++ to develop your program. Your program should include a header comment with the following information: your name, course number, section number, assignment title, and date. Also, make sure you include comments throughout your code describing the major steps in solving the problem.

 

 

Grading Details

 

Your program will be graded upon the following criteria:

  1. Your correctness.
  2. Your programming style and use of white space. Even if you have a plan and your program works perfectly, if your programming style is poor or your use of white space is poor, you could get 10% or 15% deducted from your grade.
  3. Whether or not you have designed reasonable functions to solve the task at hand.
  4. Compatibility with Dev C++ (in Windows). If your program does not compile in this environment, you will get a sizable deduction from your grade.

 

COP3223 Introduction to C – Programming Assignment 4

Objectives

  1. Learn how to use 2-dimensional arrays and files (of pictures)
  2. Learn about how functions take arrays as arguments
  3. Learn how to use functions whose type is an array
  4. Learn how to manipulate the contents of an array

 

The arrays you will be working with represent pictures. In order to view the pictures you will work with, you need to install a free program called Irfanview.

  1. a) Using your web browser of choice, navigate to https://www.tucows.com/
  2. b) In the search field, enter Irfanview; click the Go button.
  3. c) The first choice should be the desired program; it will have the following description:

 

“This free image viewer and converter supports an extensive list of formats.”

 

Click on this entry.

 

 

  1. d) Click the “Download Now” button; follow the instructions provided by your web browser for the download.

 

 

  1. e) Run the downloaded file (iview436_setup.exe); follow all instructions.
  • When the installation wizard asks what file extensions you would like Irfanview to process, make sure to select the .pbm, .pgm, and .ppm extensions.

 

  1. f) In order to test your installation, download the chess1.pgm file from the class’s section 2 website and open it. A picture should appear.

 

 

Running the provided program (assign4.c)

 

Download the assign4.c, chess1.pgm, and face05.pgm files from the course page

(https://www.cs.ucf.edu/courses/cop3223/fall2013/section1)

– be sure to save them all into the same directory.  assign4.c is the main file that

includes functionality for reading images; this will be explained in detail later.  The other files – chess1.pgm and face05.pgm – are image files that will be processed.  Open the source file in your development environment of choice, compile it, and run it.  When the program prompts for an image file, enter either “chess1.pgm” or “face05.pgm”.  The program will then prompt for the path to which to save the processed image file.  You may enter any name you wish provided you do not enter chess1.pgm or face05.pgm; a possible name is “outputchess.pgm”.  Once the program finishes, it’ll close itself.  A new picture with the output filename you provided will be created in the same directory.  View the output picture and the respective input picture you entered first and try to spot the difference.  This difference will be explained more rigorously later, but for simplicity we can say that the color values in each pixel have been incremented by some predetermined value.  For this assignment, you will modify the assign4.c file to include new processing mechanisms.  As it stands, the file provided to you has functions to read an image, store it in an array, perform the aforementioned incrementing operation on each pixel, and write the output image to a file.

 

 

The details of the program

 

To start out, we first need to understand how the .pgm image format works and how it is stored in a file. (For a more detailed description of the .pgm file format, you can look at https://netpbm.sourceforge.net/doc/pgm.html .) The image file starts with  header information. Header information contains the image file type, in our case, “P5”, image size (width and height), and the maximum intensity value, in our case, 255.

Following the header are the details of the image pixels. Essentially, an image is nothing but a two dimensional array where each cell holds an intensity (sometimes called color or gray) value of that particular pixel in the image.  For this format, the intensity value is an integer between 0 and 255, inclusive.  A value of 0 indicates black, a value of 255 indicates white, and every number in between is a tone of gray.

 

8 9 9 9 9 . .
8 9 9 9 9 . .
9 8 8 8 9 . .
9 9 9 9 9 . .
8 9 8 9 9 . .
. . . . . . .
. . . . . . .

 

Intensity values of the chess1.pgm image

So, a .pgm file is simply a header followed by a list of ints (stored as chars), such as,

89999….89999….98889….99999…., etc.

 

When you open the assign4.c file, the first thing you should notice is the “img” variable.  The two stars between the variable type and the variable name indicate to the compiler that the image is a two dimensional array that can have any size.  The global variables numRows and numCols will specify how many rows and columns the image has.  Following these declarations are the function prototypes; some of these, such as addtopixels, writeoutpic, and readpic, are used directly from main while others, such as readHeader, isComment, readImgID, readImgSize, readMaxVal, and readBinaryData will only be called from another function.  setImage is a function that can (and will) be called both from main and other functions.  To better understand these functions, we should take a closer look at the main function.

The first operation performed by the main function involves reading the name of the image.  After this value has been read and stored, the readpic function is called with this filename; this function opens the file, calls the readHeader function in order to process the header information of the picture, and initially creates an empty image via the setImage function. (Aside: the setImage function’s technique of creating the empty image is interesting on its own, but beyond the scope of this class. You should think of its role as being mainly to declare an array, as in, “int somearrayname[numRows][numCols];” To make setImage practically do this, the caller, main or some other function, needs to have said “int** somearrayname;”, and then say “somearrayname=setImage(); The major advantage to getting the array by this approach is that we do not need to know the values of numRows and numCols at compile time.) The function then reads in the intensity values of the provided image file and saves these values in the cells of the empty image.  The image file is then closed, and the filled array is returned.  We will not discuss the specifics of the readHeader and other intermediary functions, since the details behind the behaviors of these functions are beyond the scope of this class.  Instead, we will focus on the functions that are called from main.

The second function that main calls is called addtopixels.  This function is provided as an example of the way in which we can process the individual intensity values of the picture.  addtopixels merely adds the input value – which is 103 in the example – to every element of the image array.  It basically brightens the image: if you increase an intensity value, it will get closer to white.  The functions you will write will be similar in nature to addtopixels – albeit a bit more complicated – so study it well.  Once the image has been processed, the resulting image array is saved using the writeoutpic function.  This function opens a file with the provided filename and writes to it the needed image header information.  The processed image array is then written to this file after the header.  Once the image has been written, the only remaining task is to free the memory previously utilized by the array; this is performed with the built-in function “free”.  If you can follow the order and logic behind these actions, you should be able to understand the assignment quite readily.

The Sobel Operator Function (Read this section last)

The sobel operator is used in image processing to detect the edges in an image; the details of this operator can be found at https://en.wikipedia.org/wiki/Sobel_operator. Basically, we will provide you with a function that computes the sobel operation on a picture, but we will only provide you with the function itself and an example illustrating how it can be called by main in the files sobelfunc.c and sobelcallfrag.c, respectively.  Open the sobelfunc.c file and copy the topmost line (concerning math.h), add add this line to your program.  This will allow your program to access C’s math library.  Next, add the sobel function to the bottom of your program file.  sobelcallfrag.c will allow you to test this function; open this file and copy the image creation lines (the two lines following the first comment) to the segment of your code right after you create your image array.  Put the rest of the sobelcallfrag.c file into your main.  You can now test the sobel operator by reading in an image, applying the sobel function to it, and writing the output to an output image file, all of which are essentially provided for you in the sobelcallfrag.c file.

Once you’ve successfully tested the sobel function, you will be fully prepared to read images, work on image arrays, highlight the edges of a picture with the sobel operator function, and output images to files.  From this point on, you are not required to, but are free to display your own talent and creativity regarding picture processing.

 

Program to submit

Start with assign4.c.  Make sure it runs and produces for you the brightened picture. Then, start to modify the program as follows. Make the program prompt a menu, and read in the user’s choice.

  • Option 1 should be: read name of picture and read picture, add (could subtract by providing a negative number) brightness: the value to be added should be prompted for, and read in.
  • Option 2 should be to Subtract pictures. This operation requires two picture files (which are prompted for), and will “subtract” the second picture from the first. Before the resulting picture is outputted, ensure that the absolute value of the subtraction operation at each pixel location is computed.  The subtraction option should be performed by a function whose parameters are pointers to the two pictures.  You must call the setImage() function to create the array that will hold the output picture – the provided sobel code fragment illustrates the manner in which you can call setImage to do what you need to do. Then, you should call the writeoutpic function to produce the output .pgm file.
  • Option 3 should be the Sobel edge highlighter: This operation will employ a function provided to you called sobel; this technique will highlight the edges of the provided picture. For details, see the section on Sobel.

 

Your program should contain one function for each of the operations as per the above guidelines.  Once the user has entered the desired operation and the required number of filepaths to pictures, call your function for that operation.  The output of all of the three cases must be stored in a new picture array that will be outputted to three fixed files titled out1.pgm, out2.pgm and out3.pgm respectively.

A variety of functions are being provided to you; among these are the sobel edge highlighter code and functions that will read in and store pictures, initialize a two-dimensional array of integers that will serve as your picture data type, and output such an array of integers as a .pgm file.  Moreover, a sample code fragment illustrating the use of the sobel function is being provided.  These functions are available on the course website.

 

Final Note

If you found this assignment to be particularly enjoyable or have creative ideas for image processing that you would like to implement, please see Dr. Lobo for further directions on picture processing activities at UCF.