Exercise 1.1 Hello world The goal of this exercise is to verify that computer and compiler setup are functioning correctly. To verify that your setup runs fine, compile and run the hello world example #include <iostream> using namespace std ; int main() { cout << Hello World << endl ; return 0 ;
Exercise 1.2 Counting chars in a string The goal of this exercise is to learn basic pointer skills. We will write a C++ program that analyzes the character content of a string that is given to you by the user of your program. The program should display the number of uppercase and lowercase characters, the number of digits and the number of other characters. Approach a) Step 1: write a small program that reads in a string from the terminal up to a newline character and prints that same string again to the terminal. You can use the code fragment below include <iostream> int main() { // Allocate buffer char buf[20] ; // Read in string from terminal std::cout << "Enter String: " ; std::cin.getline(buf,20) ; // Write string back to terminal std::cout << buf << std::endl ; return 0 ;
Exercise 1.2 Counting chars in a string b) Step 2: Add code that loops over the string and looks at the string one character at a time through a pointer. You are not allowed to use an integer index variable in the loop, you must use a pointer as loop variable Think about the following: How do you create a pointer and how do you make it point to the first character of the string? How do you make the pointer point to the next character in the string? How do you know that you re at the end of your string? c) Step 3: Finally add code that determines if each given character is uppercase, lowercase, a digit or otherwise. Think about how character literals work. First write a line of code that determines if a given character is the uppercase A. Now modify this code to determine if the character is any upper case letter A-Z. Before you start this find out what the ASCII table looks like (e.g. Google ASCII table) and exploit the ordering of the ASCII characters. Add similar code for lowercase characters, digits and other characters and make your program print how many of each are in the string.
The goal of this exercise is to learn how to use pointers and references with functions. The task you will need to perform is to implement an algorithm that sorts the contents of an array of 10 integers or strings in increasing order Part 1 Integer sorting a) Step 1 - Write a small main program that allocates an array of 10 integers and fill them with random values. #include <iostream> using namespace std ; int main() { // Array in random order int arr[10] = { 9, 8, 5, 2, 4, 3, 6, 1, 7, 0 ; return 0 ;
b) Step 2 - Add code to the program that prints the content of the array arr to the screen. We will need this later in the exercise to verify that the sorting algorithm is working. c) Step 3 Now we need to introduce a sorting algorithm to order the array. The goal of this exercise is to learn how to program an existing algorithm called bubble sort. This is not the fastest algorithm that exists, but it is very simple to code: Here is how it works: given an array A of length n, you can sort its contents by executing the following double loop: for (i=0; i<n-1; i++) { for (j=0; j<n-1-i; j++) { // if A[j+1]>A[j] swap A[j] and A[j+1] This loop considers every possible pair of elements A[i] and A[j] in the array and swaps them if they are in the incorrect order. Note that the actual code for swapping is not in the example above, it is a C++ comment that explains what should be done here (the actual code will depend on the data type in array A) d) Introduce in your program a function sort(int* A, int lena) that implements the code above. Be sure to add both the forward declaration and the function in your program. Run the program and confirm that nothing happens yet, because the sorting algorithm is not yet complete: the code that compares and swaps the elements is still missing.
e) Step 4 Now we will provide the missing piece of the sort algorithm: the compare and swap. Instead of coding it in the swap function we will delegate both steps to a new function called order(). Add a declaration and an implementation of an order() function that takes references to two ints as input arguments. In the implementation of the function add code that compares the values of the 1 st and 2 nd argument and that swaps their values if the 2 nd argument is greater than the 1 st argument. Note that the order() routine you re asked to develop here has a different functionality than the swap() routine that appeared in this mornings course material: swap() always swaps the elements, the order() function only does that if the order is incorrect Run the program and verify that the code works correctly.
Part 2 String sorting a) Make a copy of the main program and change it so that it allocates an array of 10 const char* pointers and initialize them with 10 string literals. For example const char* arr[10] = { "C++", "Fortran", "Pascal","Algol","Visual Basic","Python", "Java","Eiffel","Assembly", C" ; b) Adjust the arguments of the sort() and order() functions to accept the new type of array. What type of arguments should the order() now take? The easiest way to figure it out is to think of const char* as the new data type that replaces the data type int, and exactly replace int with const char*. Compile and run your program. Verify if your program works correctly by running it.
Part 2 String sorting c) Your program compiles but does not function correctly because the operation a<b for const char* is legal C++ but does not do what you think it should do: it compares the memory addresses of the pointers a and b and thus sorts them according by position in memory. To perform a lexographical sort (that means we sort according to alphabetical order of the string content), we can use a C++ helper function that will tell us of the content of string a is alphabetically lower or higher than that of string b To use this function we need to adjust the order() function. First include the header file <string.h> in your program, as this contains the declaration of the function we need. Next use the function int strcmp(const char* a,const char* b) to construct a new condition statement that replaces the if (a>b) statement to determine if the first or second argument of the order() function should come first. The strcmp() function is designed such that if the content of a is alphabetically before the content of b the return value is >0. If a is alphabetically behind b then the return value is <0. Run the program and observe that it now work correctly.