Description
CSE-031-01 Points: 20
Overview
The goal of this lab is to refresh your programming skills and create simple programs that will display output to the console, read input from the user, use conditional and loop statements in C, and perform some simple error handling.
Note: You need to have a separate program for each of the parts of this lab. When you submit your assignment through CatCourses, make sure that ALL PARTS are included.
Getting Started
In this class, we will use the Linux terminal extensively, so you should get familiar with it as much as possible (there is plenty of information online). We recommend setting up a smart directory structure to save your assignments throughout the semester, but it is up to you about how you want to save your work. We will setup a CSE31 directory on the Desktop, a directory for the lab (i.e. Lab_1) inside it, and a directory inside the lab directory for each part of the lab (i.e. part1, part2, etc…). Note that all the files shown in green below are the ones you will be submitting for this assignment.
You must have a clear idea of how to answer the lab activities before leaving lab to receive participation score.
Tutorial in Linux
As a software engineer (or a normal human), it is impossible to remember all the commands for Linux terminal. It is particularly true for those that you don’t use Linux often. So, where can you find the resources?
your answers to all the TPS questions that follow in the lab handout):
1. Record your TPS partners’ names.
2. Independently search the internet for 3 online tutorials on how to use Linux terminal.
3. Share your tutorials with your TPS partners.
4. Bookmark your results in the browser of your computer.
Create Lab_1 directory
1. How can you open a terminal from your Linux computer?
a. Can you open more than 1 terminal at the same time?
b. Why do you think you want to open more than 1 terminal at the same time?
2. In the terminal, how can you tell what are contents inside the current directory (i.e., what is the command)?
3. From your current directory, how can you navigate to Desktop directory?
4. While you are in Desktop, create a new directory called CSE31. How do you do this?
Coding 1: Create main.c
Copy the following code to your file:
#include <stdio.h>
int main()
{
printf(“Welcome to CSE 31! “);
return 0;
}
Save your file and exit gedit. Congratulations, you have just written your first C program!
The first line, #include< stdio.h>, includes a library that allows you perform simple Input/Output (I/O) operations. In this program, the library allows you to use the printf statement. In the next line, int main(), we start the main function, which is a mandatory function for any C program. This is the function that first executes when the program is launched. The contents of the function’s code need to be surrounded by curly braces (lines 3 and 6). In line 4, we are printing the text Welcome to CSE031! to the screen, ending with a new line ( ). Finally, the last line returns from the main function, which will stop execution of the program.
Once you have created this file and understood its content, you want to compile the source file (main.c) into an executable so that you can run it on your computer. To compile, we will use GCC compiler (not G++ – it is used for C++).
1. Independently find 2 online references on how to use GCC in a Linux terminal.
2. Share what you have found with your partners and save your results in the bookmark of your browser.
You will refer to these references to answer the following questions.
3. What command do you type in the terminal to compile your main.c?
4. How do you know if your program has compiled successfully?
5. What does the –c flag do in gcc?
6. What does the –g flag do in gcc?
7. How do you change the executable name from main to cselab1?
8. What happens when you compile your code by typing gcc main.c only?
9. Now, let us run the program you have just compiled. What command do you use?
Coding 2: Create punishment.c
valid one is entered. When both inputs are correct, you should display the punishment phrase the correct number of times (Programming in C is fun!), making sure to change it to Progranming in c is phun! (the typo) during the repetition count defined/input by the user.
You will need to use scanf to process user inputs. Look it up online to find out how to use it. Please see the sample runs below. Your program must produce an output that exactly resembles the Sample Runs, including identical wording of prompts, spacing, input locations, etc.
Sample Runs (user input shown in blue, with each run separated by a dashed line):
——————————————————————————-SAMPLE RUN 1 Enter the number of times to repeat the punishment phrase: 4
Enter the repetition line where you want to introduce the typo: 1
Progranming in c is phun!
Programming in C is fun!
Programming in C is fun!
Programming in C is fun!
——————————————————————————-SAMPLE RUN 2 Enter the number of times to repeat the punishment phrase: 6
Enter the repetition line where you want to introduce the typo: 3
Programming in C is fun!
Programming in C is fun!
Progranming in c is phun!
Programming in C is fun!
Programming in C is fun! Programming in C is fun!
——————————————————————————-SAMPLE RUN 3 Enter the number of times to repeat the punishment phrase: -8
You entered an invalid value for the number of repetitions!
Enter the number of times to repeat the punishment phrase again: 0
You entered an invalid value for the number of repetitions!
Enter the number of times to repeat the punishment phrase again: 2
Enter the repetition line where you want to introduce the typo: 0
You entered an invalid value for the typo placement!
Enter the repetition line where you want to introduce the typo again: 3
You entered an invalid value for the typo placement!
Enter the repetition line where you want to introduce the typo again: 2
Programming in C is fun!
Progranming in c is phun!
Coding 3: Create averages.c
Create a new program that will ask a user to enter a number repeatedly. This program will calculate 2
averages based on whether the sum of digits in the inputted numbers are odd or even: avg_even (average of all inputs whose digits sum up to an even number) and avg_odd (average of all inputs whose digits sum up to an odd number). The program will stop when the user enters a ‘0’. For example, the numbers 2, 26 and 123 are those whose digits sum up to an even number (2, 2+6=8 and 1+2+3=6, respectively).
Before writing the program in C code, perform the TPS activity below to write a pseudocode to describe your approach to this problem.
Your program must produce an output that exactly resembles the Sample Runs, including identical wording of prompts, spacing, input locations, etc. Notice that the text in the input prompts, where the user enters an integer, changes each time — stating “1st integer”, “2nd integer”, “3rd integer”,
“4th integer”, etc. You will need to code this behavior into your program.
Sample Runs (user input shown in blue, with each run separated by a dashed line):
——————————————————————————-SAMPLE RUN 1 Please enter the 1st integer: 2
Please enter the 2nd integer: 26
Please enter the 3rd integer: 123
Please enter the 4th integer: 3
Please enter the 5th integer: -14
Please enter the 6th integer: 111
Please enter the 7th integer: 0
Average of inputs whose digits sum up to an even number: 50.33
Average of inputs whose digits sum up to an odd number: 33.33
——————————————————————————-SAMPLE RUN 2 Please enter the 1st integer: 92
Please enter the 2nd integer: -142 Please enter the 3rd integer: 7
Please enter the 4th integer: 36
Please enter the 5th integer: 0
Average of inputs whose digits sum up to an odd number: -1.75
——————————————————————————-SAMPLE RUN 3
Please enter the 1st integer: 0
There is no average to compute.
Collaboration
You must credit anyone you worked with in any of the following three different ways: 1. Given help to
2. Gotten help from
3. Collaborated with and worked together
What to hand in
When you are done with this lab assignment, submit all your work through CatCourses.
Before you submit, make sure you have done the following:
• Your code compiles and runs on a Linux machine (without the need for special libraries).
• Attached punishment.c, pseudoAverages.txt, averages.c, and tpsAnswers.txt.
• Filled in your collaborator’s name (if any) in the “Comments…” text-box at the submission page.




Reviews
There are no reviews yet.