# quiz3_answers - finds the first negative number in array a...

This preview shows pages 1–2. Sign up to view the full content.

Programming Using C Quiz 3 Name: 1. [50 p ] Given the following code fragment: int i = 1, a[] = {0, 1, 2}, *b, *c; b = a; c = &i; decide whether the following C expressions are correct, and if they are, give their value. (a) [5 p ] a = b incorrect (b) [5 p ] a == b correct, 1 (c) [5 p ] b == a[0] incorrect (d) [5 p (e) [5 p ] b[0] == a[0] correct, 1 (f) [5 p ] c == b + 1 correct, 0 (g) [5 p ] *c == *(b + 1) correct, 1 (h) [5 p ] *(b + *c) == *(a + 1) correct, 1 (i) [5 p (j) [5 p 2. [50 p ] Write a program that ﬁnds the ﬁrst negative number in an array. The program should use the following functions: /* reads an array from keyboard and stores it in array a * returns the number of elements read; * this number is also read from the keyboard int read_array(int *a);

This preview has intentionally blurred sections. Sign up to view the full version.

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

Unformatted text preview: /* finds the first negative number in array a. * If such number exists in a, the function returns 1 * and stores its value at address negative. * If such number does not exist, returns 0. */ int find_negative(int *a, int n, int *negative); You don’t need to write the read array function; just write the ﬁnd negative and main functions. Answer: 1 int find_negative(int *a, int n, int *negative) { int found = 0, i; for (i = 0; i < n && !found ; i++) if (a[i] < 0) { found = 1; *negative = a[i]; } return found; } int main() { int a[N]; int n=read_array(a); int negative; int found = find_negative(a,n,&negative); if (found) printf("First negative: %d\n", negative); else printf("No negative.\n"); return 0; } 2...
View Full Document

## This note was uploaded on 01/14/2012 for the course CGS 3460 taught by Professor Staff during the Summer '08 term at University of Florida.

### Page1 / 2

quiz3_answers - finds the first negative number in array a...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online