CSCI 1320 - Assignment #6


These assignments will have you playing more with pointers and arrays since that is what we have been talking about. All of these assignments will also take command-line arguments instead of reading input from the user with scanf.

After each problem description I've given you a sample output file. You should produce something similar to my output.

As a side note, be sure to save the code from all the programs you do. It is quite possible that later in the semester other assignments will include things that overlap with them and your code will be reusable.

File Stats:

There is a utility in Linux called wc which stands for word count. You can run it on one or more files and it will tell you how many lines, words, and characters there are in the files. For this assignment option you will do the same thing, only I also want you to count how many time each letter occurs in each file (regardless of case). The files you should work on will be specified on the command line. Your program should read through each file and count up how many characters, words, and lines there are as well has how many times each letter occurs. You will print this information for each file followed by a grand total for all the files. Your program might be invoked as "a.out *.c", which would go through all the C program files in the current directory.

You will consider any string that is between whitespaces to be a word. All white spaces in ASCII are at or before the space character (' '), so use <=' ' for doing this counting. Check the provided output for details on what that should look like.

output

Symbolic Algebra:

We wrote simple little calculators in class and even a parser for Scheme expressions. Now we want to go a bit further to have you write something that could actually be useful. For this option, you will write a program that recursively parses a string for an arithmetic expression and returns the value of it. Examples would be 5+7 or 9*(5+7.5)/2. Your parser should do proper order of operations binding (things in parentheses bind highest, * and / before + and -, and go from left to right for the same level of priority).

The approach I want you to take for solving this problem is with a recursive algorithm that breaks the problem into pieces starting with the lowest priority operation. You will write a function double parse(char *exp,int start,int end);. This function will return the value of the expression in the string exp in the characters between indexes start and end. First it should find the lowest priority operator (it can't be in parentheses). If it finds one it recurses twice with the values of start and end that give the strings on either side of that operator. If there isn't an operator that isn't in paretheses you can check if the string starts with '(' and pull the bounding parentheses off and recurse on what is left. If it doesn't start with '(' you know that part of the expression is just a number it returns the value of the number.To get the value of a number you can use the atof function in stdlib.h.

The user will give you a formula, that doesn't include any spaces, at the command line and you should simply print the value it evaluates to. So a potential invocation of your program might be as follows: "a.out 5+3*(70/5)". I'm not providing a sample output file because this would simply output 47.

This assignment will lead into future assignments. It will be merged with the BASIC editor if you did that for assignment #5, but can be used for other things in other cases.

Mandelbrot Set:

This program will generate text files that can be displayed using the raster viewer I have given you previously to render sections of the Mandelbrot set. This is a rather famous fractal produced by a simple equation. This equation is z_n+1=(z_n)^2+c. The trick is that z and c are complex numbers (they have both real and imaginary parts. Also, z_0=c. For every point c in the complex plane, you can run through this series and it will either run off toward infinity or it won't. If it does run off it is not part of the Mandelbrot set. If it stays bound, it is part of the Mandelbrot set. Obviously, you can't run the equation forever so the user will tell you the maximum number of times to try. If the magnitude of z (distance from the origin in the complex plan) ever gets bigger than 2, you should stop the calculation and go to the next point because that one will just get larger and larger.

When the user runs your program he/she will provide seven command line arguments: number of pixels, maximum iterations, file name, real minimum, real maximum, imaginary minimum, and imaginary maximum. You will open the filename and print the number of pixels on the first line. Let's call the N. Then you will do N points across the row in the complex plane where the imaginary component is imaginary maximum and the real component goes from real minimum to real maximum. You will do this over again N times, making the imaginary value slightly smaller each time so you get to imaginary minimum at the Nth iteration. For each value you calculate you will output one integer, the number of iteration the magnitude stayed less than 2 (or maximum iterations if it stayed less than two that whole time). This file can be read in by RasterViewer to produce a black and white image of the set.

For extra credit, don't output the number of iterations. Instead, output an RGB encoded hex string (like in the raster option for assignment 4). You can decide how to vary the color, but pick something to make the points in the set stand out from the rest and try to make it colorful.

The first two output files were made with the following command:

a.out 400 150 fullMandel.txt -1.5 0.5 -1 1

int output : This file can be viewed using the RasterViewer with "java -jar RasterViewer -d 150 fullMandel.txt".

rgb output : This file can be viewed using the RasterViewer with "java -jar RasterViewer RGB fullMandelRGB.txt".

big rgb : This one shows a blowup in color.