Skip to content
School of Physics
Home Our Teaching Resources C programming ex5.html
Back to top

Exercises 5

These exercises form part of your assessment.

Hint: Test your input

Since input using scanf() is new to you, I strongly recommend that when you read in a number you immediately print it out to the screen as a test that you have read it in properly. (To be honest, I would recommend this anyway, it's a very quick test that can save a lot of time and trouble.) Ideally checks like this should be printed to stderr, not stdout as they are not part of the output that the program is designed to create.

File test

This is unassessed and need not be handed in.

To test that you can write and read a file, write a short program to write a double to a file, close the file, open the file again for reading and read in the number. Remember to use "%lf" for reading a double. Of course, you should check that you read in the number correctly.

Look to see where the program created the output file, it should be inside a folder in your project folder.

Exercise: angles and sides of a triangle.

  1. Write a function that prints out a numbered menu of items (1, 2 etc.) and accepts a number typed by the user. Check it is in the correct range, and if so return that value to the calling function. If the number is out of range, handle the situation appropriately.
  2. Use a variation of the above "menu" function to allow the user to specifiy a triangle in one of three ways:
    1. The lengths of all three sides.
    2. The lengths of two sides and the angle between them.
    3. The length of one side and the angle each of the other two sides makes with it.

    For each case, read in the appropriate three numbers and calculate and print out all three angles and lengths of the sides of the triangle. I suggest writing a separate function for each case.

  3. Use the program to check itself: once you know all the angles and sides of a triangle try entering different combinations of these into the program and see if it gives out the same values.
Hand in the program and the results of your checks. We shall ask for your work to be handed in soon after the start of next week's practical session.
                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Share
Back to top