c语言的数组基础应用

 

首先设计如下界面;

编写如下代码:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace bb

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        int[]a=new int[5];

        int i = 0;

        

        private void button1_Click(object sender, EventArgs e)

        {

            if (i < 5)

            {

                a[i] = Convert.ToInt32(textBox1.Text);

                label2.Text += Convert .ToString( a[i])+"  ";

                i++;

                textBox1.Text = "";

            }

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            label2.Text = "排序前的数字序列:";

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            Array.Sort(a);

            label2.Text += "\n排序后的数字序列:"+a[0]+" "+a[1]+" "+a[2]+" "+a[3]+" "+a[4];

        }

    }

}

运行结果:

代码如下,如有帮助,请采纳一下,谢谢。

#include <stdio.h>
int main()
{
	int a[100];
	int nmb = 0;
	int i,j,tmp;
	printf("请输入一组正整数,并以-1结束:\n");
	while(1)
	{
		scanf("%d",&a[nmb]);
		if(a[nmb] == -1)
			break;
		else
			nmb++;

	}
	//排序
	//从小到大排序
	for (i = 0;i< nmb-1;i++)
	{
		for (j = 0;j< nmb -1-i;j++)
		{
			if (a[j] > a[j+1])
			{
				printf("交换%d和%d\n",a[j],a[j+1]);
				tmp = a[j];
				a[j] = a[j+1];
				a[j+1] = tmp;
			}
		}
	}
	printf("\n排序后的结果:\n");
	for (i = 0;i<nmb;i++)
	{
		if(i < nmb -1)
			printf("%d ",a[i]);
		else
			printf("%d\n",a[i]);
	}
	return 0;
}