Question

C programming is the main one and pick another code of choice!!! Background This assignment is...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
C programming is the main one and pick another code of choice!!! Background This assignment is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • WRITE A CODE IN C++ A CLASS WITH MEMBER FUCNTIONS THAT COUNTS THE LINES OF CODE...

    WRITE A CODE IN C++ A CLASS WITH MEMBER FUCNTIONS THAT COUNTS THE LINES OF CODE OF C++ FILE OR TEXT FILE .COMMENTS SHOULD NOT BE COUNTED AND SPACE BETWEEN LINES OF CODE SHOULD BE IGNORED AS WELL. ASK FOR INPUT FROM THE USER IT CAN BE CPP FILE OR TEXT FILE Assignment:Write a program to count lines of code according to your LOC Counting Standard. Notes: -Your program should accept as input the name of a file that contains...

  • Assignment Modify the code Binary File IO Example Code. The code reads and prints information abo...

    In java Assignment Modify the code Binary File IO Example Code. The code reads and prints information about a BMP file. Modify the code so that it also prints the resolution of the BMP file (both width and height) and the number of bits per pixel. Do not use any Java library or third-party library code that reads BMP files. You must read the file using simple Java code and extract the information, similar to the code in the example....

  • Hi the programming language for this assignment is C programming. Could you also please number the...

    Hi the programming language for this assignment is C programming. Could you also please number the answers exactly as they appear in the assignment? Thank you. (9) 1. An array of structures is needed to keep track of 50 grocery items containing the item name, item type, cost, quantity, and tax percentage for each grocery item. The following declarations have already been done: { struct grocery char name[20], type[15]: float cost, taxp: int quan: 3 Also, the following declaration has...

  • C++ Question Write a program that will calculate a student's year marks for all his subjects.

    Write a program that will calculate a student's year marks for all his subjects. The program must read the subject code, the assignment marks for two assignments and the percentage that each assignment contributes towards the year mark, for each subject that the student is registered for.Create an input file called assignments.dat that contains the following information for a specific student:The first field in each line represents the subject code, followed by the percentage that assignment 1 contributes towards the...

  • Please, please help! (Programming Assignment: fork gymnastics) Write a C/C++ program (call it string-invert) that takes...

    Please, please help! (Programming Assignment: fork gymnastics) Write a C/C++ program (call it string-invert) that takes a string argument from the command line and outputs the string in reversed order You have two constraints: Constraint 1: Each process can output at most one character. If you want to output more than a single character, you must fork off one or more processes in order to do that, and each of the forked processes in turn outputs a single character t...

  • The language is C++. Code needs to be added to line 28 and 37. Could someone...

    The language is C++. Code needs to be added to line 28 and 37. Could someone provide some help with how to do it? CODE Write a program to unscramble words A file with list of words is provided along with a template program to get you started. 1. Define a struct with 2 string members: sorted and original 2. Declare an array of 200000 elements with data type of the struct 3. Read from a file a list of...

  • I need help with this assignment in C++, please! *** The instructions and programming style detai...

    I need help with this assignment in C++, please! *** The instructions and programming style details are crucial for this assignment! Goal: Your assignment is to write a C+ program to read in a list of phone call records from a file, and output them in a more user-friendly format to the standard output (cout). In so doing, you will practice using the ifstream class, I'O manipulators, and the string class. File format: Here is an example of a file...

  • Please help with this python assignment. Thank you. question 1 Write a Python program to read...

    Please help with this python assignment. Thank you. question 1 Write a Python program to read a file line by line store it into a variable. question 2 Write a Python program to read a file line by line store it into an array. question 3 Write a python program to find the longest words. question 4 Write a Python program to count the number of lines in a text file. question 5 Write a Python program to count the...

  • Attached to this assignment as a separate document is the C++ code for a program that...

    Attached to this assignment as a separate document is the C++ code for a program that determines if a given string is a palindrome or not. A palindrome is a word that is spelled the same backward or forward. "bob" is an example of a palindrome. The program is sometimes a talking point during lecture, and may not fully adhere to the many bobisms I've been telling you about. In fact, it may not entirely work but that wasn't its...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT