|
CS 2213/1, Spring 2005, Exam 1 Exam 1 Answers |
Last Name: | First Name: |
4.31 3.6 8.27 1.23 7.9
4.3, 3.6, 8.3, 1.2, 7.9
#include <stdio.h>
int main() {
double a[5];
int i;
/* Question 4 */
for (i = 0; i < 5; i++)
scanf("%lf", &a[i]); /* or &a[i] could be a+i (Mr. Lecollier) */
/* Question 5 */
for (i = 0; i < 5; i++) {
printf("%5.1f", a[i]);
if (i != 4) printf(","); /* or printf( i < 4 ? "," : "\n"); */
else printf("\n");
}
}
% cc -o p4_5 p4_5.c
% p4_5
4.31 3.6 8.27 1.23 7.9
4.3, 3.6, 8.3, 1.2, 7.9
#include <stdio.h>
#include <math.h>
double seed = 314159.0;
double rand(void);
int main() {
int i;
printf("%.15f\n", rand());
printf("%.15f\n", rand());
for (i = 0; i < 30; i++)
printf("%i ", (int)(rand()*6.0 + 1.0));
printf("\n");
}
double rand() {
double a = 16807.0,
m = 2147483647.0;
double q;
seed = a*seed;
q = floor(seed/m);
seed = seed - q*m;
return(seed/m);
}
% cc -o p7 p7.c -lm
% p7
0.458724340171891
0.779985268963494
2 1 4 6 3 3 1 2 4 2 2 1 3 1 4 3 3 1 6 5 2 6 6 3 4 4 6 3 3 3
| First version | Second version | Third version |
|---|---|---|
#include <stdio.h>
#include <ctype.h>
int main(void) {
char ch;
while ((ch = getchar()) != EOF)
putchar(tolower(ch));
} |
#include <stdio.h>
int main(void) {
char ch;
while ((ch = getchar()) != EOF) {
if (ch >= 'A' && ch <= 'Z')
ch = ch -'A' + 'a';
putchar(ch);
}
} |
#include <stdio.h> /* by J. Edwards */
#include <ctype.h>
int main(void) {
char buff[99];
char ch;
int i;
for (i = 0; (ch = getchar()) != EOF; i++)
if (i < 98) buff[i] = tolower(ch);
else break;
buff[i] = '\0';
printf("%s", buff);
} |
% cc -o p8 p8.c % p8 Joe is 4 years old on Feb 29! (Return, CTRL-D) joe is 4 years old on feb 29! | ||
| Program | First File: sqr_main.c |
Second File: sqr.c |
Header File: sqr.h | |
|---|---|---|---|---|
#include <stdio.h>
int sqr(int x) {
return x*x;
}
int main() {
int i, sum = 0;
int primes[] =
{2, 3, 5, 7,
11, 13, 17};
for (i = 0; i < 7; i++)
sum += sqr(primes[i]);
printf("sum: %i\n", sum);
}
|
#include <stdio.h>
#include "sqr.h"
int main() {
int i, sum = 0;
int primes[] =
{2, 3, 5, 7,
11, 13, 17};
for (i = 0; i < 7; i++)
sum += sqr(primes[i]);
printf("sum: %i\n", sum);
}
|
#include "sqr.h"
int sqr(int x) {
return x*x;
}
|
int sqr(int ); | |
| Output | Output | |||
sum: 666 | sum: 666 | |||
What corresponds to a public function (that is, public method)? A normal function definition (not declared "static").
What corresponds to a private function (that is, private method)? A function declared "static".
What corresponds to a private data member?
A regulare variable declaration (a public variable
is declared "extern").
(See the first table at
Object-oriented programming in C
Consider the following program:
#include <stdio.h>
int main() {
int x, y, z, i;
int *xp = &x; /* xp points to x */
int *q;
int a[] = {10, 20, 30, 40};
*xp = 47; /* same as: x = 47; */
y = *&x; /* *&x same as x, so same as: y = x; or y = 47;*/
q = &a[2]; /* q points to a[2] */
*q = 37; /* same as: a[2] = 37; */
*(a+3) = 16; /* same as: a[3] = 16; */
printf("%i\n", y);
for (i = 0; i < 4; i++) printf("%i ", *(a+i));
printf("\n");
}
% cc -o p11 p11.c % p11 47 10 20 37 16
xp ----> x == 47 q ----> a[2] == 37 a+3 ----> a[3] == 16
| First version | Second version | Third version |
|---|---|---|
#include <stdio.h>
int main() {
char s[] = "How long am I?";
char *p = s;
int i = 0;;
while (*p++) /* or (*p++ != '\0') */
i++;
printf("Length: %i\n", i);
} |
#include <stdio.h>
int main() {
char s[] = "How long am I?";
int i = 0;
while (*(s + i))
i++;
printf("Length: %i\n", i);
} |
#include <stdio.h>
int stringlen(char *);
int main() {
char s[] = "How long am I?";
printf("Length: %i\n", stringlen(s));
}
int stringlen(char *s) {
int i = 0;
while (*s++)
i++;
return i;
}
|
| Common Output | ||
% cc -o p14 p14.c % p14 Length: 14 | ||
| Methods that DO NOT WORK | |
|---|---|
| First FAILURE | Second FAILURE |
#include <stdio.h>
int main() {
char s[] = "How long am I?";
int i = 0;
while (*s++)
i++;
printf("Length: %i\n", i);
}
|
#include <stdio.h>
int main() {
char s[] = "How long am I?";
int i = 0;
while (*(s + i++)) /* or (*(s + i++) != '\0') */
;
printf("Length: %i\n", i);
}
|
% cc -o p14_3 p14_3.c "p14_3.c", line 5: operands must have scalar type: op "++" | % p14_2 Length: 15 |
Standard mistake: char *s; as answer. This does not give any storage for characters, but only creates storage for an uninitialized pointer.
See Strings
in buffers.
Note: It is very important to check that you do not go beyond
the bounds of the buffer. For simplicity, I left this off in
the code on the link above, and I didn't expect it on the exam,
but I should have emphaisized it more.
The third version of Problem 8 above shows this type of check.