C++ Programing for Financial Engineering Online Certificate. Join hundreds of graduates from over 35 countries on 5 continents.

Advanced C++11/C++14 and Multidisciplinary Applications Online Certificate. Launched in January 2017.

  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

LEVEL 2 HW, lecture video/notes, sample code

Discussion in 'Level 2' started by Daniel Duffy, 9/24/11.

Thread Status:
Not open for further replies.
  1. Daniel Duffy

    Daniel Duffy C++ author, trainer
      C++ Level 1

    PLEASE DOWNLOAD Level 2.zip file below which contains this HW (in PDF), reading materials (in PDF) and code.

    Lecture videos are HERE

    All exercises in this level must be coded exclusively in C syntax (no <iostream>, cout, cin, classes, etc.)

    1.6 - The Preprocessor
    • Exercise 1
    Write a C-program that contains two print macro calls. The first prints the variable a, the second prints the variables a and b. Printing happens by the use of the PRINT1 and PRINT2 macros that accept arguments. These macros must be defined in an include-file. The variables a and b gets their value in the function main().

    Name the program “Macro.c” and the include-file “Defs.h”. Don’t forget to implement the mechanism to avoid multiple inclusion of the header file.
    • Exercise 2
    Create the two macros MAX2(x,y) and MAX3(x,y,z). These macros must return the maximum value of the given arguments. Let the macro MAX3 make use of the macro MAX2. Add these macros to the file “Defs.h”.

    Attached Files:

  2. Daniel Duffy

    Daniel Duffy C++ author, trainer
      C++ Level 1

    1.7 - Pointers and Arrays
    • Exercise 1
    Try to create a function Swap(). This function must exchange the value of two variables. For example: if i=123 and j=456, then i=456 and j=123 after the Swap() function has been called. The variables i and j are declared, initialised and printed in the function main(). This problem can be solved by using pointers as arguments for the Swap()function.
    • Exercise 2
    The following program reads a string with a 30 character maximum. Implement the Length() function. The function Length() must determine the length of the string. Give Length()the address of the array as argument.
    Note: your Length() function should be similar to the built-in strlen() function so your job is to mimic that function without using it.

    EOF is used in the function main(). This means End-of-File and is discussed later on in this document.
    In DOS, EOF can be entered by the key combination Ctrl-z (often written as ^Z). With ^Z (Say: control Z) is meant pressing the control-key and the z-key simultaneously.
    Code (C++):
    1. /* Calculate the length of a string */
    3. #include <stdio.h>
    5. #define MAXLINE 30
    7. // String length declaration
    8. int Length(char str[]);
    10. int main()
    11. {
    12. char string[MAXLINE+1]; // Line of maximum 30 chars + \0
    13. int c; // The input character
    14. int i=0; // The counter
    16. // Print intro text
    17. printf("Type up to %d chars. Exit with ^Z\n", MAXLINE);
    19. // Get the characters
    20. while ((c=getchar())!=EOF && i<MAXLINE)
    21. {
    22. // Append entered character to string
    23. string[i++]=(char)c;
    24. }
    25. string[i]='\0'; // String must be closed with \0
    27. printf("String length is %d\n", Length(string));
    29. return 0;
    30. }
    32. /* Implement the Length() function here */
    • Exercise 3
    Code (C++):
    1. /* Predict what will be printed on the screen */
    3. #include <stdio.h>
    5. #define PRD(a) printf("%d", (a) ) // Print decimal
    6. #define NL printf("\n"); // Print new line
    8. // Create and initialize array
    9. int a[]={0, 1, 2, 3, 4};
    11. int main()
    12. {
    13. int i;
    14. int* p;
    16. for (i=0; i<=4; i++) PRD(a[i]); // 1
    17. NL;
    19. for (p=&a[0]; p<=&a[4]; p++) PRD(*p); // 2
    20. NL;
    21. NL;
    23. for (p=&a[0], i=0; i<=4; i++) PRD(p[i]); // 3
    24. NL;
    26. for (p=a, i=0; p+i<=a+4; p++, i++) PRD(*(p+i)); // 4
    27. NL;
    28. NL;
    30. for (p=a+4; p>=a; p--) PRD(*p); // 5
    31. NL;
    33. for (p=a+4, i=0; i<=4; i++) PRD(p[-i]); // 6
    34. NL;
    36. for (p=a+4; p>=a; p--) PRD(a[p-a]); // 7
    37. NL;
    39. return 0;
    40. }
    • Exercise 4
    Create a C-program that has a function DayName()which can print the day of a given day-number. For example:

    1 gives: Day 1 is a Sunday
    7 gives: Day 7 is a Saturday.

    The day-name (1-7) should be written "hard-coded” into the program using an array of strings.
  3. Daniel Duffy

    Daniel Duffy C++ author, trainer
      C++ Level 1

    1.8 - Structures
    • Exercise 1
    Write a C-program that prints the contents of a struct called Article. An Article has the following characteristics:​
    • Article number
    • Quantity
    • Description (20 characters)
    The test program must create an Article of which the contents are assigned at initialization level.​

    Printing the Article is done with a Print() function. This function gets the address of the structure as a parameter.​

    Tip: Suppose that p is the pointer to the structure. It will allow the fields to be printed by (*p).fieldname or p->fieldname.​
  4. Daniel Duffy

    Daniel Duffy C++ author, trainer
      C++ Level 1

    1.9 - Input and Output
    • Exercise 1
    Create a C-program that reads the characters from the keyboard and shows them on screen (the inputted characters should only be displayed when the user hits 'enter', line by line).

    When ^A is entered, the program must end properly. Then the following message will appear: “CTRL + A is a correct ending.”

    Tip: getchar() reads and putchar() writes the type int. The value of ^A is 1.
    • Exercise 2
    Alter the last program of exercise 1 in such a way that the output doesn’t go to the screen but is written to a file. The file to write to must be specified by the user.
Thread Status:
Not open for further replies.