c中的冒泡排序和getchar
bubble sorting and getchar in c
我正在使用microsoft visual studio 2012并试图制作一个冒泡排序。下面是我的代码:
#include "stdafx.h"
#include "String.h"
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int array[100], n, c, d, swap;
printf("enter numbers of elementsn");
scanf_s("%d",&n);
printf("enter %d integersn", n);
for (c = 0; c < n; c++){
scanf_s("%d", array);
}
for (c = 0; c < (n - 1); c++)
{
for (d = 0; d < n - c - 1; d++)
{
if (array[d]>array[d + 1]){
swap = array[d];
array[d] = array[d + 1];
array[d + 1] = swap;
}
}
}
printf("sorted list in ascending order:n");
for (c = 0; c < n; c++){
printf("%dn", &array[c]);
}
getchar();
return 0;
}
首先,我不能让控制台停留一个键输入。getchar()
似乎不工作,但我没有任何错误。另外,当我看到控制台时,我可以说数字是"-310892"。我不知道为什么。
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main(void)
{
int array[100],n,c,d,flag,swap;
printf("Enter the no. of elementsn");
scanf("%d",&n);
for(c=0;c<n;c++)
{
scanf("%d",&array[c]); // here you have to add & for assigning a address to variable in memory
}
for(c=0;c<(n-1);c++)
{
flag=0;
for(d=0;d<n-c-1;d++)
{
if(array[d]>array[d+1])
{
swap=array[d];
array[d]=array[d+1];
array[d+1]=swap;
flag=1;
}
}
if(flag==0)
break;
}
printf("sorted elements in ascending order:n");
for(c=0;c<n;c++)
{
printf("%dt",array[c]);// you want to print the element not its address so no need of &
}
getch();
return 0;}
- 请注意,我添加了额外的变量"标志",这有助于提高程序的效率,因为当你的元素完成排序时循环会中断,但在你的程序中循环可能会做一些额外的迭代。
我修正了别人提到的错别字,并添加了system("pause")
。在VS 2010上为我工作得很好。还没有访问VS 2012来测试它。下面是你的代码:
#include <string.h>
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("enter numbers of elementsn");
scanf("%d",&n);
printf("enter %d integersn", n);
for (c = 0; c < n; c++){
scanf("%d", &array[c]);
}
for (c = 0; c < (n - 1); c++)
{
for (d = 0; d < n - c - 1; d++)
{
if (array[d]>array[d + 1]){
swap = array[d];
array[d] = array[d + 1];
array[d + 1] = swap;
}
}
}
printf("sorted list in ascending order:n");
for (c = 0; c < n; c++){
printf("%dn", array[c]);
}
system("pause"); // <---- Added this!!!
return 0;
}
关于冒泡排序的实现:
- 你的
scanf_s
在第一个for
循环总是读取数字到数组的第一个位置。 - 你的
printf
在最后一个for
循环中需要一个整数,但你提供了一个地址。
为了防止控制台消失,您可以将getchar()
替换为system("pause")
,尽管这是不可移植的。
纠正这些事情,气泡排序对我有用:
#include "stdafx.h"
#include "String.h"
#include <iostream>
using namespace std;
#include <string.h>
int main() {
int array[100], n, c, d, swap;
printf("enter numbers of elementsn");
scanf_s("%d",&n);
printf("enter %d integersn", n);
for (c = 0; c < n; c++) {
scanf_s("%d", &array[c]);
}
for (c = 0; c < (n - 1); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d + 1]) {
swap = array[d];
array[d] = array[d + 1];
array[d + 1] = swap;
}
}
}
printf("sorted list in ascending order:n");
for (c = 0; c < n; c++){
printf("%dn", array[c]);
}
system("pause");
return 0;
}
相关文章:
- 如何在C++中对数组进行冒泡排序
- 我想在 c++ 中对单向链表进行冒泡排序,但我一直面临左值错误,无法解决它
- 在C++中使用冒泡排序时出现意外值
- 冒泡排序中所需的交换次数,而无需实际对其进行排序
- 如何对文件中数组中的单个列进行冒泡排序?C++
- 需要关于如何将我的随机数数组连接到冒泡排序的帮助
- 冒泡排序无法对C++中的对象的动态数组进行排序
- 冒泡排序.C++
- 链表的冒泡排序
- 有没有选择排序优于冒泡排序的输入
- C++:如何使用冒泡排序来重新排列我的数据
- 冒泡排序随机数
- 矢量下标超出范围-冒泡排序
- 使用冒泡排序比较类对象
- 在c++中使用冒泡排序/交换对字符串进行排序
- 矢量下标超出范围错误,C++,冒泡排序
- 冒泡排序、选择排序和插入排序
- 使用冒泡排序c++对结构数组的成员进行优先级排序
- 冒泡排序链表C++
- c中的冒泡排序和getchar