Beruflich Dokumente
Kultur Dokumente
y a1 x b1 y a2 x b2
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.
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. * *** ***** ******* ********* ******* ***** *** *
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) 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
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.
Make certain that the program detects division by zero and also checks for unknown operators.
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.
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.
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.
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).