name:
int *ptr; ptr = (double *) malloc(sizeof(double));
#include <stdio.h> typedef struct { int first; int second; } pair; typedef int intlist[10]; void inc2(intlist il, pair p) { il[3]++; p.first++; } main () { intlist m; pair q; m[3] = 4; q.first = 5; inc2(m, q); printf("m[3]: %d and q.first: %d.\n", m[3], q.first); }
#include <stdio.h> main () { FILE *fptr; char filename[21]; printf("Enter a filename: "); scanf("%s", filename); fptr = fopen(filename, "r"); fprintf(fptr, "this is a sentence\n"); fclose(fptr); }
foo.c
in the current working
directory contains the program given in question 3.
How many lines of output does the following Unix command produce?
cat foo.c | grep first
#include <stdio.h> #include <stdlib.h> main() { int i; int *ptr; i = 10; ptr = &i; *ptr = 7; printf("i is: %d\n.", i); }
out.txt
after
the following command is executed. Assume that foo.c
is a text
file in the current working directory.
wc foo.c > out.txt
answertype
,
where 0 is used to represent false, and 1 is used to represent true.
#define NUMSTUDENTS 6 #define NUMQUES 5 typedef int answertype[NUMSTUDENTS][NUMQUES];
The answer key to this exam is stored in an array of type
keytype
.
typedef int keytype[NUMQUES];
key
of type keytype
initialized so that the first three answers are true (1) and the rest
are false (0).
key
, what error does
the following code fragment contain? (Hint: the answer is NOT that
the key for the exam is changed.)
int i, a; for (i = 0; i <= NUMQUES; i++) { printf("Enter the next answer: "); scanf("%d", &a); if (a == 0) key[i] = 0; else key[i] = 1; }
void gradeit(answertype, keytype);
i.e. it takes student answers and the key as arguments. Your function
should write exam scores into the output file scores.dat
, one
line per exam, in the following form:
Exam:
exam-number Score:
score
where each correct answer is worth five points. For example, if the
first exam has four answers correct, the first line of the file should be:
Exam: 0 Score: 20
You can assume that <stdio.h>
has been included, that file
scores.dat
will open correctly (i.e. you don't need to check for
errors when opening the file) and that your
function appears in the same file with the previous type and constant
declarations.
scores.dat
and
finds and prints the highest exam score. The user may have edited the
file by hand, so you can't assume that the file contains exactly six
lines. However, you can assume that all lines in the file have the format
given in the previous part of this problem, and you don't need to check for
impossible scores. You don't need to check for errors when opening the file,
and you can assume that <stdio.h>
has been included in the program
file.#include <stdio.h> int inc(int x) { x++; return (x); } main() { int y = 9; int res; res = inc(y); printf("The value and result are: %d %d.\n", y, res); }
FILE *fptr; fptr = stdout; fprintf(fptr, "I'm the output.\n");
#include <stdio.h> int x = 7; void printx(void) { printf("x is %d.\n", x); } main() { int x = 8; x++; printx(); }
#include <stdio.h> main() { char c = 'n'; switch (c) { case 'u': printf("u"); case 'n': printf("n"); case 'i': printf("i"); break; case 'x': printf("x"); break; default: printf("dos"); break; } }
#define NAMELEN 41 #define LINELEN 81 typedef char linetype[LINELEN]; typedef struct { linetype street; linetype cityAndState; } addresstype; typedef struct { char name[NAMELEN]; addresstype address; int age; } persontype;and the following declaration of
p
:
persontype p;
printf()
needed to print the
cityAndState
associated with p
.
scanf()
needed to read a new
age
for p
from the keyboard.
name
for p
from the keyboard. Make sure that a name
can contain
multiple words (i.e. can contain spaces and tabs).
p.name[10];
elvis
is logged into the Unix system kitchen.graceland.com
.