早教吧作业答案频道 -->其他-->
各位编程的高手朋友们请帮我解决这个问题吧急!Youaregivenawordpuzzlewhichyoumustsolve.Thedatafilecontainsthenumberofwordstolocate(int)followedbythewords,oneperline.Nextisthesizeoftheboardasrows(i
题目详情
各位编程的高手朋友们 请帮我解决这个问题吧 急!
You are given a word puzzle which you must solve. The data file contains the number of
words to locate (int) followed by the words, one per line. Next is the size of the board
as rows (int) and columns (int) followed by the actual board, one row per line. For
example, the supplied data file consists of 21 words followed by a 25x25 word board.
You are to read the words into a ragged array. This array will consist of 21 rows, where
each row represents 1 word. The board should be read into a second 25x25 char matrix.
For each word in the word list, identify its location in the board. For output, write the
board with the located words to an ASCIIDisplayer.
Suggestions
How you should proceed with the solution and things to think about.
1. Create the data structures for the ragged array to hold the word list. Each word in
the list will represent a right sized char array. The board is simply a 25x25
char matrix.
2. Read the data file loading both the word list and the board. When reading the
word list consider reading a word as a String, and then using the
toCharArray method to convert the string to a right-sized character array.
3. Write a small test routine which prints both of these data structures to a displayer.
It is nice to know that your program works correctly up to this point. The output
should match the input.
4. Write a search routine which, for each character, will search all 8 directions for
each word in the list. My solution had 3 nested for loops. For each location in the
board (2 indices, hence 2 nested loops), find the word (1 index into the word list).
The actual character by character matching will require additional indexing.
5. Consider using a 2nd 25x25 char matrix for output. Once a word is found, write
it into this output matrix. Printing the output matrix will give the above output.
The output matrix should be initialized to blank characters.
You are given a word puzzle which you must solve. The data file contains the number of
words to locate (int) followed by the words, one per line. Next is the size of the board
as rows (int) and columns (int) followed by the actual board, one row per line. For
example, the supplied data file consists of 21 words followed by a 25x25 word board.
You are to read the words into a ragged array. This array will consist of 21 rows, where
each row represents 1 word. The board should be read into a second 25x25 char matrix.
For each word in the word list, identify its location in the board. For output, write the
board with the located words to an ASCIIDisplayer.
Suggestions
How you should proceed with the solution and things to think about.
1. Create the data structures for the ragged array to hold the word list. Each word in
the list will represent a right sized char array. The board is simply a 25x25
char matrix.
2. Read the data file loading both the word list and the board. When reading the
word list consider reading a word as a String, and then using the
toCharArray method to convert the string to a right-sized character array.
3. Write a small test routine which prints both of these data structures to a displayer.
It is nice to know that your program works correctly up to this point. The output
should match the input.
4. Write a search routine which, for each character, will search all 8 directions for
each word in the list. My solution had 3 nested for loops. For each location in the
board (2 indices, hence 2 nested loops), find the word (1 index into the word list).
The actual character by character matching will require additional indexing.
5. Consider using a 2nd 25x25 char matrix for output. Once a word is found, write
it into this output matrix. Printing the output matrix will give the above output.
The output matrix should be initialized to blank characters.
▼优质解答
答案和解析
1.scanf的用法错误
是scanf("%f",&a[i][j]);而不是scanf("%f",a[i][j]);
2.printf的用法错误
是printf("%6f",a[i][j])或者printf("%d",(int)a[i][j]);而不是printf("%6d",a[i][j]);数组的声明是float类型则输出数据时也要指定好格式
3.函数传参错误
float sum(float a[][6],int j)的参数传递如下
float a[4][6];
sum(a,j);而不是sum(a[i][6],j)或sum(a[][6],j);
应该传递一个2维数组指针,而你传递的是一个float值或者是一个维数组指针
4.代码混乱
for(i=0;i<3;i++)
for(j=0;j<5;j++)
printf("%6d",a[i][j]);
printf("\n");
根本达不到输出一行数据后换行的要求
要改为
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
printf("%d ",(int)a[i][j]);
printf("\n");
}
另外:你举的例子的输出结果不正确
以下代码在VC6中运行正确
#include
float student_aver(float a[][6],int i);
float sum(float a[][6],int j);
float one_grade_aver(float a[][6],int j);
int main()
{
int i,j;
float a[4][6];
a[3][5]=0;
printf("please enter the grades:\n");
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
scanf("%f",&a[i][j]);
}
printf("Input:\n");
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
printf("%d ",(int)a[i][j]);
printf("\n");
}
for(i=0;i<3;i++)
a[i][5]=student_aver(a,i);
for(j=0;j<5;j++)
a[3][j]=sum(a,j);
printf("Output:\n");
for(i=0;i<4;i++)
{
for(j=0;j<6;j++)
printf("%0.1f ",a[i][j]);
printf("\n");
}
printf("Average:\n");
for(j=0;j<5;j++)
printf("%4.1f ",one_grade_aver(a,j));
printf("\n");
return 0;
}
float student_aver(float a[][6],int i)
{
int k;
float sum=0,aver;
for(k=0;k<5;k++)
sum=sum+a[i][k];
aver=sum/5;
return (aver);
}
float sum(float a[][6],int j)
{
int k;
float sum=0;
for(k=0;k<3;k++)
sum=sum+a[k][j];
return (sum);
}
float one_grade_aver(float a[][6],int j)
{
float aver;
aver=sum(a,j)/3;
return (aver);
}
是scanf("%f",&a[i][j]);而不是scanf("%f",a[i][j]);
2.printf的用法错误
是printf("%6f",a[i][j])或者printf("%d",(int)a[i][j]);而不是printf("%6d",a[i][j]);数组的声明是float类型则输出数据时也要指定好格式
3.函数传参错误
float sum(float a[][6],int j)的参数传递如下
float a[4][6];
sum(a,j);而不是sum(a[i][6],j)或sum(a[][6],j);
应该传递一个2维数组指针,而你传递的是一个float值或者是一个维数组指针
4.代码混乱
for(i=0;i<3;i++)
for(j=0;j<5;j++)
printf("%6d",a[i][j]);
printf("\n");
根本达不到输出一行数据后换行的要求
要改为
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
printf("%d ",(int)a[i][j]);
printf("\n");
}
另外:你举的例子的输出结果不正确
以下代码在VC6中运行正确
#include
float student_aver(float a[][6],int i);
float sum(float a[][6],int j);
float one_grade_aver(float a[][6],int j);
int main()
{
int i,j;
float a[4][6];
a[3][5]=0;
printf("please enter the grades:\n");
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
scanf("%f",&a[i][j]);
}
printf("Input:\n");
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
printf("%d ",(int)a[i][j]);
printf("\n");
}
for(i=0;i<3;i++)
a[i][5]=student_aver(a,i);
for(j=0;j<5;j++)
a[3][j]=sum(a,j);
printf("Output:\n");
for(i=0;i<4;i++)
{
for(j=0;j<6;j++)
printf("%0.1f ",a[i][j]);
printf("\n");
}
printf("Average:\n");
for(j=0;j<5;j++)
printf("%4.1f ",one_grade_aver(a,j));
printf("\n");
return 0;
}
float student_aver(float a[][6],int i)
{
int k;
float sum=0,aver;
for(k=0;k<5;k++)
sum=sum+a[i][k];
aver=sum/5;
return (aver);
}
float sum(float a[][6],int j)
{
int k;
float sum=0;
for(k=0;k<3;k++)
sum=sum+a[k][j];
return (sum);
}
float one_grade_aver(float a[][6],int j)
{
float aver;
aver=sum(a,j)/3;
return (aver);
}
看了 各位编程的高手朋友们请帮我解...的网友还看了以下:
那位英语水平上佳的朋友帮我翻译一下,我急用,不要用在线直译之类手拉手各位老师、朋友们:你们好!我叫 2020-04-07 …
管仲隰朋我急这用啊】【管仲,隰朋从于恒公而伐孤竹,春往冬反,迷惑失道.今人不止以其愚蠢心而师圣人之 2020-05-17 …
谜语:瞒了皇上两个月(打一个两个字的词语,不一定是成语)我感觉“两个月”是指“朋”,可是,好像没有 2020-06-27 …
急!!求高手写篇英语文章安慰我一刚失恋的MM朋友...急!!求高手写篇英语文章安慰我一刚失恋的MM 2020-07-24 …
各位编程的高手朋友们请帮我解决这个问题吧急!Youaregivenawordpuzzlewhich 2020-07-26 …
BLV240平方铝线的载流量是多少朋友您好:我想请教一下BLV-240平方铝线的载流量是多少?若两根 2020-11-10 …
骑自行车400作文要求1.要初一水平(更好的水平也行)2.我急用呐3.胡说的别进4.条理清晰(要有描 2020-11-21 …
帮我解一下下面的题,我急要!下面的量成不成正比例,请说明理由.1.小新跳高的高度和他的身高.2.订阅 2020-11-22 …
急求!帮忙翻译这个天使故事各位英语好的朋友,帮小妹我翻译一下这段话,我急用!谢谢各位!每一个人都有一 2020-11-23 …
有人能帮我把这个地址翻译成英文吗?急用哪位朋友能帮我把这个地址翻译成英文,我急用中国陕西省西安市高新 2021-02-05 …