# Computational Projects

# C - Computational

## C-COM-ReadFill (1 point)

### Read lines and fill matrix

Write a program that reads an arbitrary number of columns of numbers from a file and fills in a matrix with these numbers until you reach to the end of file.

Your code should also print out succesfully read number of lines and columns.

## C-COM-Sorting (2 points)

### Sorting algorithm

Read in a column of real numbers from a file. Sort the numbers and write the sorted numbers in a different file.

You cannot use a sorting algorithm from any package.

No shortcuts, you MUST write your own!

## C-COM-MultLarge (2 points)

### Multiply large numbers

Multiply two 30-digit numbers **not** using simple multiplication or math functions or libraries. This field is called "number crunching" but you should solve the problem in a simple way explained below. Numbers should not start with zero.

**Hint (the simplest way):**

n1="123456789012345678901234567890"

n2="234567890123456789012345678901"

Split each of these two numbers (strings n1 and n2) into two arrays (Note that naturally each character of strings can be accessed: n1[0] = '1')

Cross process each digit from these two arrays (i.e two numbers) accordingly.

Note that in your code

**only**perform multiplication of single digit numbers e.g 9 x 9 = 81You are not allowed to use powers of 10 in your calculation loop.

You have to leave the result as string (in case of example below: '1476')

123

x 12

-----

246 : 3x2=6 (1's), 2x2=4 (10's), 2x1=2 (100's)

+123 : 1x3=3 (10's), 1x2=2 (100's), 1x3=3 (1000's)

-----

1476

## C-COM-ColorToGray (2 points)

### Convert a color image into gray scale

The color coding of images on a screen is done in a pixel-by-pixel fashion. One way to code the color is to get the **RGB (red-blue-green)** code of the image pixel-by-pixel and use an algorithm to convert it to grayscale.

Write a code that does this conversion.

In order for this project to be complete, you must find a way to visualize both the color and the grayscale images.

You can use any python library to

**read**&**write**the image file (eg matplotlib). However, you**cannot**use library's own conversion functions.

## C-COM-RandomGen (2 points)

### Write a pseudorandom number generator

Random numbers are used very often in various branches of scientific computing. For instance, the Monte Carlo method aims to find the lowest energy configuration of a system by means of taking steps along random directions and testing whether each step causes an energy drop. Achieving truly random numbers, however, is a challenging task. For this reason, most computer languages provide functions that generate **pseudorandom numbers**, which are a sequence of numbers that simulate a true random sequence but are generated deterministically following a well-defined algorithm. There are several random number generator algorithms but one of the simplest ones is the middle-square method. Write a Python code, which generates a sequence of random numbers, each of 4 digits. Here are the steps to follow :

Start with a long integer, N. Take this to be the current time in milliseconds. (

**Hint :**You can use the time module.)Take the last four digits of N. This will potentially be your seed number, which initiates the sequence of random numbers.

Check if there are any zeros anywhere in the seed. If there are, create a new seed by means of finding the current time in milliseconds and taking the last four digits. When you get a four digit number that doesn't have any zeros in it, this will be your seed.

The reason why we are avoiding zeros is that their presence ruins the randomness of the sequence to be generated.

Take the seed and take its square.

Take the middle four digits; this is your new seed.

Take the new seed and take its square, take the middle four digits; this is your new seed.

Continue in this manner and collect the first 500 numbers in your sequence in an array.

Plot a histogram of these numbers and see whether they are really randomly distributed.

## C-COM-AntiAlias (3 points)

### Anti-aliasing

Anti-aliasing is a method in image processing for smoothing out rough edges. Your computer screen is made out of pixels and each pixel is assigned a color code. The ensemble of all the pixels then make up the images on your screen. Consider the circle on the left in the figure below. When it is run through an anti-aliasing algorithm, the pixels in the boundary between the foreground (blue) and background (white) colors are filled with hues inbetween.

Write an anti-alising program. Your code should contain the following functions.

make_circle : Construct a quarter circle like the one above using a primary color such as red, blue or green as foreground and white as background. This should be presented as a square matrix of numbers. Each matrix element will then represent the corresponding pixel.

antialias : This function should go over the matrix of color codes and replace the color code of each "pixel" and replace it with the average of its immediate neighbors. The corner and edge pixels will have fewer neighbors.

visualize : This function will take any matrix and convert it to a png file. For this function, you will need to use some appropriate libraries.

The main body of the code should start with the following parameters :

N : The size of the figure. Your matrix of color codes will be NxN.

Npass : It is possible to make your image progressively smoother by applying the anti-aliasing algorithm multiple times. This parameters decides how many times the algorithm will run through the matrix.

NOTE that you can always propose alternative ways of designing this algortihm. Talk to your instructor.

# C - String Manipulation

## C-STR-StringGraph (3 points)

### String graphics

Read two columns of data from a file. Take the first column as X and second column as Y axis. Display the data so that X axis runs/flows down and the number on the Y axis is scaled to fit in horizontal 80 characters of ’*’ (depending on the maximum value of the axis)

**Hint:**

Make a subroutine for the function i.e. give a number (x) it returns value of this function at that point (y).

Now in a 2D array, arranged according to your canvas size, locate this point P(x,y) according to your scale on x and y axes.

Note that project’s key point is arranging the scales of the axis on the canvas.

Corresponding string graphics is given below/right.

**Example:**

6| **

5| **

4| **

3| **

2|**

1|

+----------

1 2 3 4

## C-STR-PlotterSim (3 points)

### Plotter simulator

You have a pen plotter device that moves both in x and y directions. When it is instructed, it puts a dot at (x, y). The single unit move is 1 pixel. The location information can also be given to the plotter from a double array e.g. C[x][y]. When it is looped over this array, the plotter has to be moved to locations containing pixel values of 1 and puts a dot. Note that plotter’s initial position is (0, 0) and it cannot move to absolute positions, it accepts only increments from the current location.

Write a program that plots a function to the plotter. It has to display (using python's plotting library) the whole plotting area for each increment.

**Hint:**

Assume that plotter’s printing size is 80 x 80 pixels.

Therefore scale all functions to this size.

## C-STR-HangmanSim (3 points)

### Hangman simulator

Remember the game "hangman" where the computer grabs a word from a dictionary and plays a guessing game with the user. By entering one letter at a time, the user must guess the word before the man hangs. There is no set "correct" way to do this. Get creative!