将C编程转换为伪代码
我正在为一个算法编写一个伪代码。结果看起来很像C语言,因为它是我最熟悉的语言 代码如下:将C编程转换为伪代码,c,algorithm,pseudocode,C,Algorithm,Pseudocode,我正在为一个算法编写一个伪代码。结果看起来很像C语言,因为它是我最熟悉的语言 代码如下: START Declare int m, int n Input value of m and n Declare int array source[n], target[m][n] For(int i = 0; i < n; i++) Input value of source[i] For(int i = 0; i < m; i++) For(int j = 0; j
START
Declare int m, int n
Input value of m and n
Declare int array source[n], target[m][n]
For(int i = 0; i < n; i++)
Input value of source[i]
For(int i = 0; i < m; i++)
For(int j = 0; j < n; j++)
Input value of target[i][j]
Declare int maxsource, minsource, maxtarget, mintarget
For(int i = 0; i < n; i++)
If i == 0
Minsource = source[i]
Maxsource = source[i]
Else
If minsource > source[i]
Minsource = source[i]
Else if maxsource < source[i]
Maxsource = source[i]
Endif
Endif
For(int i = 0; i < m; i++)
For(int j = 0; j < n; j++)
If j == 0
Mintarget = target[i][j]
Maxtarget = target[i][j]
Else
If mintarget > target[i][j]
Mintarget = target[i][j]
Else if maxtarget < target[i][j]
Maxtarget = target[i][j]
Endif
Endif
If minsource == mintarget && maxsource == maxtarget
Print true
STOP
Else if maxtarget > maxsource
Print false
STOP
Endif
Print false
STOP
开始
声明int m,int n
m和n的输入值
声明int数组源[n],目标[m][n]
对于(int i=0;isource[i]
Minsource=源[i]
如果maxsource<源[i]
Maxsource=源[i]
恩迪夫
恩迪夫
For(int i=0;itarget[i][j]
Mintarget=目标[i][j]
如果maxtargetmaxsource
打印错误
停止
恩迪夫
打印错误
停止
我如何将其转换为更类似于的伪代码?有什么提示吗?如果我很了解您,您不希望伪代码看起来像程序代码,这样使用不同编程语言的人就可以使用它并解决相同的任务。我有一些建议可以帮助你实现这一目标。主要思想是,您应该避免使用编程语言的语法元素。从解决的问题而不是编程语言的角度来思考。用任何人都能读懂的简单语言编写伪代码。它应该很简单,没有编码技能的人可以遵循它并解决相同的任务。下面是查找数组中第二个最大元素的函数的简单伪代码示例
The function takes two inputs:
arr - array of integers
length - lenght of the array
It returns:
secMax - Second maximum element
然后是步骤(应使用简单的语言)
正如您所见,我只是解释了这些步骤,没有使用任何编程语言的语法元素。因此,现在任何人都可以使用他/她的舒适语言来实现我的步骤,以解决相同的任务
假设我想用C实现,我的代码如下:
//Defining the function according to the information above
//It takes to inputs and returns an integer
int findSecondMaximum(int arr[], int length)
{
//Define and Set the maximum and second maximum as the minimum possible value
int max, secondMax;
max = secondMax = INT_MIN;
for (int i = 0; i < length; i++) //Repeat for each element of the array
{
//If the current element is greater than the current maximum
if (vect[i] > max)
{
//then make the current maximum as second maximum
secondMax = max;
//Make the maximum as the current array element
max = vect[i];
}
/*else if the current array element is less than maximum but is greater than
second maximum*/
else if (vect[i] > secondMax && vect[i] < max)
{
//make it second maximum
secondMax = vect[i];
}
}
//return second maximum
return secondMax;
}
//根据上述信息定义函数
//它接受输入并返回一个整数
int findSecondMaximum(int arr[],int length)
{
//定义并设置最大值和第二个最大值作为可能的最小值
int max,secondMax;
max=secondMax=INT\u MIN;
for(int i=0;i最大值)
{
//然后将当前最大值设为第二个最大值
secondMax=max;
//将最大值设为当前数组元素
max=vect[i];
}
/*如果当前数组元素小于最大值但大于
第二个最大值*/
else if(vect[i]>secondMax&&vect[i]
我希望这会有所帮助。主要检查一下。
要么:
- 使用Pascal样式(例如),或
- 使用数学风格
If minsource > source[i]
minsource = source[i]
Else if maxsource < source[i]
maxsource = source[i]
Endif
在第一次使用时声明可以更好地阅读
Declare int minsource, maxsource
For (int i = 0; i < source.length; i++)
If i == 0
minsource = source[i]
maxsource = source[i]
Else
If minsource > source[i]
minsource = source[i]
EndIf
If maxsource < source[i]
maxsource = source[i]
Endif
Endif
Endfor
For (int i = 0; i < m; i++)
Declare int mintarget, maxtarget
{mintarget, maxtarget} = array_min_max(target[i]);
If minsource == mintarget && maxsource == maxtarget
Print true
STOP
Else if maxtarget > maxsource
Print false
STOP
Endif
Endfor
Print false
STOP
声明int-minsource,maxsource
For(int i=0;isource[i]
minsource=源[i]
恩迪夫
如果maxsource<源[i]
maxsource=源[i]
恩迪夫
恩迪夫
结束
For(int i=0;imaxsource
打印错误
停止
恩迪夫
结束
打印错误
停止
关于伪语言本身:上面的“Basic”及其可识别的C语言,我觉得有点恼人。我也会选择min_target或minTarget而不是minTarget
有很多种语言。在计算机科学中有一个长期的非传统。高级表达语言Algol68经常被使用,因为它甚至有一个用于类型和关键字的粗体字体。如果你能使用richt文本,那就太好了
类似Pascal的伪代码,可能是从Modula借用的IF-THEN-ELSE-END
就我个人而言,我不喜欢驼峰式,我更喜欢所有小写。什么是
伪代码?您的代码已在伪代码中读取。伪代码根据您所处的上下文具有不同的含义。这里有一个参考资料可以帮助你吗?@DipStax啊,很高兴知道。我只是在想也许它太像一个真实的代码而不是一个伪代码。谢谢@拉希德。谢谢@SelvaN:在你的伪代码中有一个主要的问题:你使用的是小字母和大写的混合:不要这样做:C++是区分大小写的,所以<代码> MimTale< /Cal>和<代码> MimTalue<代码>被认为是不同的。
If minsource > source[i]
minsource = source[i]
Endif
If maxsource < source[i]
maxsource = source[i]
Endif
Function {int min, int max} array_min_max(int[] array)
min = Integer.MAX_VALUE;
max = Integer.MIN_VALUE;
For value in array
min = Math.min(min, value);
max = Math.max(min, value);
Endfor
Endfunction
Declare int minsource, maxsource
For (int i = 0; i < source.length; i++)
If i == 0
minsource = source[i]
maxsource = source[i]
Else
If minsource > source[i]
minsource = source[i]
EndIf
If maxsource < source[i]
maxsource = source[i]
Endif
Endif
Endfor
For (int i = 0; i < m; i++)
Declare int mintarget, maxtarget
{mintarget, maxtarget} = array_min_max(target[i]);
If minsource == mintarget && maxsource == maxtarget
Print true
STOP
Else if maxtarget > maxsource
Print false
STOP
Endif
Endfor
Print false
STOP