C程序计算峰值数 [英] C program counts number of peaks

查看:175
本文介绍了C程序计算峰值数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

12 13 14 15 16
10 50 11 60 10
5  10 12 16 13
20 70 18 80 14
13  5 12 20 11





峰值:{50,60,70,80}



我尝试过:





Peaks are:{50,60,70,80}

What I have tried:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) {
FILE *pToFile = fopen("C:/Users/Win10User/Desktop/ana.txt","r");

int **A;int i,j; 
while (!feof (pToFile))
{
for(i=1,j=1;i<=3,j<=8;i++,j++);
fscanf (pToFile, "%d", &A[i][j]); 
printf ("%d ",A[i][j]);
if (A[i][j]>A[i-1][j]&& A[i][j]>A[i][j-1] && A[i][j]>A[i-1][j+1] && A[i][j]>A[i-1][j-1] && A[i][j]>A[i+1][j] && A[i][j]>A[i][j+1]&& A[i][j]>A[i+1][j-1] && A[i][j]>A[i+1][j+1]) 
{
printf("%d ",A[i][j]);
}
return (EXIT_SUCCESS);
} 

推荐答案

所以......到目前为止,你已经阅读了输入内容并且没有与它们相提并论?



我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!

我会给你一些线索:

1)想想问题:什么是峰值?据推测,它的值高于它周围的所有八个值:

So ... so far you've read the inputs in and don'e nothing with them?

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!
I'll give you a couple of clues though:
1) Think about the problem: what is a peak? Presumably, it's a value which is higher than all the eight values around it:
14 15 16
11 60 10
12 16 13

因此,首先将其视为二维数组,并将A分配为此 - 此时,您创建一个指针指向整数的指针,但您永远不会分配任何要存储的值的实际内存。

2)当你把它当作一个2D数组时,你可以编写一个带有数组和两个索引的函数:X和Y.它看起来在该点周围的八个位置(确保忽略越界索引)并返回true或false,具体取决于它是否为峰值。然后您需要的是一对嵌套循环和一个if语句...



试一试 - 它根本不复杂!



如果您遇到特定问题,请询问相关问题,我们会尽力提供帮助。但是我们不会为你做这一切!

So start by considering this as a two dimensional array, and allocate A as just that - at the moment, you create a pointer-to-a-pointer-to-a-integer, but you never allocate any actual memory for the values to be stored in.
2) When you consider this as a 2D array, you can write a function which takes the array and two indexes : X and Y. It "looks" at the eight locations around that point (making sure to ignore out-of-bounds indexes) and returns "true" or "false" depending on it being a peak or not. Then all you need is a pair of nested loops and a "if" statement...

Give it a try - it's not really complicated at all!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


你应该使用调试器来查看你的代码在做什么。

You should use the debugger to see what your code is doing.
for(i=1,j=1;i<=3,j<=8;i++,j++)



没有双计数器的循环,我想你想做嵌套循环。

C编程教程 - 39:嵌套循环 - YouTube [ ^ ]



当你不明白你的代码在做什么或为什么它做它做的时候,答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


A loop with double counter does not exist, I guess you wanted to do nested loops.
C Programming Tutorial - 39: Nested Loops - YouTube[^]

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于C程序计算峰值数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆