📄 practical quiz 2.html
字号:
<html>
<head>
<link rel=stylesheet href="/css/cteform.css" type="text/css">
<title>Practical Quiz 2 &copy; 2002-2005 iCarnegie, Inc. - UI Version 3.15</title>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<script language="JavaScript1.2">
CTEUtils_ImgDirURL = "/img";
CTEUtils_CssDirURL = "/css";
CTEUtils_JsDirURL = "/js";
</script>
<script language="JavaScript1.2" src="/js/ctesortable.js"></script><script language="JavaScript">var filelisting_2609065_tbl_info = new Array();
filelisting_2609065_tbl_info[1] = new Array();
filelisting_2609065_tbl_info[1]["name"] = "Size";
filelisting_2609065_tbl_info[1]["type"] = "number";
filelisting_2609065_tbl_info[2] = new Array();
filelisting_2609065_tbl_info[2]["name"] = "Status";
filelisting_2609065_tbl_info[2]["type"] = "string";
filelisting_2609065_tbl_info[3] = new Array();
filelisting_2609065_tbl_info[3]["name"] = "File Type";
filelisting_2609065_tbl_info[3]["type"] = "string";
filelisting_2609065_tbl_info[4] = new Array();
filelisting_2609065_tbl_info[4]["name"] = "Download";
filelisting_2609065_tbl_info[4]["type"] = "string";
var filelisting_2609065_tbl = new Object;
filelisting_2609065_tbl.name = 'filelisting_2609065_tbl';
filelisting_2609065_tbl.info = filelisting_2609065_tbl_info;
var filelisting_2609071_tbl_info = new Array();
filelisting_2609071_tbl_info[1] = new Array();
filelisting_2609071_tbl_info[1]["name"] = "Size";
filelisting_2609071_tbl_info[1]["type"] = "number";
filelisting_2609071_tbl_info[2] = new Array();
filelisting_2609071_tbl_info[2]["name"] = "Status";
filelisting_2609071_tbl_info[2]["type"] = "string";
filelisting_2609071_tbl_info[3] = new Array();
filelisting_2609071_tbl_info[3]["name"] = "File Type";
filelisting_2609071_tbl_info[3]["type"] = "string";
filelisting_2609071_tbl_info[4] = new Array();
filelisting_2609071_tbl_info[4]["name"] = "Download";
filelisting_2609071_tbl_info[4]["type"] = "string";
var filelisting_2609071_tbl = new Object;
filelisting_2609071_tbl.name = 'filelisting_2609071_tbl';
filelisting_2609071_tbl.info = filelisting_2609071_tbl_info;
var filelisting_3201056_tbl_info = new Array();
filelisting_3201056_tbl_info[1] = new Array();
filelisting_3201056_tbl_info[1]["name"] = "Size";
filelisting_3201056_tbl_info[1]["type"] = "number";
filelisting_3201056_tbl_info[2] = new Array();
filelisting_3201056_tbl_info[2]["name"] = "Status";
filelisting_3201056_tbl_info[2]["type"] = "string";
filelisting_3201056_tbl_info[3] = new Array();
filelisting_3201056_tbl_info[3]["name"] = "File Type";
filelisting_3201056_tbl_info[3]["type"] = "string";
filelisting_3201056_tbl_info[4] = new Array();
filelisting_3201056_tbl_info[4]["name"] = "Download";
filelisting_3201056_tbl_info[4]["type"] = "string";
var filelisting_3201056_tbl = new Object;
filelisting_3201056_tbl.name = 'filelisting_3201056_tbl';
filelisting_3201056_tbl.info = filelisting_3201056_tbl_info;
var Assm = new Object;
Assm.name = 'Assm';
Assm.postRenderings = new Array();
</script></head><body onload="">
<div align="center"><font size="4">Practical Quiz 2</font><br>
<br>
</div>
<hr>
<td width=5><a name="top_2609065"></a></td>
<table><tr>
<td width=5><br></td> <td class=td0_highlight-label width=12 align=right valign=top nowrap >
<span class=highlight-label >1.</span></td>
<td width=5><br></td> <td valign=top class=td0_x align=left > <a href="#2609065">Go to bottom of question.</a> </td>
</tr></table>
<table><tr>
<td width=5><br></td> <td valign=top class=td0_x align=left > <p><b>Are there any memory errors in the following program? If so, list all of them. Assume that the user enters in correct input, and that the sizes entered are at least one.
</b></p>
<p>
Write your solution in a text or Word file and submit it below.
</p>
<pre>void main() {
char *str, *input;
int *ilist;
int i, size1, size2;
printf("Number of letters in word: ");
scanf("%d", &size1); /* user inputs an integer */
printf("Number of integers: ");
scanf("%d", &size2); /* user inputs an integer */
str = (char *) malloc(size1);
ilist = (int *) malloc(size2);
printf("Word: ");
scanf("%s", str); /* user inputs a string */
for(i = 0; i < size2; i++) {
printf("Number %d of %d: ", i + 1, size2);
scanf("%d", ilist + i); /* user inputs an integer */
}
}
</pre>
</td>
<td width=5><br></td></tr></table>
<a name="2609065"></a><table><tr>
<td width=5><br></td> <td valign=top class=td0_x align=left > <a href="#top_2609065">Go to top of question.</a> </td>
</tr></table>
<td width=5> </td>
<hr><td width=5><br></td><a name="top_2609071"></a><table><tr>
<td width=5><br></td> <td class=td0_highlight-label width=12 align=right valign=top nowrap >
<span class=highlight-label >2.</span></td>
<td width=5><br></td> <td valign=top class=td0_x align=left > <a href="#2609071">Go to bottom of question.</a> </td>
</tr></table>
<table><tr>
<td width=5><br></td> <td valign=top class=td0_x align=left > <p>
<b>
Are there any memory errors in the following program? If so, list all of them.
</b></p>
<p>
Write your solution in a text or Word file and submit it below.
</p>
<pre>
/* return 1 if str is "1", 0 otherwise */
int checkIf1(char *str) {
char *newstr = malloc(strlen(str) + 1);
strcpy(newstr, str); /* set newstr to str */
if (strcmp(newstr, "1") == 0) { /* newstr is "1" */
return 1;
}
free(newstr);
return 0;
}
void main() {
char *strArr[4] = {"1", "2", "3", "4"};
int i;
for(i = 0; i < 4; i++) {
printf("%d\n", checkIf1(strArr[i]));
}
}
</pre>
</td>
<td width=5><br></td></tr></table>
<a name="2609071"></a><table><tr>
<td width=5><br></td> <td valign=top class=td0_x align=left > <a href="#top_2609071">Go to top of question.</a> </td>
</tr></table>
<td width=5> </td>
<hr>
<td width=5> </td>
<a name="top_3201056"></a><table><tr>
<td width=5><br></td> <td class=td0_highlight-label width=12 align=right valign=top nowrap >
<span class=highlight-label >3.</span></td>
<td width=5><br></td> <td valign=top class=td0_x align=left > <a href="#3201056">Go to bottom of question.</a> </td>
</tr></table>
<table><tr>
<td width=5><br></td> <td valign=top class=td0_x align=left > <p><b>Are there any memory errors in the following program? If so, list all of them.</b>
</p>
<p>
Write your solution in a text or Word file and submit it below.
</p>
<pre>
struct data {
char *str1, *str2;
};
/* returns two strings concatenated if they are not the same, NULL otherwise */
char *mergeSingleIfDifferent(char *s1, char *s2) {
char *str = (char *) malloc(strlen(s1) + strlen(s2) + 1);
if (strcmp(s1, s2) == 0) { /* strings are equal */
str = NULL;
}
else {
strcpy(str, s1);
strcat(str, s2);
}
return str;
}
/* copies merged strings (or NULL) into array of strings passed in (results) */
void mergeArrayIfDifferent(char *results[], char *strA1[], char *strA2[], size) {
int i;
for(i = 0; i < size; i++) {
results[i] = mergeSingleIfDifferent(strA1[i], strA2[i]);
}
}
void printAndFree(int c, char *str) {
if (str != NULL) {
printf("%d: %s\n", c, str);
free(str);
}
}
void main() {
char *strArr1[8] = {"1", "2", "3", "4", "5", "6", "7", "8"};
char *strArr2[8] = {"a", "2", "c", "4", "e", "6", "g", "8"};
char *results[8];
int i;
mergeArrayIfDifferent(results, strArr1, strArr2, 8);
for(i = 0; i < 8; i++) {
printAndFree(i, results);
}
}
</pre>
</td>
<td width=5><br></td></tr></table>
<a name="3201056"></a><table><tr>
<td width=5><br></td> <td valign=top class=td0_x align=left > <a href="#top_3201056">Go to top of question.</a> </td>
</tr></table>
<td width=5><br></td>
<hr><table><tr>
<td width=5><br></td> <td valign=top class=td0_x align=left > <a href="#top_of_page">Go to top of assessment.</a> </td>
</tr></table>
<table><tr>
<td width=5><br></td> <td class=td0_copyright align=left valign=top >
<span class=copyright ><br>© Copyright 2002 iCarnegie, Inc. All rights reserved.</span></td>
</tr></table>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -