Question
This project will allow you to use many of the core elements of the C language to both see how they work as well as
This project will allow you to use many of the core elements of the C language to both see how they work as well as learn how they relate to how we add logic to a C program. The goal in this lab is to build a C program that will use the following topics:
-
Compiling
-
Looping
-
Input / output (not files)
-
Conditional testing
-
Functions
It is highly recommended that you start out with very small pieces of code that will enable you to understand a single topic as well as be able to debug your code along the way. This is a form of an agile process where you build upon previous versions to improve your program over time.
Along with an agile/iterative approach, it is also highly recommended that you make copies of your code along the way so you can go back to a previous version if you get stuck or figure out a better way to improve your code over time - this will demonstrate a form of refactoring code.
Your program will need to do the following to achieve full credit.
-
Start program with a command line switch that enables/disables debugging
-
If your program is in debugging mode, you will need to have various places in your code where you are doing I/O that output what was either read in, or the output of a function that is not going to be directly output via normal print statements.
-
-
Prompt a user for input in the form of a menu that does not exit until the user enters the selection for exit. The input selection will be managed with a switch statement in C.
Menu:
1) Add 2 numbers (integers)
2) Multiply 2 numbers (floats)
3) Read in string and reverse the string as output
4) Output the current date and time
-
Based on the input, call a function based on the input - one function per menu selection
-
Run the function and if needed, prompt the user for additional information to be used in that function for output
-
Read input and convert menu selection(s) into integers/floats (if needed)
For the functions:
-
Adding 2 numbers - will require you to prompt for 2 numbers, read them in, convert to integers, add them together and output the result
-
Multiply 2 numbers - will require you to prompt for 2 floating point numbers, read them in, convert them to floating point variables, multiply and output with 3 decimal points of precision
-
Read in a string and reverse - you will prompt for a string (no whitespace characters), read it into a variable, reverse using a for loop that will output the string in reverse
-
Output the current date and time - using C preprocessor macros, output the current date and time
For the command line switch, you will be collecting data to enable/disable debugging and using that variable in conditional testing to output debugging info or not. An example of how you will collect from the command line might look like:
$ lab1 debug
or
$ lab1 no-debug
The data will be collected into a variable named argv[1].
You will also need to test that the user entered an argument by checking the argument count with the variable argc. If the user did not enter a command line argument you will need to output the error to the user as a usage statement that tells the user what they did wrong in the form of :
$ lab1
Error: Usage: lab1 [debug | no-debug]
And when you print this message you will need to use the argv variable for the program name vs. hardcoding it in your program.
When your program is in debug mode, you will have various statements around your code where you are collecting input, calculating data for output, or entering / exiting functions, etc. This will allow you to follow the logic as your program executes. This is an important piece of learning the C language as there are many low level concepts that are not fully understood by just running a program. These low level concepts will be covered in class, but a few of them are:
-
What a string really is in the C programming language
-
How I/O is performed and relates to variables (default types, where data is stored, etc)
-
Ability to see logic flow throughout the program to identify defects/bugs where they started vs where they may be identified (i.e. output)
Learning objectives:
-
Compiling and running a C program
-
Input and output for standard in and standard out
-
Command line arguments
-
Looping
-
Conditional statements
Requirements
Design document
Please create design doc in text format and name it README.txt - not MS Word/etc
You are required to start with a design document that will define your program. The document will need to define at least the following elements:
-
Who wrote the program (ensure all members are listed in the doc)
-
Program description paragraph
-
Menu definition and usage
-
Each function defined with
-
Input type(s) (if any)
-
Output type
-
Algorithm description
-
-
Listing of files required and what type they are (.h header, .c c code, etc..)
Grading Criteria
You will need to submit a single file (zip or tarred) inclusive of all of your source code including header files (or any additional files), your design doc, and a test script.
Your test script will be the output of the command script. Script is a command line program that once run, will copy your output to your screen to a file. An example of how to use script is as follows:
$ script Project1.txt
NOTE: This will start the program and everything that happens on your screen from now until you type exit will be included in the file named Project1.txt. If you do not provide an argument to script it will create a file called typescript. When you type exit you will not be exiting your shell, rather the script program that is copying all of your output to your file. So the proper use of script for project output would be the following algorithm.
-
Be ready to run through all test cases knowing they work
-
Enter the script program
-
Run through all test cases of your project
-
When done, type in exit to leave the script program
-
Include the script output file in project submission
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