早教吧 育儿知识 作业答案 考试题库 百科 知识分享

各位编程的高手朋友们请帮我解决这个问题吧急!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.
▼优质解答
答案和解析
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);
}
看了 各位编程的高手朋友们请帮我解...的网友还看了以下: