| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1171 人关注过本帖
标题:哪位高手可以解决以下4道C语言题目?
取消只看楼主 加入收藏
只为流浪
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-11-27
收藏
 问题点数:0 回复次数:1 
哪位高手可以解决以下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
只为流浪
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-11-27
收藏
得分:0 
我们学校是全英文教学的啦,所以很烦的,而且我电脑水平比较菜,呵呵
2007-11-27 23:47
快速回复:哪位高手可以解决以下4道C语言题目?
数据加载中...
 
   



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

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