CSc 352: Assignment-2: Writing Simple C Programs

Due Monday, June 17th
NOTE: I might add a few more depending on how much we cover today(06-10) in class.

Please use the command:
turnin cs352_hw2 quest1 quest2.c quest3.c

1- Assuming that values of i and j are not changed in the body of the loop, can the following pieces of code ever lead to an infinite loop? Explain. Put your explanations into a file called quest1.

a)

printf("Input two integers: ");

scanf("%d%d", &i, &j);

while (i * j < 0 && ++i != 7 && j++ != 9) {

}

b)

printf("Input two integers: ");

scanf("%d%d", &i, &j);

for ( ; i * j <= 0 ; ++i, j++) {

}


2- Write a program(name it quest2.c) that will check the proper pairing of braces and parantheses. Your program should get the inputfrom the standard input(use getchar() macro) and should output to the standard output(use putchr() macro). The program should read and print each character in the from the input. If the number of right parantheses(braces) exceeds the number of left parantheses(braces) at any point inside the input it should print the character pair !? at that point in the output. After all the input is processed and the number of left parantheses(braces) exceeds right parantheses(braces)

Sample Input==>

(This (is a{n ( example} to} show} what) the )input to your) program ((looks {like. Your }program should} check }the proper{ pairing} of braces and (parantheses, in other words (each left brace (should be paired with a right brace }and each left} {parantheses} should be) paired) with a right parantheses.

 

Output should be as==>

(This (is a{n ( example} to}!? show}!? what) the )input to your) program ((looks {like. Your }program should}!? Check }!?the proper{ pairing} of braces and (parantheses, in other words (each left brace (should be paired with a right brace }!?and each left}!? {parantheses} should be) paired) with a right parantheses.

ERROR: Extra braces: }}}}}}

ERROR: Missing parantheses: )))


3- Let num be a positive real number. and let the sequence of real numbers x_i be given by:

x_0 = 1,

x_i+1 = 1/2 * (x_i + a / x_i) , for i>=0

It is known that as i approaches infinity, x_i approaches to the squareroot of a.

Using the above, write a program(named quest3.c) that finds squareroot of a. Your program should approximate the desired result as close as it can. (i.e., the above recurrence should be computed until the point where x_i+1 doesn't change anymore.)