Exercise 1a The printf function and strings 1. Compile and run the program that prints the text "hello world C programming" in a terminal. 2. Add a printf function to a new line with its own text that...

Exercise 1a The printf function and strings
1. Compile and run the program that prints the text "hello world C programming" in a terminal.
2. Add a printf function to a new line with its own text that consists of different
10
words separated by spaces.
3. In the first printf, replace in the string all the spaces with a \n. What is the effect? See C book, p. 34.
4. In the second printf, replace the spaces with a \t. What is the effect?
5. Print a \ (backslash) at the end of the second string.
6. With printf, print an integral value 1 with %d in a string and a floating point value 1.123 with %f. See C book, p. 36.
7. Print the floating point value 1.123 with 2 numbers behind the decimal point as well. See C book, p. 37.
8. Copy the line created with printf but swap the %d and %f without switching the numerical values. Why is this code incorrect? What is the output? Does the compiler give any error or a warning?
Exercise 1b Ohm's law calculations
Create the Exercise1b C-project. Enter the code for the exercises below to the main.c of Exercise 1a. Ohm's Law is the main focus: U = I × R.
1. Copy next code into main.c and read the comments. These comments describe the code you have to implement.
int main(void)
{
double U = 0.0;
double I = 10.0;
double R = 20000.0;
U = I * R;
printf("U = %.1lf V\n", U);
printf("I = %.1lf A\n", I);
printf("R = %.1lf Ohm\n\n", R);
/* Increase the resistance by 5000 Ohm, use +=
I remains the same value, recalculate U */
printf("U = %.1lf V\n", U);
printf("I = %.1lf A\n", I);
printf("R = %.1lf Ohm\n\n", R);
U = 1.5;
R = 1000;
/* Calculate I and print all values */
11
/* Make I three times larger and add 5, use *= and +=
U remains the same value,
calculate R, print all values */
/* Use one printf() invocation,
to print all values again */
return 0;
}
___________________________________________________________________
Exercise 2 Getting familiar with if-else statements
Create a new C project using QtCreator: Exercise2a. Clear the main code block in main.c (remove all statements, except for the return 0; at the bottom of the code block). We will reproduce new formulas in the code and see whether the results of calculations comply with certain logic criteria (see C book Chapter 11).
In QtCreator, it is possible to have several projects open at the same time. You do have to select which project is active. If we want to run a program, then this is the program in the active project. This also makes it easy to copy code fragments from one project to another.
Exercise 2a Fahrenheit and Celsius
Create a program with the formula that converts a Fahrenheit temperature value into a Celsius temperature value:
Celsius = 5.0 × ( Fahrenheit – 32.0 ) / 9.0
1. For the variables specified in the formula, declare double typed variables.
2. Implement the formula given in a C expression with clear names for the variables. Execute this formula three times with different values for Fahrenheit. Print the results.
3. If, after each conversion into Celsius degrees, the temperature is under 12 °C, the text "it's cool outside" must be printed.
4. If, after each conversion into Celsius degrees, the temperature is 12 °C or higher but not above 18 °C, the text "it's okay outside" must be printed.
12
5. If the above applies and the temperature is higher than 16 °C, then the text "it's perfect outside" must be printed. Perform this test in the if- code block of the previous point.
6. If, after each conversion to Celsius degrees, the temperature is 18 °C or higher, the text "it's hot outside" must be printed.
7. Implement the formula for the reverse conversion and test it with three different values for Celsius. Are the results correct?
Exercise 2b Reading a key pressed and deciding what to do
Create the Exercise2b project.
1. Declare a character type variable above in the main code block.
2. Read in one character with the scanf function, use %c in the format string. Which header file is needed for using this scanf function?
3. Determine whether this character is an 'a', 'b' or 'c' and then print the message “The character entered is an a, b or c”.
4. Then, with the isdigit function, determine whether the entered character is a number (see C book, page 172). In order to use this function, the right header file must be included. Which header file is this? Print a suitable message if it is a number and a suitable message if it is not.
5. If it is a number, determine whether the character entered is not "1" and not "9". Print a suitable message.
6. If we add the value 2 (integer) to the character entered, what is the result? Print this result with a suitable text.
7. Re-enter an input of one character by executing scanf. Use a switch-case statement (see C book Chapter 17) to print the text "up" for the value 'w', the text "left" for the value 'a', the text "right" for 'd' and the text "down" for 'x'.
For all other values, a text you have chosen must be printed in the default part of the switch-case statement.
8. Use the construction ?: (conditional operator, see C book p.116) to replace the value of the character last entered with a '0' if this is a number and smaller than or equal to a '5'; otherwise replace the character entered by a '9'.
Program four input possibilities for a character in a loop with scanf in order to show this by printing the result of the ?: construction.
___________________________________________________________________
Exercise 3 Getting familiar with repeat loops and strings
Create a new C project using QtCreator: Exercise3a. For all kinds of constants, use #define and an informative name for the constant. Place them after the #includes at the top in the program (see the document with C style programming guidelines).
Exercise 3a Strings (char array)
Strings are often used to save texts and to process them in applications that must
13
send texts to other systems and/or must place them on a display.
1. Create a string sized 25 characters (use a #define for the size). At the initialisation of the string, enter the following text: “Code number = 12345”.
2. Both in a loop and with the strlen function, determine how many numbers there are in the string. Use the standard isdigit function. Which header file is required for this? See C book Chapter 19.
3. In the already existing string, enter the text “abcDEFG” with the strcpy function. Replace all letters with upper case letters by using a loop and the strlen and toupper functions.
4. In a loop, print all characters of this string one after the other. Print the hexadecimal value behind each character as well. For example:
A 0x41
B 0x42
C 0x43
Check if the values showed, are correct by consulting an ASCII table.
Exercise 3b Calculation commands based on input
Create the Exercise3b project.
We often need to be able to give commands in applications in text form. Here, we choose commands that must be entered by pressing a keyboard button.
1. Declare two integers. Using an informative text, ask the user to enter the values. Then create a while loop where we can read a character with getchar. See C book, p. 165 to 169. Careful read the text ‘The Newline Consideration’.
2. If the character entered is a ’+’, we must add the two int variables and print the result. Do this for the operators ’-’, ’*’ and ’/’ with the related operation as well. Create all decisions in one switch-case statement.
3. If an ’s’ is entered, the loop must end and the program must end with the printing of the text “Program ended”.
4. If none of the keys mentioned are pressed but another one is, a clear error message must be given, prompting the user to press the correct operator keys (+, -, * or /).
___________________________________________________________________
Exercise 4 Arrays and mathematical functions
Create a new C project using QtCreator: Exercise4.
Exercise 4a Fahrenheit and Celsius table
1. Program the conversion from Fahrenheit to Celsius in a for loop. The range of
14
Fahrenheit is 10.0°F to 40.0°F in steps of 2°F. The result must be a table with the Fahrenheit value in the first column and the related Celsius value in the second column.
2. First, above the table, place the text: Fahrenheit Celsius.
3. The width of a column (print field) must be entered in the %lf in printf.
For example, for a print field width of 10 and two numbers after the decimal point: use %10.2lf. All the printed numbers in a column must align with the last character of the printed text above the column.
Exercise 4b Use of the int array
Expand the code of Exercise 4a (use project Excercise4).
4. Declare an int array of size 4 and initialise this entire array with 0.
5. Print an informative text using printf in order to ask the user for input. Every single value in the array must be requested from the user with scanf in a loop.
Clear after every scanf invocation the input buffer (stdin) to remove all entered (buffered) faulty characters for the next scanf:
while (getchar() != '\n')
{
/* empty loop */
}
6. Then calculate in a loop the sum of all of the array elements. Print the content of the array (values in a row on one line) and the sum. Use information texts to explain the output.
7. Enter a new int array (size 10) with the power of 4 (1, 4, 16, …) in a for loop. Info: 4 to the power of 0 is equal to 1. Use the pow function. Include the correct header file for the pow function.
8. Then determine the first value of the int array created that lies between 10 and 70. Print this value with the position in the array (index). Use information texts to explain the output.
Exercise 4c Root calculation in table format
Expand the code of Exercise 4a and b (use project Excercise4).
9. Create a double array and fill this array with the root of the numbers 1.0, 2.0, 3.0, up to and including 20.0. Use a while loop for calculating the root for every value with sqrt. Include the correct header file with for the sqrt function.
10. Print a table with a column for the values 1.0, 2.0, etc. and a column for the related root values. For printing, select a suitable field width and 5 numbers behind the decimal point.
11. At the top of the table, put suitable informative text above each column.
15
Exercise 4d Pointers
Expand the code of Exercise 4a, b and c (use project Excercise4).
12. Create a string for 20 characters. Use a #define to indicate the size.
13. Use printf and scanf, and ask the user to enter a text of at least 10 and at most 19 characters without spaces and tabs.
14. Create a pointer variable that refers to the 3rd character in the string. See C book, Chapter 24.
15. Print this character with this pointer. Print the value of this pointer as well.
16. Increase the value of the pointer variable by 3. Print the character that this pointer refers to.
___________________________________________________________________
Exercise 5 Structs, unions and text files
Create a new C project using QtCreator: Exercise5a.
Exercise 5a Data in an array of structs
1. Create an array the size of three postcode_t types (Dutch postal code):
typedef struct {
int number;
char twoChars[3];
} postalcode_t;
Complete this array at the declaration with different postal code values and print the entire array with printf() in a for loop. Make sure that two structs in the array contain the same value. Use the next postal codes for initialising the array:
1234AB, 4567AB and 1234AB
2. Compare the content of two different structs in the array with an if statement and print if they are equal. Do this for the two similar structs and for two different structs in the array.
3. Print the size of the struct postalcode_t with sizeof() in bytes as well.
4. Write the content of the array of postalcode_t per element to a text file. Note: only the data content of the structs, not other related texts.
5. Read the content of the text file in a new array and print the read content.
16
Exercise 5b Union
Create a new C project using QtCreator: Exercise5b.
A union puts all data fields in the same memory location. This makes it possible for you to approach the same data in different forms (types) (reading and writing).
1. Create the following union and the necessary typedef and constant:
#define SIZE_BUFFER 16
typedef unsigned char byte_t;
union {
byte_t bData[SIZE_BUFFER];
int iData;
} bytes;
In the integer field, enter the value 1 and print the content of the bData array of bytes in hexadecimals (the number is equal to the size of an int in bytes, use the sizeof function).
2. Put a long type field in the union as well. Give this long the value 1 and print the bytes of this long by printing the correct quantity of bytes of the bData array.
3. Put a double type field in the union as well. Give this double the value 1.0 and print the bytes of this double by printing the correct quantity of bytes of the array's bData.
___________________________________________________________________
Exercise 6 Programming functions
Create a new C project using QtCreator: Exercise6a.
Being able to create functions yourself is a very important skill for a software developer. Create the following three functions and test the proper working of the functions by invoking them at different times with different input data in the main function.
Exercise 6a Conversion of Fahrenheit into Celsius and vice versa
1. Create the C functions for the conversion of Fahrenheit into Celsius and vice versa.
Put the next prototypes of these functions above main and the definitions under main:
17
double FtoC(double fahrenheit);
double CtoF(double celcius);
2. Use printf and scanf, invoke each function three times with different input values and print this input of the result of the functions with an informative text. For each input requested, print an informative text that makes clear what the user must enter.
Exercise 6b Comparing postal codes
Create a new C project using QtCreator: Exercise6b.
1. Create an array with four postal codes (see Exercise 5a). Create a function yourself that compares two postal codes. If the two postal codes are the same, a 1 (true) must be returned, or else a 0 (false). Compare them field by field. Two strings can be compared by the strcmp() function.
Put the prototype above the main function and put the implementation of the function under main. Prototype:
int comparePostalcodes(const postalcode_t *pPC1,
const postalcode_t *pPC2);
The input of this function is done with pointers. Why is const used? Put the answer in the comments in your code.
2. Fill the array with the declaration with postal codes with at least two being the same.
3. Test the function to be created several times with different input from the array and print the input and output.
___________________________________________________________________
Exercise 7 Modular programming
Create a new C project using QtCreator: Exercise7a. We will be using the code created in Exercise 6a. It is possible to keep several projects open in QtCreator so that it is easy to copy code from one project to another.
Exercise 7a "FCconversion" module
1. Using QtCreator, create the files FCconversion.h and FCconversion.c (menu bar File, New File or Project). Add these files to the project Exercise7a. Right click in the project explorer the project name, choose Add Existing Files … .
2. Put the prototypes of the functions for the conversion between Fahrenheit and
18
Celsius as created in Exercise 6a, in FCconversion.h.
3. Put the definitions of these conversion functions in FCconversion.c.
4. Include FCconversion.h in the main program.
5. Execute some calculations in main to demonstrate the use of the functions by calling these functions several times (as done in Exercise 6a).
Exercise 7b ‘Postalcodes’ module
Create a new C project using QtCreator: Exercise7b. We will be using the code created in Exercise 6b. It is possible to keep several projects open in QtCreator so that it is easy to copy code from one project to another.
Amend the program details in the "PostalCodes" module that is to be created like those specified in Exercise6b.
1. Create the module with the name Postalcodes: Postalcodes.h and Postalcodes.c and add them to the Qt project.
2. Add the comparePostalcodes function created in Exercise 6b there. Put the typedef for postalcode_t in Postalcodes.h above the function prototypes, because the function prototypes must be able to use postalcode_t in the parameter list.
3. Create the following function that in an array with postal codes looks for a postal code with certain upper case letters and returns the first occurrence as a pointer value. If not found, the value NULL is returned.
postalcode_t *searchPostalcodeUpperCaseChars(
const postcode_t pcs[],
int size,
const char upperCaseChars[]);
Invocation example:
postalcode_t data[N_PCS] = { ……… };
postalcode_t *pPostalCode =
searchPostalcodeUpperCaseChars(data, N_PCS, “AB”);
if (pPostalCode != NULL) {
………
4. Include Postalcodes.h in the main program.
5. In main, create an array of 5 postal codes, completing them with data so that we can test the functions. See the previous code fragment as well. Search for different upper case letters and print whether they were found and, if they were found, the related entire postal code.
6. In the Postalcode module, enter a function that writes the entire content of an
19
array with postal codes to a text file. The name of the text file must be able to be entered through the parameter list. The function returns a 0 if it was successful and a -1 if writing to the file was not successful.
int writePostalCodes(const postalcode_t pcs[],
int size,
const char fileName[]);
Write format: every postal code on a new line, separate the number and the string by a space. This will simplify the reading of this data file.
Example: 1222NG becomes 1222 NG '\n'
7. In the postcode module, enter a function that reads a text file and with the read content fills an array with postal codes. The function returns a 0 if it was successful, a -1 if reading the file was not successful, and a -2 if the file contains more than the array's size.
int readPostalCodes(postalcode_t pcs[],
int size,
const char fileName[]);
If there are more postal codes in the file than the array can hold, reading must be stopped. If there are less in the file than the size of the array, then the remaining array must be supplemented with this value: 0000##. Demonstrate this in an example in main.
___________________________________________________________________
Oct 01, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here