| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1170 人关注过本帖
标题:哪位高手可以解决以下4道C语言题目?
只看楼主 加入收藏
只为流浪
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-11-27
收藏
 问题点数:0 回复次数:10 
哪位高手可以解决以下4道C语言题目?
[bold][underline]Week 9 Exercises – Pointers, arrays, malloc and free.[/underline][/bold]
[underline]Objectives[/underline][font=TimesNewRoman]Design and write functions that use multidimensional arrays as function arguments. Understand memory allocation and de-allocation using malloc and free.[/font]
[underline]Tutorial Assessment[/underline]The students are required to do [bold][underline]ALL[/underline][/bold] exercises. [font=TimesNewRoman]All assessed exercises need to be submitted in the Assessment section of this module under ICE. For [bold]Exercises 2, 3 and 4 [/bold]you should submit the source code for each exercise separately (no design document is needed).[/font]
[underline]Deadline[/underline]The deadline for submitting the exercises is: [bold][italic]Thursday 22 November 2007 at 4pm.[/italic][/bold]
[font=Times New Roman] [/font]
[underline]Exercise 1[/underline]
[font=TimesNewRoman]Analyse and explain in detail the following program.[/font]

[align=left][font=Times New Roman] [/font]

[/align]

[align=left][bold][font=Times New Roman]/* dyn_arr.c -- dynamically allocated array */[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]#include <stdio.h>[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]#include <stdlib.h> // for malloc(), free()[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]int main(void)[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]{[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]double * ptd;[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]int max;[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]int number;[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]int i = 0;[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman] [/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]puts("What is the number of type double entries?");[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]scanf("%d", &max);[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]ptd = (double *) malloc(max * sizeof (double));[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]if (ptd == NULL)[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]{[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]puts("Memory allocation failed. Goodbye.");[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]exit(EXIT_FAILURE);[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]}[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]/* ptd now points to an array of max elements */[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]puts("Enter the values (q to quit):");[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]while (i < max && scanf("%lf", &ptd[i]) == 1)[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]++i;[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]printf("Here are your %d entries:\n", number = i);[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]for (i = 0; i < number; i++)[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]{[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]printf("%7.2f ", ptd[i]);[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]if (i % 7 == 6)[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]putchar('\n');[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]}[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]if (i % 7 != 0)[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]putchar('\n');[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]puts("Done.");[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]free(ptd);[/font][/bold]

[/align]

[align=left][bold][font=Times New Roman]return 0;[/font][/bold]

[/align]

[align=left][bold][font=CourierNewPS-BoldMT]}[/bold][/font]

[/align]

[align=center][bold][font=Wingdings]F[/font][/bold][bold][underline]Assessed exercises: 2, 3 & 4[/underline][/bold][bold][underline][/underline][/bold]

[/align][underline]Exercise 2 (3 points)[/underline]
[font=TimesNewRoman]Write a program that declares a 3 by 5 two-dimensional array and initialises it with some values of your choice. The program should:[/font]
    [*][font=Times New Roman]print all the values,[/font][*][font=Times New Roman]double the values[/font][*][font=Times New Roman]print again all the values[/font]

[align=left][font=TimesNewRoman]Write a function that is responsible for displaying the values and a second function that doubles the values. The functions could take as arguments the [bold]array name [/bold]and the [bold]number of rows [/bold]as arguments.[/font]

[/align][underline]Exercise 3 (3 points)[/underline]

[align=left][font=TimesNewRoman]Write a function that returns the largest value in a two-dimensional array of [bold]int[/bold]. Test the function in a program.[/font]

[/align][underline]Exercise 4 (4 points)[/underline]
[font=TimesNewRoman]Write a function that returns the largest value in a one-dimensional array of int. Test the function in a program. [italic]Allow the user to specify[/italic] the size of the array at execution time. Allow the user to provide the array values during execution. Use [bold]malloc() [/bold]to allocate space for the array. Use the resulting pointer to call the function that is responsible for processing the array elements. Print the largest value. Free the memory when you finished.[/font]
搜索更多相关主题的帖子: C语言 
2007-11-27 23:33
ecjtudream
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2007-11-10
收藏
得分:0 
全英文的啊


只能看懂一点点,所以不完全明白!

[[italic] 本帖最后由 ecjtudream 于 2007-11-27 23:46 编辑 [/italic]]
2007-11-27 23:44
只为流浪
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-11-27
收藏
得分:0 
我们学校是全英文教学的啦,所以很烦的,而且我电脑水平比较菜,呵呵
2007-11-27 23:47
wubizao
Rank: 1
来 自:荆州长大电信
等 级:新手上路
帖 子:223
专家分:0
注 册:2006-6-24
收藏
得分:0 
啊??
英语
我永远得痛...............

在路上走,看见了C,从此爱上了她
2007-11-28 11:32
beyond0702
Rank: 1
来 自: 桂 林
等 级:新手上路
帖 子:219
专家分:0
注 册:2007-11-17
收藏
得分:0 
全英文教学,爽啊
2007-11-28 12:16
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
以后请翻译后再发贴
--------------------------------------------------------------------------------------------------

[Google 翻译]


本周九日练习-指针,数组的m alloc和自由。
objectivesdesign和书写功能,即使用多维数组作为函数的参数。了解内存分配和取消分配使用的malloc和自由。
补习assessmentthe学生须尽一切演习。所有评估演习须提交在评估一节本单元下的冰毒。对于演习的第2 ,第3和第4 ,你应该提交的源代码,为每一个单独演习(没有设计文件是必需的) 。
deadlinethe截止日期前提交演习的是:周四2007年11月22日下午四时。

演习一
分析,并详细解释了下列程序。

/* dyn_arr.c -- 动态分配的数组 */

#include <stdio.h>
#include <stdlib.h> // for malloc(), free()

int main(void)
{
double * ptd;
int max;
int number;
int i = 0;
puts("What is the number of type double entries?");
scanf("%d", &max);
ptd = (double *) malloc(max * sizeof (double));
if (ptd == NULL)
{
puts("Memory allocation failed. Goodbye.");
exit(EXIT_FAILURE);
}
/* ptd now points to an array of max elements */
puts("Enter the values (q to quit):");
while (i < max && scanf("%lf", &ptd[i]) == 1)
++i;
printf("Here are your %d entries:\n", number = i);
for (i = 0; i < number; i++)
{
printf("%7.2f ", ptd[i]);
if (i % 7 == 6)
putchar('\n');
}
if (i % 7 != 0)
putchar('\n');
puts("Done.");
free(ptd);
return 0;
}


评估演练:二日,三日及四日


演习2 ( 3分)
写这个纲领宣布某3 × 5的两维阵列和initialises它与一些价值观的你的选择。该计划应该:

打印所有的价值观,
双重价值
打印再度所有价值观

写的职能,这是负责任的展示价值和第二个功能1倍,价值观。职能,可采取作为论据数组名称和行的数目作为论据。


演习3 ( 3分)


写一个函数返回最大的价值,在一个二维数组int 。测试功能的一个程序的难度。


演习4 ( 4分)
写一个函数返回最大的价值,在一维数组int 。测试功能的一个程序的难度。允许用户指定大小的数组,在执行时间。允许用户提供数组值在处决。使用的malloc ( )分配空间为数组。利用由此产生的指针,以呼叫功能,即负责处理数组元素。打印最大的价值。释放内存时,你完了。

[[italic] 本帖最后由 cosdos 于 2007-11-28 13:17 编辑 [/italic]]

—>〉Sun〈<—
2007-11-28 12:37
永夜的极光
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2721
专家分:1
注 册:2007-10-9
收藏
得分:0 
都是简单问题,就是鸟文看起来反感

从BFS(Breadth First Study)到DFS(Depth First Study)
2007-11-28 13:03
万兽无缰
Rank: 1
等 级:新手上路
威 望:1
帖 子:296
专家分:0
注 册:2007-8-27
收藏
得分:0 
演习4 ( 4分)
写一个函数返回最大的价值,在一维数组int 。测试功能的一个程序的难度。允许用户指定大小的数组,在执行时间。允许用户提供数组值在处决。使用的malloc ( )分配空间为数组。利用由此产生的指针,以呼叫功能,即负责处理数组元素。打印最大的价值。释放内存时,你完了。
最后一句翻译的有意思~~~

女朋友问我想怎么死~~~
             我说我想"爽死"
2007-11-28 13:08
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
/* 第一题 */

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    double * ptd;
    int max;
    int number;
    int i = 0;
   
    puts("What is the number of type double entries?");
    scanf("%d", &max);
   
    ptd = (double *) malloc(max * sizeof (double)); /* 使用 malloc() 分配空间 */
   
    if(ptd == NULL)   /* 如果没有分配到空间 */
    {
        puts("Memory allocation failed. Goodbye.");
        exit(EXIT_FAILURE);     /* 程序失败终止 */
    }

    puts("Enter the values (q to quit):");
    while(i < max && scanf("%lf", &ptd[i]) == 1)   /* 读入数据到数组中 */
        ++i;
    printf("Here are your %d entries:\n", number = i);
    for(i = 0; i < number; i++)     /* 显示数组中的数据 */
    {
        printf("%7.2f ", ptd[i]);
        if(i % 7 == 6)
            putchar('\n');
    }
    if(i % 7 != 0)
        putchar('\n');
        
    puts("Done.");
    free(ptd);         /* 释放分配的内存 */
    return 0;
}


/*********  第二题,看不懂,请楼主翻译 **********/


/**** 第三题  ****/
#include <stdio.h>

int max_v(int * p, int c, int r);

int main(void)
{
    int ar[5][5] = {1, 20, 3, 4, 50, 6, 70, 8, 90, 10, 11, 12, 13, 15};

    printf("MAX : %d\n", max_v((int *)ar, 5, 5));

    getchar();
    return 0;
}

int max_v(int * p, int c, int r)
{
    int i, n, max;
    n = c * r;
    max = p[0];
    for(i = 0; i < n; i++)
    {
        if(p[i] > max)
            max = p[i];
    }
    return max;
}


/**** 第四题 ***/

#include <stdio.h>
#include <stdlib.h>

int max_v(int * p, int n);

int main(void)
{
    int max, i = 0;
    int * p;
   
    puts("Arrary size:");
    while(scanf("%d", &max) != 1 || max < 1)
    {
        while(getchar() != '\n');
        puts("Error.");
    }
        
    if((p = (int *)malloc(max * sizeof(int))) == NULL)
    {
        puts("Error.");
        exit(1);
    }

    puts("Input value: ");
   
    while(i < max)
    {
        while(scanf("%d", p + i) != 1)
        {
            while(getchar() != '\n');
            puts("int");
        }
        i++;
    }
   
    printf("MAX : %d\n", max_v(p, max));

    free(p);
    puts("Done.");
    return 0;
}

int max_v(int * p, int n)
{
    int i, max;
   
    max = p[0];
    for(i = 0; i < n; i++)
    {
        if(p[i] > max)
            max = p[i];
    }
    return max;
}

[[italic] 本帖最后由 cosdos 于 2007-11-28 13:16 编辑 [/italic]]

—>〉Sun〈<—
2007-11-28 13:15
zbqf109
Rank: 1
等 级:新手上路
帖 子:289
专家分:0
注 册:2006-12-31
收藏
得分:0 
第二题就是让定义一个3×5的(矩阵)二维数组,对其初始化,然后输出所有元素,让所有元素的值×2,然后再次输出。
2007-11-28 16:25
快速回复:哪位高手可以解决以下4道C语言题目?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019934 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved