hw3-solutions

Hw3-solutions

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: le, Do- while Loops (20 pts) Compete the following function that counts the number of positive (greater than zero) integers in an array, using three types of loops. See the comments for more details. The three versions should behave exactly the same. // Count the number of positive integers in an array // X: the input array. // N: the number of elements in the array, N > 0 // return: the counted number. int count_positive(int X, int N) a. [10 pts] Complete the function using a for loop. int count = 0; for (int i = 0; i < N; i++) { if (X[i] > 0) count++; } return count; b. [5 pts] Complete the function using a while loop. int count = 0; int i = 0; while (i < N) { if (X[i] > 0) count++; i++; } return count; c. [5 pts] Complete the function using a do- while loop. int count = 0; Name: Lab Section: int i = 0; if (i < N) { do { if (X[i] > 0) count++; i++; } while (i < N); } Question 4: String Operations, Pointers, and C Library Functions (20 pts) You may only use pointers, not array, in the following exercises. a. [5] Complete the following function that returns the length of string s. int strlen(char *s) { int len =...
View Full Document

This note was uploaded on 02/24/2013 for the course CPR E 288 taught by Professor Staff during the Fall '08 term at Iowa State.

Ask a homework question - tutors are online