Vous êtes sur la page 1sur 12

INTERNATIONAL UNIVERSITY SCHOOL OF ELECTRICAL ENGINEERING (EE)

PROGRAMMING FOR ENGINEERS LAB

INTERNATIONAL UNIVERSITY SCHOOL OF ELECTRICAL ENGINEERING (EE)

LAB 1: INTRODUCTION TO C PROGRAMMING VARIABLES, DATA TYPES, AND ARITHMETIC EXPRESSIONS


1. Write a program that asks the user to enter two numbers, obtains the two numbers from the user and prints the sum, product, difference, and quotient of the two numbers. 2. Write a program that asks the user to enter two integers, obtains the numbers from the user, then prints the larger number followed by the words "is larger." If the numbers are equal, print the message "These numbers are equal." 3. Write a program that reads in the radius of a circle as an integer and prints the circle's diameter, circumference and area. Use the constant value 3.14159 for p. Do all calculations in output statements. [Note: In this chapter, we have discussed only integer constants and variables 4. Write a program that reads an integer and determines and prints whether it is odd or even. 5. Write a program that inputs a five-digit integer, separates the integer into its individual digits and prints the digits separated from one another by three spaces each. [Hint: Use the integer division and modulus operators.] For example, if the user types in 42339, the program should print: 4 2 3 3 9 6. Write a program to solve a set of equations:

y a1 x b1 y a2 x b2

- Input: a1, b1, a2, b2 - Output: root (X,Y), if any.

7. Write a program to solve a 2nd-order equation: ax2 bx c 0 - Input: a,b,c - Output: roots (if any) for all cases.

Programming for Engineers Lab.docx

INTERNATIONAL UNIVERSITY SCHOOL OF ELECTRICAL ENGINEERING (EE)

LAB 2: PROGRAM LOOPING


1. Calculate the value of p from the infinite series

Print a table that shows the approximate value of p after each of the first 1,000 terms of this series. 2. Write a program that prints the following diamond shape. You may use output statements that print either a single asterisk (*) or a single blank. Maximize your use of repetition (with nested for statements) and minimize the number of output statements. * *** ***** ******* ********* ******* ***** *** *

3. The total resistance of n resistors in parallel is:

Suppose we have a network of two resistors with the values 400 W and 200 W. Then our equation would be:

So the total resistance of our two-resistor network is 133.3 W. Write a program to compute the total resistance for any number of parallel resistors. 4. Write a program that converts numbers to words. Example: 895 results in ''eight nine five." 5. ("The Twelve Days of Christmas" Song) Write a program that uses repetition and switch statements to print the song "The Twelve Days of Christmas." One switch statement should be used to print the day (i.e., "First," "Second," etc.). A separate switch statement should be used to print the remainder of each verse. Programming for Engineers Lab.docx

INTERNATIONAL UNIVERSITY SCHOOL OF ELECTRICAL ENGINEERING (EE)

Programming for Engineers Lab.docx

INTERNATIONAL UNIVERSITY SCHOOL OF ELECTRICAL ENGINEERING (EE) The Twelve days of Christmas On the first day of Christmas my true love sent to me: A Partridge in a Pear Tree On the second day of Christmas my true love sent to me: Two Turtle Doves and a Partridge in a Pear Tree On the third day of Christmas my true love sent to me: Three French Hens Two Turtle Doves and a Partridge in a Pear Tree On the fourth day of Christmas my true love sent to me: Four Calling Birds Three French Hens Two Turtle Doves and a Partridge in a Pear Tree On the fifth day of Christmas my true love sent to me: Five Golden Rings Four Calling Birds Three French Hens Two Turtle Doves and a Partridge in a Pear Tree On the sixth day of Christmas my true love sent to me: Six Geese a Laying Five Golden Rings Four Calling Birds Three French Hens Two Turtle Doves and a Partridge in a Pear Tree On the seventh day of Christmas my true love sent to me: Seven Swans a Swimming Six Geese a Laying Five Golden Rings Four Calling Birds Three French Hens Two Turtle Doves and a Partridge in a Pear Tree On the eighth day of Christmas my true love sent to me: Eight Maids a Milking Seven Swans a Swimming Six Geese a Laying Five Golden Rings Four Calling Birds Three French Hens Two Turtle Doves and a Partridge in a Pear Tree On the ninth day of Christmas my true love sent to me: Nine Ladies Dancing Eight Maids a Milking Seven Swans a Swimming Six Geese a Laying Five Golden Rings Four Calling Birds Three French Hens Two Turtle Doves and a Partridge in a Pear Tree On the tenth day of Christmas my true love sent to me: Ten Lords a Leaping Nine Ladies Dancing Eight Maids a Milking Seven Swans a Swimming Six Geese a Laying Five Golden Rings Four Calling Birds Three French Hens Two Turtle Doves and a Partridge in a Pear Tree On the eleventh day of Christmas my true love sent to me: Eleven Pipers Piping Ten Lords a Leaping Nine Ladies Dancing Eight Maids a Milking Seven Swans a Swimming Six Geese a Laying Five Golden Rings Four Calling Birds Three French Hens Two Turtle Doves and a Partridge in a Pear Tree On the twelfth day of Christmas my true love sent to me: 12 Drummers Drumming Eleven Pipers Piping Ten Lords a Leaping Nine Ladies Dancing Eight Maids a Milking Seven Swans a Swimming Six Geese a Laying Five Golden Rings Four Calling Birds Three French Hens Two Turtle Doves and a Partridge in a Pear Tree

INTERNATIONAL UNIVERSITY SCHOOL OF ELECTRICAL ENGINEERING (EE)

LAB 3: MAKING DECISIONS


8. Write a program that asks the user to enter two integers, obtains the numbers from the user, and then prints the larger number followed by the words "is larger." If the numbers are equal, print the message "These numbers are equal." 9. Using the conditional operator, write a program that asks the user to enter three integers, and then display the median value among those numbers. 10. Write a program that asks the user to type in an integer n and n interger values at the terminal, and then display the minimum value among those numbers. 11. Write a program to solve a 2nd-order equation: aX2+bX+c=0 - Input: a, b, c - Output: roots (if any) for all cases. 12. Using the switch construct, write a program that converts numbers to words. Example: 895 results in ''eight nine five." Remember to display zero if the user types in just a 0. 13. Write a program that acts as a simple printing calculator. The program should allow the user to type in expressions of the form:
number operator

The following operators should be recognized by the program:


+ * / S E

The S operator tells the program to set the accumulatorto the typed-in number. The E operator tells the program that execution is to end. The arithmetic operations are performed on the contents of the accumulator with the number that was keyed in acting as the second operand.The following is a sample run showing how the program should operate:
Begin Calculations 10 S Set Accumulator to 10 = 10.000000 Contents of Accumulator 2 / Divide by 2 = 5.000000 Contents of Accumulator 55 Subtract 55 -50.000000 100.25 S Set Accumulator to 100.25 = 100.250000 4 * Multiply by 4 = 401.000000 0 E End of program = 401.000000 End of Calculations.

INTERNATIONAL UNIVERSITY SCHOOL OF ELECTRICAL ENGINEERING (EE)

Make certain that the program detects division by zero and also checks for unknown operators.

INTERNATIONAL UNIVERSITY SCHOOL OF ELECTRICAL ENGINEERING (EE)

LAB 4: C ARRAYS
1. Write a program that calculates the average of an array of 10 floating-point values. 2. What output do you expect from the following program? #include <stdio.h> int main (void) { int numbers[10] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int i, j; for ( j = 0; j < 10; ++j ) for ( i = 0; i < j; ++i ) numbers[j] += numbers[i]; for ( j = 0; j < 10; ++j ) printf ("%i ", numbers[j]); printf ("\n"); return 0; } 3. You dont need to use an array to generate Fibonacci numbers. You can simply use three variables: two to store the previous two Fibonacci numbers and one to store the current one. Rewrite program 7.3 so that arrays are not used. Because youre no longer using an array, you need to display each Fibonacci number as you generate it. 4. Using array, write a program that converts numbers to words. Example: 895 results in ''eight nine five." 5. Prime numbers can also be generated by an algorithm known as the Sieve of Erastosthenes. The algorithm for this procedure is presented here. Write a program that implements this algorithm. Have the program find all prime numbers up to n = 150.

INTERNATIONAL UNIVERSITY SCHOOL OF ELECTRICAL ENGINEERING (EE)

LAB 5: C FUNCTIONS
Write functions to solve the following exercises. 14. Prints the following diamond shape (input: the size of diamond). * *** ***** ******* ********* ******* ***** *** * 15. Solve a set of equations: Y=a1x+b1 Y=a2x+b2 - Input: a1, b1, a2, b2 - Output: root (x,y), if any. 16. Write a program to solve a 2nd-order equation: ax2 + bx + c=0 - Input: a,b,c - Output: roots (if any) for all cases. 17. Convert numbers to words. Example: 895 results in ''eight nine five." 18. Convert a positive integer to another base. 19. Write a function that raises an integer to a positive integer power. Have the function return a long int, which represents the results of calculating xn. 20. Write a function prime that returns 1 if its argument is a prime number and returns 0 otherwise. 21. Write a function that prints all prime numbers n 22. Write a function to sort an array of integers into descending order. 23. Write a function called arraySum that takes two arguments: an integer array and the number of elements in the array. Have the function return as its result the sum of the elements in the array.

INTERNATIONAL UNIVERSITY SCHOOL OF ELECTRICAL ENGINEERING (EE)

LAB 6: STRUCTURES AND CHARACTER STRINGS


1. Write a function elapsed_time that takes as its arguments two time structures and returns a time structure that represents the elapsed time (in hours, minutes, and seconds) between the two times. So the call elapsed_time (time1, time2) where time1 represents 3:45:15 and time2 represents 9:44:03, should return a time structure that represents 5 hours, 58 minutes, and 48 seconds. Be careful with times that cross midnight. 2. Write a function called findString to determine if one character string exists inside another string. The first argument to the function should be the character string that is to be searched and the second argument is the string you are interested in finding. If the function finds the specified string, have it return the location in the source string where the string was found. If the function does not find the string, have it return 1. So, for example, the call index = findString ("a chatterbox", "hat"); searches the string "a chatterbox" for the string "hat". Because "hat" does exist inside the source string, the function returns 3 to indicate the starting position inside the source string where "hat" was found. 3. Write a function called removeString to remove a specified number of characters from a character string. The function should take three arguments: the source string, the starting index number in the source string, and the number of characters to remove. So, if the character array text contains the string "the wrong son", the call removeString (text, 4, 6); 4. Write a function called clockKeeper that takes as its argument a dateAndTime structure as defined in chapter 9 of the textbook. The function should call the timeUpdate function, and if the time reaches midnight, the function should call the dateUpdate function to switch over to the next day. Have the function return the updated dateAndTime structure. (In your program, make sure to

INTERNATIONAL UNIVERSITY SCHOOL OF ELECTRICAL ENGINEERING (EE)

include the timeUpdate and dateUpdate functions which are already written in the textbook!) 5. We can calculate the number of elapsed days between two dates by computing the value of N for each of the two dates and then taking the difference, where N is calculated as follows:

Write a program that permits the user to type in two dates and then calculates the number of elapsed days between the two dates. Try to structure the program logically into separate functions. For example, you should have a function that accepts as an argument a date structure and returns the value of N computed as shown previously. This function can then be called twice, once for each date, and the difference taken to determine the number of elapsed days.

INTERNATIONAL UNIVERSITY SCHOOL OF ELECTRICAL ENGINEERING (EE)

LAB 7: POINTERS
Write a function called insertEntry to insert a new entry into a linked list. Have the procedure take as arguments a pointer to the list entry to be inserted (of type struct entry as defined in chapter 12 of the textbook), and a pointer to an element in the list after which the new entry is to be inserted. 6. The function developed in exercise 1 only inserts an element after an existing element in the list, thereby preventing you from inserting a new entry at the front of the list. How can you use this same function and yet overcome this problem? (Hint: Think about setting up a special structure to point to the beginning of the list.) 7. Write a function called removeEntry to remove an entry from a linked list. The sole argument to the procedure should be a pointer to the list. Have the function remove the entry after the one pointed to by the argument. (Why cant you remove the entry pointed to by the argument?) You need to use the special structure you set up in exercise 2 to handle the special case of removing the first element from the list. 8. Write a function called sort3 to sort three integers into ascending order. (This function is NOT to be implemented with arrays.) 9. Given the definition of a date structure as defined in chapter 12 of the textbook, write a function called dateUpdate that takes a pointer to a date structure as its argument and that updates the structure to the following day (see Program 9.4).

Vous aimerez peut-être aussi