1. 首页 > 科技

编写一个程序,完成以下要求: (1)定义一个数组,用循环实现从键 盘上输入 10 个正整数?

编写一个程序,完成以下要求: (1)定义一个数组,用循环实现从键 盘上输入 10 个正整数?

定义一个数组,用循环实现从键盘上输入10个正整数并放入数组。怎么做?还有第二问,急用!

#include

void main()

{

int i,a[10],sum=0,max=0;

float s;

printf("请输入10个整数:\n");

for(i=0;i<10;i++)

{

scanf("%d",&a[i]);

}

for(i=0;i<10;i++)

{

if(max

{

max=a[i];

}

sum+=a[i];

}

s=sum/10.0;

printf("元素的最大值为: %d,输入数组的平均值为:%ld",max,s);

}

1、编写一个程序实现如下功能,要求调用函数实现。 1)从键盘上输入10个整数存入一数组中,将它们按照从

#include

void Sequence(int str[],int N)

{

int i,j,s;

for(i=0;i

for(j=i+1;j

{

if(str[i]>str[j])

{

s=str[i];

str[i]=str[j];

str[j]=s;

}

else continue;

}

printf("排序后的数据顺序为:\n");

for(i=0;i

printf("%d ",str[i]);

printf("\n");

}

void Search(int str[],int x)

{

int i,j=11;

for(i=0;i<10;i++)

if(x==str[i])

{

printf("要查找的元素在数组中的第%d位。\n",i+1);

j=i;

break;

}

if(j==11)

{

printf("数组中没有%d,将%d插入数组重新排序。\n",x,x);

str[10]=x;

Sequence(str,11);

}

}

int main()

{

int str[11];

int i,x;

printf("输入10个整数:\n");

for(i=0;i<10;i++)

scanf("%d",&str[i]);

Sequence(str,10);

printf("输入要查找的数据x:\n");

scanf("%d",&x);

Search(str,x);

return 0;

}

我在编译器上运行了,可以达到你要求的效果。哪里不懂的话再问我。

1. 编写程序,定义一个整型的一维数组,从键盘给数组输入十个不同整数,用冒排序法对数组进行升序排序,

1.

Dim a(10) As Integer

Dim i As Integer

Dim j As Integer

Dim temp As Integer

For i = 1 To 10

TiShi = "请输入第 " & i & " 个数:"

a(i) = InputBox(TiShi, "输入数据")

Next

For i = 1 To 9

For j = 1 To 10 - i

If a(j) > a(j + 1) Then

temp = a(j)

a(j) = a(j + 1)

a(j + 1) = temp

End If

Next

Next

For i = 1 To 10

Print a(i)

Next

2.

Dim a(10) As Integer

Dim i As Integer

Dim p As Integer

Dim max As Integer

For i = 1 To 10

TiShi = "请输入第 " & i & " 个数:"

a(i) = InputBox(TiShi, "输入数据")

Next

max = a(1)

p = 1

For i = 2 To 10

If max < a(i) Then

max = a(i)

p = i

End If

Next

Print max, p

3.

Dim a(5, 5) As Integer

Dim avg(5) As Single

Dim i As Integer

Dim j As Integer

Dim temp As Integer

For i = 1 To 2

temp = 0

For j = 1 To 5

TiShi = "请输入第 " & i & " 名同学的第 " & j & "科成绩:"

a(i, j) = InputBox(TiShi, "输入成绩")

temp = temp + a(i, j)

Next j

avg(i) = temp / 5

Next

For i = 1 To 2

Print i & "号";

For j = 1 To 5

Print a(i, j);

Next

Print "平均分="; avg(i)

Next

4.

Dim a(100) As String

Dim i As Integer

Dim j As Integer

Dim temp As String

Dim Num As Integer

Num = InputBox("请输入英雄人数(<100)", "输入数据")

For i = 1 To Num

TiShi = "请输入第 " & i & " 名英雄姓名:"

a(i) = InputBox(TiShi, "输入数据")

Next

For i = 1 To Num - 1

For j = 1 To Num - i

If a(j) > a(j + 1) Then

temp = a(j)

a(j) = a(j + 1)

a(j + 1) = temp

End If

Next

Next

For i = 1 To Num

Print a(i)

Next

编一个程序,定义数组,用for循环语句,顺序输入10个实数,然后逆序输出这10个数。

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

//定义数组

int[] nums = new int[10]; 

Scanner input = new Scanner(System.in);

for (int i = 0; i < nums.length; i++) {

System.out.print("请输入第"+(i+1)+"个数:");

nums[i] = input.nextInt();

}

//遍历输出数组元素

for (int i = nums.length-1; i >= 0; i--) {

System.out.println(nums[i]);

}

}

}