|
CS 2213/1, Spring 2005 Quiz 3 Questions |
Last Name: | First Name: |
Write lines of code to do the following, in sequence:
The following program will NOT swap (interchange) the values of a and b. Add and modify the code as necessary so that the program will do the swap.
#include <stdio.h>
void swap(int *i, int *j) {
int t = *i;
*i = *j;
*j = t;
}
int main() {
int a = 23, b = 47;
printf("Before. a: %d, b: %d\n", a, b);
swap ( &a, &b );
printf("After. a: %d, b: %d\n", a, b);
return 0;
}
Consider the following program:
/* 1*/ #include <stdio.h>
/* 2*/
/* 3*/ int main() {
/* 4*/ int a[] = {4, 9, 25, 49, 121, 169, 289, 0};
/* 5*/ int i = 0;
/* 6*/ int sum = 0;
/* 7*/ while (a[i] != 0)
/* 8*/ sum = sum + a[i++];
/* 9*/ printf("Sum: %i\n", sum);
/*10*/ }