You are on page 1of 8

# ECE 175: Computer Programming for Engineering Applications

Page 1 of 8

Midterm 2
Thursday, 20 November 2008 Duration: 75 minutes Maximum Points: 75

Name: ______________________________________________________________________________ 1. 2. 3. 4. 5. 6. Do not forget to write your name. The exam consists of 8 pages in total. Make sure all pages are printed. You may only use the 2-page help sheet! Otherwise, this is a CLOSED book exam. If you make any assumptions, state them clearly. The second page contains some example functions that we discussed in class. Good Luck! ---------------------------- Use space below for rough work. ---------------------------

## ECE 175: Computer Programming for Engineering Applications

Page 2 of 8

Topics
Basics Strings etc. Total

Score
/35 /55 /90

## Some functions available in ctype.h library

toupper(char) tolower(char) isalpha(char) isdigit(char)

## Some functions available in string.h library

strlen(char strcpy(char strcat(char strcmp(char a[]) a[],char b[]) a[],char b[]) a[],char b[]) returns the length of string a copies array b to array a appends array b to array a compares two strings a and b (returns 0 if equal, neg. number if a < b, and pos. number if a > b)

## Example functions on integer arrays discussed in class

int compare_array(int a[], int b[]) { int i = 0; while (a[i]!= 0 && b[i]!= 0) { if (a[i] != b[i]) break; i++; } return a[i] - b[i]; } // Assuming 0 will determine the end of array

int scan_array(int a[]) // Assuming 0 will determine the end of array { int i=0; do { scanf("%d", &a[i]); i++; }while (a[i] != 0); }

## Example usage of rand() function:

int x; x = rand();

## ECE 175: Computer Programming for Engineering Applications

Page 3 of 8

Basics. (35 points) 1. (4 points) Explain the output of the rst printf statement. Complete the remaining two printf statements
with two dierent answers so that their output is the same as the rst printf statement.
int array; printf("%u", array); Meaning: __________________________________________________________

## printf("%u", ______________); printf("%u", ______________);

2. (2 points) What character terminates a string? What is the character's ASCII value?

3. (3 points) Explain in no more than three sentences what the program segment below does. Don't just mention that it prints the word "Hello"; be more detailed on the working of the printf statement.
char x = "Hello"; printf("%s", x); Answer:

4. (3 points) The output of the following program is "thing". Fill in the missing part of the program using the variable declared.
char a[] = "something"; printf("%s\n", _____________________);

5. (3 points) Implement the function toupper(char ch) available in the ctype.h library. The function works as follows: If ch is a lower-case alphabet, the function returns the corresponding upper-case alphabet. Otherwise, the function simply returns the original character. [Note: The ASCII value of 'A' is 65 and 'Z' is 90. The ASCII value of 'a' is 97 and 'z' is 122.]
int toupper(char ch) {

## ECE 175: Computer Programming for Engineering Applications

Page 4 of 8

6. (4 points) Write a function isSeparator(char c) that identies if a given character c is one of the following characters: (1) space; (2) question mark; (3) exclamation; (4) double quote; (5) single quote; (6) comma; (7) colon; (8) semi-colon; (9) end-of-string character; or (10) a period. The function returns 1 if true, 0 otherwise. [To use single quote or double quote as a character, you will have to use it with a backslash.] You may not use any additional function calls to any library in writing this function. Keep it simple!
int { isSeparator(char c)

7. (6 points) Write a function reverseWord(char x[]) that reverses the word contained in string x. You may not use any additional variables than what is already provided. Assume that x contains only one word.
reverseWord(char x[]) { int i, n;

## ECE 175: Computer Programming for Engineering Applications

Page 5 of 8

8. (5 points) Write a function isPalindrome(char x[]) that identies if the string x is a palindrome or not. Assume that x will consist of only a simple english word. The function returns 1 if true; 0 otherwise.
int { isPalindrome(char x[])

9. (5 points) The C standard library (stdlib.h) provides a function called rand() that returns a random integer number. The usage is shown in Page 2. We are required obtain K random digits, and store it in an array from a through a[K-1]. It is also required that each digit may contain only values from 1 through B. Complete the function below to achieve this goal. [Hint: If you have an integer x, you can take the remainder of x with B to get a number from 0 to B-1. You can then add 1 to this number to get a number from 1 to B.]
int { get_random_digits(int a[], int K, int B)

Page 6 of 8

## Strings. (55 points)

(15 points) Write a function strcmp_nocase that takes two strings as arguments and returns: (1) a negative value if the rst string is less than the second string; (2) a positive value if the rst string is greater than the second string; and (3) 0 if the two strings are equal. The comparison performed by the function is not case sensitive. For example: If s1 = "aPPle" and s2="AppLe", strcmp_nocase function would return 0, implying the strings are equal. The strcmp() function available in the C library will return a non-zero value as it is case-sensitive. You may not declare any additional variables. [Hint: See example functions in Page 2.]
int { strcmp_nocase(char s1[], char s2[])

int i;

## ECE 175: Computer Programming for Engineering Applications

Page 7 of 8

(20 points) The main problem with using gets() function in C is that it does not check for array boundary. We want to implement our own function to get a sentence from the user that will have a maximum limit on the length of the sentence. In addition to passing the string as a parameter, we would also like to pass an integer that limits the maximum number of characters that can be stored in the array (say max_limit). Complete the function below to achieve the above goal. The function must accept a complete sentence from the user if the sentence has less than max_limit characters (including the end-of-string character). Otherwise, the string c will contain the rst max_limit -1 characters entered by the user with the last character c[max_limit - 1] set to the end-of-string character. First, write in English what your approach to the problem is, and then write the C code. [Hint: See example functions in page 2.] In English:

You may not use any additional variables than what is provided.
get_Sentence_With_Max_Limit(char c[], int max_limit)

int i;

## ECE 175: Computer Programming for Engineering Applications

Page 8 of 8

(20 points) Write a function that takes a sentence as input (in a string) and returns the number of words in the sentence. The sentence may contain words and punctuation characters. You may restrict the punctuation characters to the following: (1) space; (2) question mark; (3) exclamation; (4) double quote; (5) single quote; (6) comma; (7) colon; (8) semi-colon; (9) end-of-string character; or (10) a period. You may assume that the function isSeparator() on Page 4 was implemented correct and may use it here [This will signicantly reduce your writing]. Input: Quote of the day: "Be the change you wish to see in the world!" Return value: 14 Note: The sentence may have multiple spaces or multiple separators between two words. For example the words "day" and "Be" are separated by a colon, space, and a double-quote. Therefore, simply counting the number of separator characters would not indicate the number of words in the sentence. You may assume that the rst character in the string is ALWAYS an alphabet. Approach to the problem in English:

C Function:
int word_count(char c[], int max_limit) {