标签:10 arr int len ++ 数组 printf
数组是一个能存储多个同种类型数据的容器
//数组作为参数传递:传递时传递的仅仅是存放首地址的变量,传递时注意将数组长度一同传递
//索引越界:索引从0~length - 1
#include <stdio.h>
void getArr(int arr[],int len);
int main(){
int arr1[] = {1,2,3,4,5};
int len = sizeof(arr1) / sizeof(int);
printf("%zu\n",sizeof(arr1));//存放的是整个数组
getArr(arr1,len);
}
void getArr(int arr[],int len){
int i = 0;
printf("%zu\n",sizeof(arr));//只传过来存放首地址的变量
for(i = 0;i < len;i++){
printf("%d ",arr[i]);
}
}
//数组算法题
//1、求最值
#include <stdio.h>
void MaxMin(int arr[],int len);
int main(){
//打印最值
int arr[5];
int i,len = sizeof(arr) / sizeof(int);
printf("Input number:");
for(i = 0;i < 5;i++){
scanf("%d",&arr[i]);
}
MaxMin(arr,len);
}
void MaxMin(int arr[],int len){
int max,min,i = 0;
max = min = arr[0];
while(i < len){
if(arr[i] > max){
max = arr[i];
}
if(arr[i] < min){
min = arr[i];
}
i++;
}
printf("max = %d,min = %d",max,min);
}
//2、遍历求和1.0
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void RanArray(int arr[],int len);
void SumArray(int arr[],int len);
int main(){
//遍历数组求和
int arr[10] = {0},i;
int len = sizeof(arr) / sizeof(int);
RanArray(arr,len);
for(i = 0;i < len;i++){
printf("%d ",arr[i]);
}
SumArray(arr,len);
}
void RanArray(int arr[],int len){
//数组初始化,生成10个10~100的随机数
//传入的本就是地址,是直接修改地址中存储的数据,不用返回值
int i;
srand(time(NULL));
for(i = 0;i < len;i++){
arr[i] = rand() % 91 + 10;
}
}
void SumArray(int arr[],int len){
//求和
int sum = 0,i = 0;
while(i < len){
sum += arr[i];
i++;
}
printf("Sum of array: %d\n", sum);
}
//遍历求和2.0
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void RanArray(int arr[],int len);
int SumArray(int arr[],int len);
float AverArray(int arr[],int len);
int CountArray(int arr[],int len);
int main(){
//遍历数组求和
int arr[10] = {0},i,sum,count = 0;
int len = sizeof(arr) / sizeof(int);
float average = 0;
RanArray(arr,len);
for(i = 0;i < len;i++){
printf("%d ",arr[i]);
}
sum = SumArray(arr,len);
printf("\nSum of array: %d\n", sum);
average = AverArray(arr,len);
printf("Average of array:%0.2f\n",average);
count = CountArray(arr,len);
printf("%d个\n",count);
}
void RanArray(int arr[],int len){
//数组初始化,生成10个10~100的随机数
//传入的本就是地址,是直接修改地址中存储的数据,不用返回值
int i;
srand(time(NULL));
for(i = 0;i < len;i++){
arr[i] = rand() % 91 + 10;
}
}
int SumArray(int arr[],int len){
//计算平均值
int sum = 0,i = 0;
while(i < len){
sum += arr[i];
i++;
}
return sum;
}
float AverArray(int arr[],int len){
return (float)SumArray(arr,len) / len;
}
int CountArray(int arr[],int len){
//统计比平均值小的个数
int count = 0,i;
for(i = 0;i < len;i++){
if(arr[i] < AverArray(arr,len)){
count++;
}
}
return count;
}
//4、翻转遍历数组
#include <stdio.h>
int main(){
//遍历翻转数组
int arr[11] = {1,2,3,4,5,6,7,8,9,10,11};
int i,j,temp = 0;
for(i = 0,j = 10;i <= j;i++){
printf("%d\t",arr[i]);
}
printf("\n");
for(i = 0,j = 10;i < j;i++,j--){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
for(i = 0,j = 10;i <= j;i++){
printf("%d\t",arr[i]);
}
}
//5、乱序数组:
//方法1:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(){
//随机打乱数组
int arr[10] = {10};
int len = sizeof(arr) / sizeof(int);
//printf("%d",len);
int i = 0;
while(i < len){
printf("请输入第%d个数据:",i + 1);
scanf("%d",&arr[i]);
i++;
}
i = 0;
printf("乱序前:");
while(i < len){
printf("%d\t",arr[i]);
i++;
}
srand(time(NULL));
int rand1,rand2,temp = 0;
i = 0;
while(i < len){
rand1 = rand() % 10;
rand2 = rand() % 10;
temp = arr[rand1];
arr[rand1] = arr[rand2];
arr[rand2] = temp;
i++;
}
i = 0;
printf("\n乱序后:");
while(i < len){
printf("%d\t",arr[i]);
i++;
}
}
//方法2:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(){
int arr[10] = {10};
int len = sizeof(arr) / sizeof(int);
int i = 0;
while(i < len){
printf("请输入第%d个数据:",i + 1);
scanf("%d",&arr[i]);
i++;
}
printf("乱序前:");
i = 0;
while(i < len){
printf("%d\t",arr[i]);
i++;
}
for(i = 0;i < len;i++){
int temp,rd = rand() % 10;
srand(time(NULL));
temp = arr[rd];
arr[rd] = arr[i];
arr[i] = temp;
}
printf("\n乱序后:");
i = 0;
while(i < len){
printf("%d\t",arr[i]);
i++;
}
}
标签:10,
arr,
int,
len,
++,
数组,
printf
From: https://www.cnblogs.com/izxw/p/18362254