`

SSD06 Practical Quiz 2 个人解答

阅读更多

1.

Are there any memory errors in the following program? If so, identify all of the errors and provide a corrected code fragment to alleviate the problem. Assume that the user enters in correct input, and that the sizes entered are at least one.

Write your solution in a text or Word file and submit it below.

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 */
        }
}




















2.

Are there any memory errors in the following program? If so, identify all of the errors and provide a corrected code fragment to alleviate the problem.

Write your solution in a text or Word file and submit it below.

/* return 1 if str is "1", 0 otherwise */
int checkIf1(char *str) {
        char *newstr = malloc(strlen(str) + 1);
        strcat(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]));
        }
}









3.

Are there any memory errors in the following program? If so, identify all of the errors and provide a corrected code fragment to alleviate the problem.

Write your solution in a text or Word file and submit it below.

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[], int 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);
        }
}

 

分享到:
评论

相关推荐

    SSD06 Practical Quiz 1 个人解答

    我的解答,没做测试 博文链接:https://qianjigui.iteye.com/blog/269525

    ssd3 practical quiz 6

    ssd3 practical quiz 5ssd3 practical quiz 5ssd3 practical quiz 5ssd3 practical quiz 5ssd3 practical quiz 5ssd3 practical quiz 5ssd3 practical quiz 5

    ssd3 practical quiz 3

    ssd3 practical quiz 3ssd3 practical quiz 3ssd3 practical quiz 3ssd3 practical quiz 3ssd3 practical quiz 3ssd3 practical quiz 3ssd3 practical quiz 3

    ssd3 practical quiz 2

    ssd3 practical quiz 2ssd3 practical quiz 2ssd3 practical quiz 2ssd3 practical quiz 2ssd3 practical quiz 2ssd3 practical quiz 2ssd3 practical quiz 2ssd3 practical quiz 2

    ssd3 practical quiz 8

    ssd3 practical quiz 8ssd3 practical quiz 8ssd3 practical quiz 8ssd3 practical quiz 8ssd3 practical quiz 8ssd3 practical quiz 8

    ssd3 Practical Quiz 7 答案

    SSD3,全称为“Solid State Drives 3”,通常是指...以上就是关于SSD3的一些关键知识点,对于解答"Practical Quiz 7"会有很大帮助。了解并掌握这些内容,不仅能够应对考试,还能在实际工作中更好地理解和应用SSD3技术。

    ssd3 practical quiz2

    【标题】"ssd3 practical quiz2"是一个与软件工程相关的实践测验,可能是课程"Software System Development 3"(SSD3)的一部分。这个测验可能涉及了软件开发过程中的实际操作和问题解决,旨在检验学生在项目管理和...

    SSD04 Practical Quiz 4 个人解答

    【SSD04 实践测试4 个人解答】 在本次实践测试4中,我们主要探讨了与源码理解和工具使用相关的知识。通过个人的解答,我们可以深入理解这些关键概念,并提高在实际开发中的应用能力。以下是对每个部分的详细解析: ...

    ssd3 practical quiz 5

    ssd3 practical quiz 5ssd3 practical quiz 5ssd3 practical quiz 5ssd3 practical quiz 5ssd3 practical quiz 5ssd3 practical quiz 5

    SSD3 Practical Quiz8

    SSD3 Practical Quiz8 2009 StudentArrayList.java

    ssd3 practical quiz 1

    ssd3 practical quiz 1ssd3 practical quiz 1ssd3 practical quiz 1ssd3 practical quiz 1ssd3 practical quiz 1 ssd3 practical quiz 1

    SSD4 Practical Quiz 2 答案(仅供参考)

    SSD4 Practical Quiz 2 答案(仅供参考) 包括A Tabbed Panel Photo Album(President Clinton and Hillary Clinton )工程文件

    ssd3 practical quiz6

    "ssd3 practical quiz6"很可能是某个教育课程或培训项目的一部分,旨在测试或提升学员对SSD技术及其应用的理解。 在这个2007年的卡耐基软件工程网络教材中,"ssd3"可能指的是课程的序列号或者级别,暗示这已经是...

    SSD04 Practical Quiz 2 个人解答

    【标题】"SSD04 实践测试2 个人解答" 在本次实践测试中,我们主要探讨了与SSD(Solid State Drive)相关的技术问题,这包括但不限于存储原理、性能优化以及工具应用。SSD是一种非易失性存储设备,使用固态电子存储...

    ssd3 practical quiz 7

    这个"practical quiz 7"是该课程中的第七次实践测验,旨在测试学生对SSD3理论知识的理解以及在实际操作中的应用。由于没有提供具体的文件内容,我将根据SSD3可能涵盖的主题来生成相关的知识点。 1. **固态存储基础*...

    ssd3 practical quiz4

    【标题】"ssd3 practical quiz4" 是一个与软件工程相关的实践测验,很可能源自2007年卡耐基梅隆大学的课程。这个测验可能旨在检验学生对软件开发过程、软件设计原则、编程技能以及项目管理的理解。 【描述】提及的...

    ssd3 practical quiz8

    【标题】:“ssd3 practical quiz8”是一个关于软件工程的实践测验,可能是课程“Software System Development 3”的第八次实践考核。这个标题暗示我们将探讨与软件开发过程中的实践问题相关的知识,可能涵盖编程、...

Global site tag (gtag.js) - Google Analytics