| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1462 人关注过本帖, 1 人收藏
标题:发一道新加坡国大的编程作业
只看楼主 加入收藏
fantasysonic
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2011-9-29
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:14 
发一道新加坡国大的编程作业
发一道新加坡国大的编程作业,挑战的,请进来。
LABORATORY EXERCISE (Lab-II)1
Lab-II A: File reading and writing
Problem Statement: Consider a N x N matrix of random
numbers in the range 0 to 99. Write a random number
generator to generate a matrix of N x N (you can fix N in
the range 10 to 20) and write the matrix into a TEXT file –
call this matrix as MatrixA.txt. Read MatrixA.txt from your
program and replace all prime numbers with 0. Write your
output to another file OutputMatrix.txt. You should display
the following output:
============
MatrixA and OutputMatrix from MatrixA.txt and
OutputMatrix.txt files. (Lab Demonstrators will look for
these two files generated by your program automatically).
Number of Prime numbers: 23
=================
NOTE: Following information may be useful. Parts of the codes to read and
write to a file are given below. This is only one way and there are many
ways you may wish to write.
Input file looks like this:
First create files “MatA.txt” and MatB.txt”. Enter the values for a M=4, N=5
(4 x 5) matrix. Follow the style of arranging the numbers given in the
following sampleMatrix.txt file below.
sampleMatrix.txt:
Note - First two lines indicate the number of rows and number of
columns
=======
4
5
2 2 3 4 6
1 2 5 7 3
3 6 1 8 0
10 1 3 7 2
======
Code for reading a single (first) character from a file:
/*Declare a file pointer to sampleMatrix.txt*/
FILE *myFile;
/*number to be input*/
int ip;
/*Open the file for reading*/
myFile = fopen("sampleMatrix.txt","r");
/*Read the first integer, 4, to ip*/
fscanf(myFile,"%d", &ip);
/*close the file stream*/
fclose(myFile);
Code for writing a single character to a file:
/*Declare a file pointer*/
FILE *myFile;
/*Open the file for writing*/
myFile = fopen("output.txt","w");
/*Write a number*/
fprintf(myFile, "%d", 4);
/*close the file stream*/
fclose(myFile);

搜索更多相关主题的帖子: 新加坡 编程 generate reading another 
2011-09-29 22:06
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:3 
说实话,阅读这段英文描述的难度要高于编程的难度。
就是生成一个随机矩阵,输出到文件。将值为素数的元素清零,输出到另一个文件。

重剑无锋,大巧不工
2011-09-29 22:18
g79134682
Rank: 2
等 级:论坛游民
帖 子:80
专家分:75
注 册:2011-7-29
收藏
得分:3 
有道果然很强大。。哈哈。。但是。。新手完全看不懂啊。。。


实验(Lab-II)1
Lab-II一个文件阅读和写作
陈述问题:把一个N×N矩阵随机的
编号在0到99之间。写一个随机数
发电机产生一个矩阵的N x N(你可以解决N
10 ~ 20)范围,并将其用矩阵纳入一个文本文件-
这个矩阵作为MatrixA.txt打电话。 从你读MatrixA.txt
程序和更换所有质数0分。写下你的
另一个档案OutputMatrix.txt输出。 你应该显示
下列输出:
= = = = = = = = = = = =
MatrixA和OutputMatrix从MatrixA.txt
OutputMatrix.txt文件。 示威者将寻找实验室
这两个文件自动生成你的程序)。
数量的质数:23
= = = = = = = = = = = = = = = = =
注:以下信息可能有用。代码阅读部分
写一个文件下文。这是只有一个办法,还有很多
你可能想要写的方式。
输入文件看起来像这样:
先创建文件”MatA.txt”和MatB.txt”。 输入值的一次M = 4、5例
(4×5)矩阵。按照风格安排编号了
以下sampleMatrix.txt文件下面。
sampleMatrix.txt:
注:前两线指出号码行和数量的

= = = = = = =
4
5
2两个三个四个6
1—2五7 3
3六1 8 0
7 2 . 10 1 - 3
= = = = = =
代码阅读一单(第一)角色从一个文件:
/ *声明一个文件指针sampleMatrix.txt * /
文件* myFile;
/ *数目输入* /
智力ip;
/ *打开文件阅读* /
myFile =函数(“sampleMatrix.txt”、“r”);
整数/ *读第一、四、ip * /
fscanf(myFile,“% d”,&ip);
关闭文件流/ * * /
fclose(myFile);
写一个单字符代码到一个文件:
声明一个文件指针/ * * /
文件* myFile;
/ *打开文件写* /
myFile =函数(“output.txt”、“w”);
写下一个数字/ * * /
函数myFile,“% d”,4);
关闭文件流/ * * /
fclose(myFile);
2011-09-29 22:59
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:0 
机器翻译的还是差点意思。

重剑无锋,大巧不工
2011-09-29 23:02
zjk103
Rank: 2
等 级:论坛游民
帖 子:22
专家分:60
注 册:2011-5-13
收藏
得分:3 
这是神马东东??
2011-09-30 11:01
yucwei
Rank: 2
等 级:论坛游民
帖 子:25
专家分:46
注 册:2007-10-10
收藏
得分:3 
想玩挑战的我发一道来着:

移球问题
      给定 N 个盒子, 5≤N≤100, 第 i 个盒子装入 bi 个球, bi≥0,i=1,...,N,

  球的总数为 M=b1+b2+...+bN, 4≤M≤10000. 在两个非空的盒子中各取一个球放

  入另外的一个盒子中, 称为一次操作. 现要求使用最少的操作次数, 将所有的球

  并入到一个盒子中. 请编写程序给出相应的操作方案, 及总操作次数.

     说明:

    (1) 按所给条件, 上述问题必有解,无须判断可解性.

    (2) 原始数据由名为 EXAM3.TXT 的文本文件给出. 格式如下:

        第一行: 一个整数, 为盒子个数(N).

        第二行: 用空格分隔的 N 个整数, 为各盒子的小球个数(bi).

  所给的数据都是正确的.

    (3) 用文本文件输出计算结果 ( 文件名: ANS3.TXT ):

  格式: 在前面的若干行中, 每一行为一组相同的操作. 例如:连续从1#,2#盒各取

  5 个小球, 放入 3# 盒, 可写为: 1  2  3  5 , 即该行的四个数据依次为:

    第 1 个盒号    第 2 个盒号    目标盒号    从第 1 个盒中移出的球数

    最后一行有四个数据: 球的总数(M), 总操作次数 , 0, 0(结束标志)

    (4) 测试时, 若实际操作次数比最少次数多 1-3 次, 可适当给分, 超过 3 次

  或操作过程中出现非法数据时, 不给分.

  例: 设 EXAM3.TXT 的数据为

        5

        3  4  5  6  0

  则一个正确的输出文件为:

        2  3  4  3

        1  2  4  1

        1  3  4  2

        18  6  0  0
2011-09-30 11:12
fantasysonic
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2011-9-29
收藏
得分:0 
我只会做出随机数 RAMDOM NUMBER 的矩阵,输入,和输出文件,试过很多次都失败。以下是我自己的CODE

#include <stdio.h>
void main()
{     
    int a[50][50];
    int i,j,k,p,m,n;
 
    p=1;              
    while(p==1)
{
        printf("Please Enter N for N x N matrix.(N must be between 10 to 20)\n");
        scanf("%d",&n);
        if((n!=0)&&(10<=n)&&(20>=n))
  {
   printf("The Matrix you entered is :%d\n",n);
   p=0;
  }
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
                a[i][j]=0;
}
 
    j=n/2+1;
    a[1][j]=1;
    for(k=2;k<=99;k++)
{
        i=i-1;
        j=j+1;
        if((i<1)&&(j>n))
  {
            i=i+2;
            j=j-1;
  }
        else
  {
            if(i<1)    i=n;
            if(j>n)    j=1;
  }
        if(a[i][j]==0)
            a[i][j]=k;
        else
  {
            i=i+2;
            j=j-1;
            a[i][j]=k;
  }
}
 
for(i=1;i<=n;i++)
{
        for(j=1;j<=n;j++)
            printf("%5d",a[i][j]);
  printf("\n");
}
}
 
2011-09-30 20:44
larryliu
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:58
专家分:128
注 册:2011-9-30
收藏
得分:3 
看来我还是要继续好好学习啊~~
2011-09-30 22:03
fantasysonic
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2011-9-29
收藏
得分:0 
有谁能做出输出到TXT文件啊。。。 我试了好多例子方法都失败
2011-10-03 19:45
c821101017
Rank: 2
等 级:论坛游民
帖 子:33
专家分:10
注 册:2011-9-21
收藏
得分:3 
    这个应该不难吧
2011-10-03 20:27
快速回复:发一道新加坡国大的编程作业
数据加载中...
 
   



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

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