Question
C programming is the main one and pick another code of choice!!! Background This assignment is based on one of the Big Ideas in this
C programming is the main one and pick another code of choice!!!
Background
This assignment is based on one of the Big Ideas in this course, namely that reading C source code is a real aid in learning how to write C code. To that end, in this project, you write a program that scans C source from the 'Net and calculates some simple statistics. We're learning lots of concepts, and by running the program you write here on several samples of C code you find on the 'Net (Internet), you can get a feel for how commonly used the various C keywords really are.
The idea is tentatively to quantify the percentage of each type of C language statements (if, while, variable declarations, etc.) and see how the programs vary on these dimensions. You pick the program to be analyzed from what you find on the 'Net (Internet), subject to the constraints that it should be primarily C, although there could be some C++ included, and your sample should be at least 1,000 lines long. Five thousand lines would be better, but see what you find.
While the programs whose source code you find will be quite complex, the program you write to compute these statistics need not to be. Because of the regularity of C grammar and a pretty much standard approach to using white space, identifying whether a line contains an if or an else or a long or a struct whatever, is pretty much a matter of scanning each string you read from the source sample for each keyword in turn easily done in C, using the strstr ( ) function, for instance.
Note the following demographic characteristics of the code you find:
-
url for the site
-
stated purpose of the code
-
date created/modified if possible
-
part of a larger system (yes or no if your sample is the complete program).
-
stated purpose of entire system (game, utility, operating system, device driver, web browser, etc. etc.)
Run your program once on a sample of C, and once on a sample of another "C-like" language. You can try any language you like, but the ones I think are most similar to C in syntax are Java and JavaScript. The scripting language Perl bears a strong resemblance as well. I think PHP will appear similar, as will C# and C++ (Duh). Languages such as Python and Lua will be much less similar, although for some very basic constructs, such as if and else, should still show non-zero counts.
There are two parts to completing this assignment:
1. You submit the code you develop as an assignment to receive credit. An assignment submission link appears in the folder containing this project.
2. You post the statistics you calculate for at least two language samples, one in C and one in another language of your choice.
Details
Select C/C++ code from 'Net. Try to find at least 500 lines (1,000 would be better).
Pick the source code to something of interest to you. Google Code, GitHub, and SourceForge are all possible sources as well.
Do a similar search to locate a comparatively-sized code sample in a non-C language, too.
Count and compute:
-
number of total lines
-
number and percentage of blank lines
-
number and percentage of comments (start with // or /*)
-
number and percentages of ints, longs, floats, doubles, char
-
number and percentages of if's
-
number and percentage of else's
-
number and percentage of for's
-
number and percentage of switch
-
number and percentage of semicolons
-
number and percentage of structs
-
number and percentage of arrays (contains [ or ], divide count by 2)
-
number of blocks (contains { or }, divide count by 2)
for each language sample.
If you can think of some other easy pattern to match, feel free to throw that in, too.
Demographic information.
This is background information about the context for the code you analyzed. Stuff like the purpose of the program, when it was written, and where you found it.
Record:
-
URL
-
stated purpose of the code
-
date created/modified if possible
-
part of larger system
-
stated purpose of entire system
Approach
Here are some hints for how to approach this problem:
Find other resources on the 'Net to explain how to do the following:
-
fopen ( ) and fclose ( ) a text file.
-
fgets ( ) each line into a char array. I think this will be easier than scanf ( ) here.
-
use C string functions to scan for each keyword the function named strstr ( ) is your friend
-
Count each keyword you find, tracking that using an int variable for each keyword.
-
Remember that you need to scan each line for all keywords, since a line can have, say an if AND a comment.
-
Compute percentages basically (number of X divided by total number of lines) * 100. You will need to use a double here to avoid losing everything to the right of the decimal.
-
Print all statistics on stdout (can redirect to a file for easy saving)
Testing
Have your program print each line that it matches, preceded by "blank:", "if:" and so on and then randomly check some to see if you are finding the right stuff, or being distracted by some irrelevant text. This will produce a lot of output, but if you save it in a file, you can open that file in your text editor, and search through it for each keyword, and see if what your program found matches what your editor locates.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started