1.C语言:
include <stdio.h>
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
int i;
for (i=0; i<5; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}
2.C++:
#include
using namespace std;
int main()
{
int i;
cout<<“please put number in it:”;
int a[20]; /* i是随机值*/
for(i = 0; i < 20; i++)
{
cin>>a[i];
}
for(int n = 0; n < i; n++) {
cout<<a[n];
}
return 0;
3.Java:
public class Demo {
public static void main(String[] args) {
int[] arr=new int[]{1,2,3,4,5};//定义一个int数组类型的变量arr,并为其分配内存空间和初始化
int len=arr.length;//得到arr数组的长度
int total=0;//和
//注意,数组下标从0开始,到len-1结束
for(int i=0;i<=len-1;i++){
total+=arr[i];//求和
System.out.print(arr[i]+" ");//根据下标来输出arr每个变量
}
System.out.println("和为"+total);
}
}
4.using System;
namespace 简单数组程序 {
class Program {
static void Main(string[] args) {
Console.WriteLine("// 输入 x 退出本程序");
//请用户输入实数,循环直到正确输入
int 个数 = 10;
decimal[] 实数 = new decimal[个数];
for (int i = 0; i < 个数; i++) {
bool 结果 = false;
string 输入内容 = “”;
while (结果 == false) {
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.Write("请输入第 {0} 号实数: ", i + 1);
Console.BackgroundColor = ConsoleColor.Black;
输入内容 = Console.ReadLine();
if (输入内容.Trim(’ ‘).ToLower() == “x”) { Environment.Exit(0); }
if (输入内容.Trim(’ ').ToLower() == “”) { continue; }
结果 = 判断实数是否合法(输入内容);
}
实数[i] = decimal.Parse(输入内容);
Console.BackgroundColor = ConsoleColor.DarkGreen;
Console.WriteLine(“录入 {0} 号实数 = {1}。”, i + 1, 实数[i]);
Console.BackgroundColor = ConsoleColor.Black;
}
Console.BackgroundColor = ConsoleColor.DarkCyan;
Console.WriteLine(“按任意键开始逆序输出…”);
Console.BackgroundColor = ConsoleColor.Black;
Console.ReadKey();
for (int i = 个数 - 1; i >= 0; i--) {
Console.Write("{0}", 实数[i]);
if (i > 0) { Console.Write(","); }
}
Console.WriteLine();
Console.BackgroundColor = ConsoleColor.DarkCyan;
Console.Write("按任意键退出...");
Console.ReadKey();
}
static bool 判断实数是否合法(string 输入内容) {
decimal 转换的实数;
bool 是数字 = decimal.TryParse(输入内容, out 转换的实数);
if (是数字 == false) //转换数字失败就再次请求输入正确值。
{ Console.WriteLine("实数不正确!"); return false; } else {
return true;
}
}
}
}
5.#coding=utf-8
import numpy as np
a = np.array([1,2,3,4,5])
print a
b = np.zeros((2,3))
print b
c = np.arange(10)
print c
d = np.arange(2,10,dtype=np.float)
print d
e = np.linspace(1.0,4.0,6)
print e
f = np.indices((3,3))
print f