c_动态内存分配
来源:互联网 发布:淘宝发布宝贝怎么预览 编辑:程序博客网 时间:2024/06/12 22:49
(1)//
// main.m
// 713
//
// Created by dllo on 15/7/13.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "713.h"
int main(int argc,constchar * argv[]) {
////1.一个数组中有30个数(随机产生[0,30]),将数组中重复的数字去除,动态创建数组保存剩下的数字
// int arr[30]={0};
// int num=0;
// int j=0;
// for (int i=0; i<30; i++) {
// arr[i]=arc4random()%(30-0+1);
// }
// printf("原数组:");
// for (int i=0; i<30; i++) {
// printf("%d ",arr[i]);
// }printf("\n");
/////*******************************
// for (int i=0; i<30; i++) {
// for (int j=i+1; j<30; j++) {
// if (arr[i]==arr[j]) {
// arr[j]=-1;
// }
// }
// }
// for (int i=0; i<30; i++) {
// if (arr[i]>=0) {
// num++;
// }
// }
// printf("不重复个数:%d\n",num);
// int *p=malloc(num*sizeof(int));
// for (int i=0; i<30; i++) {
// if (arr[i]>=0) {
// p[j++]=arr[i];
// }
// }
// printf("新数组:");
// for (int j=0; j<num; j++) {
// printf("%d ",p[j]);
// }printf("\n");
////2.已知一个数组20个元素(随机1到100之间包含1和100),求大于平均数的元素个数,并动态生成一个新数组保存(提示:malloc出20个元素保存).
// int arr[20]={0};
// float sum=0;
// int num=0,j=0;
// for (int i=0; i<20; i++) {
// arr[i]=arc4random()%(100-1+1)+1;
// }
// for (int i=0; i<20; i++) {
// printf("%d ",arr[i]);
// sum=sum+arr[i];
// }printf("\n");
// for (int i=0; i<20; i++) {
// if (arr[i]>sum/20) {
// num++;
// }
// }printf("平均值:%g\n",sum/20);
// int *p=malloc(20*sizeof(int));
// for (int i=0; i<20; i++) {
// if (arr[i]>sum/20) {
// p[j++]=arr[i];
// }
// }
// for (int i=0; i<num; i++) {
// printf("%d ",p[i]);
// }printf("\n");
// free(p);
////3.输入一个数组长度,动态创建数组,所有元素随机生成,输出元素中的最大值
// int num=0;
// int max=0;
// printf("输入一个数:");
// scanf("%d",&num);
// int *p=malloc(num*sizeof(int));
// for (int i=0; i<num; i++) {
// p[i]=arc4random()%(70-30+1)+30;
// }
// for (int i=0; i<num; i++) {
// printf("%d ",p[i]);
// }printf("\n");
// for (int i=0; i<num; i++) {
// if (max<p[i]) {
// max=p[i];
// }
// }
// printf("最大为:%d\n",max);
//free(p);
////4.写一个学生类型的结构体,然后把学生类型的结构体变量保存在堆空间,然后找到成绩最好的那个学生(学生包含成绩,年龄,姓名,性别)
// float max=0;
// int k=0;
// Student per1={78.5,23,"anyichen",'W'};
// Student per2={97.5,24,"ningzhiyuan",'m'};
// Student per3={86.5,25,"lizhiyuan",'m'};
// Student per4={95.5,26,"wenshixuan",'w'};
// Student per[4]={per1,per2,per3,per4};
// Student *p=malloc(4*sizeof(Student));
// memcpy(p, per, 4*sizeof(Student));
//// for (int i=0; i<4; i++) {
//// if (max<p[i].score) {
//// max=p[i].score;
//// k=i;
//// }
//// }
// printf("成绩:%g 年龄:%d 姓名:%s 性别:%c\n",p[k].score,p[k].age,p[k].str,p[k].sex);
// free(p);
////4.排序
// Student per1={78.5,23,"anyichen",'W'};
// Student per2={97.5,24,"ningzhiyuan",'m'};
// Student per3={86.5,25,"lizhiyuan",'m'};
// Student per4={95.5,26,"wenshixuan",'w'};
// Student per[4]={per1,per2,per3,per4};
// Student *p=malloc(4*sizeof(Student));
// memcpy(p, per, 4*sizeof(Student));
// for (int i=0; i<4-1; i++) {
// for (int j=0; j<4-1-i; j++) {
// if (p[j].score<p[j+1].score) {
// float temp=0;
// temp=p[j].score;
// p[j].score=p[j+1].score;
// p[j+1].score=temp;
// }
// }
// }
// for (int i=0; i<4; i++) {
// printf("成绩:%g 年龄:%d 姓名:%s 性别:%c\n",p[i].score,p[i].age,p[i].str,p[i].sex);
// }
// free(p);
///////////
// Students stu1={23,78.5,'W',"anyichen"};
// Students stu2={24,97.5,'m',"ningzhiyuan"};
// Students stu3={25,86.5,'m',"lizhiyuan"};
// Students *stu=calloc(3, sizeof(Students));
// stu[0]=stu1;
// stu[1]=stu2;
// stu[2]=stu3;
//
return0;
}
(2)//
// 713.h
// 713
//
// Created by dllo on 15/7/13.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "713.h"
struct student{
float score;
int age;
char str[20];
char sex;
};
typedefstruct student Student;
////**********************************
//定义结构体
struct Student{
int stuAge;
float stuScore;
char stuSex;
char stuName[20];
};
typedefstruct Student Students;
(3)//
// 713.m
// 713
//
// Created by dllo on 15/7/13.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import "713.h"
- c_动态内存分配
- c_动态内存分配
- C_动态内存分配,malloc/free
- 动态内存分配
- 关于动态内存分配
- 关于动态内存分配
- 动态内存分配
- 动态内存分配
- 动态内存分配
- 动态内存分配
- 动态内存分配
- C++ 动态内存分配
- VC动态内存分配
- 动态数组分配内存
- C++动态内存分配
- 动态内存分配
- 动态内存分配
- 动态内存分配
- Hadoop map任务个数分析
- 关于NSString的练习题
- 【高级算法】单纯形法求解线性规划问题(C++实现)
- HTML5第三弹:亦酷亦萌的网络拓扑图
- opencv cvSplit cvMerge
- c_动态内存分配
- 组合数学+整数分解 POJ 2992 Divisors
- UVALive 4253 Archery(二分+atan2应用)
- C/C++/IOS/Android/MFC/python等软件定制服务,有人需要吗?
- POJ 1293 Duty Free Shop
- 主机之间建立ssh互信
- 堆排序
- leetcode刷题,总结,记录,备忘238
- hdoj1000and1089(a+b)