早教吧作业答案频道 -->其他-->
各位编程的高手朋友们请帮我解决这个问题吧急!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);
}
看了 各位编程的高手朋友们请帮我解...的网友还看了以下:
C++编程题请会的朋友帮忙改下错y=sh(1+sh(x))/(sh(2*x)+sh(3*x)),输 2020-04-26 …
作文比较简单急要“一只羊正在山坡上吃草,忽然东面来了一只凶狠的大灰狼,西边来了一只凶猛的老虎.这是 2020-05-13 …
结婚喜帖怎么写?急!请闽南人回答,谢谢!各位大哥、长辈,请帮帮小弟,我是闽南人,是南安的,按照我们 2020-05-13 …
请高手帮忙翻译一下下面的地址名称我的通信地址是:北京丰台区东高地南大红门1号内第一干休所100信箱 2020-06-06 …
英语翻译四川省成都市武侯区双安东巷1号2栋3单元4号(邮编610000)请帮忙翻译成多行邮件地址, 2020-06-16 …
帮忙翻译地址(给分)江苏省吴江市运东开发区2588号信昌电子业务部付佳收邮编215200帮忙翻译下 2020-07-01 …
请教高手,凸半圆的宏程序怎么编写,请举例?谢谢!关于数控加工中心宏程序的编写:请问R50的凸半球, 2020-07-10 …
假如你是红星中学高三(1)班的学生李华,你校校园网需要招收英文网络编辑(webeditor),请根据 2020-12-16 …
假如你是一名高中生李华,你校校园网需要招收英文网络编辑(webeditor),请根据以下提示,用英语 2020-12-21 …
假如你是某英文杂志负责“与读者对话”栏目的编辑。请根据下面这封读者来信写一封回信,帮助解决读者来信中 2021-01-14 …