Starting from:

$25

CSE031-Lab 1 Introduction to C Solved

Coding 1: Create main.c
Make sure you are in your Lab_1 directory, type gedit main.c and press enter (if you get an error, you may need to install gedit by typing sudo apt install gedit on the terminal before using it).  The gedit text editor will be displayed.  Note that gedit is only available in a Linux system with graphic interface. You may want to use other text editors (vi, nano, sublime, etc.) if terminal is the only interface to access a Linux system, or if you are using Windows or a Mac. Ask your TA if you are not sure about this.

 

Copy the following code to your file:

 

 #include <stdio.h> 

 int main() 

 { 

  printf("Welcome to CSE 31!\n"); 

  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 (\n). 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++).

 

TPS activity 3: Discuss questions 1 – 9 with your TPS partners in your assigned group (15 minutes) and record your answers in tpsAnswers.txt under a section labelled “TPS 3”:

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
A common punishment for school children is to write out the same sentence multiple times. Individually create a C program (punishment.c) that will write out the following sentence the number of times specified by the user (without the quotes, unless explicitly mentioned): “Programming in C is fun!”. Your program will ask the user for the number of times to output the punishment phrase using the following prompt (notice the space at the end of the prompt): “Enter the number of times to repeat the punishment phrase: ”. If an invalid value is entered (think about what would constitute an invalid value), your program should output You entered an invalid value for the number of repetitions! and continue asking the user for an input till a valid one is entered. To make this program “realistic”, it will introduce a typo during a certain repetition. It will ask for the repetition during which to introduce a typo using the following prompt: “Enter the repetition line where you want to introduce the typo: ”. Once again, you should check that the value entered by the user is valid (think about what would constitute an invalid value).  If the value is invalid, display You entered an invalid value for the typo placement! and continue asking the user for an input till a

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.

 

TPS activity 4: Work with your TPS partners in your assigned group to write the pseudocode together (20 minutes) in a text file named pseudoAverages.txt. Make sure to only discuss the pseudocode. You must write the C code individually.

 

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. 
 

More products