0107作业

思维导图

练习:

要求在堆区连续申请5个int的大小空间用于存储5名学生的成绩,分别完成空间的申请、成绩的录入、升序

排序、

成绩输出函数以及空间释放函数,并在主程序中完成测试

要求使用new和delete完成

复制代码
#include <iostream>

using namespace std;
int score_output(int *iptr)
{
    cout<<"学生成绩如下:"<<endl;
    for(int i=0;i<5;i++)
    {
        cout<<iptr[i]<<"\t"<<endl;
    }
    return 0;
}

int score_input(int *iptr)
{
    for(int i=0;i<5;i++)
    {
        cout<<"请输入第"<<i+1<<"个学生的成绩"<<endl;
        cin >> iptr[i];
    }
    return 0;
}

int score_sort(int *iptr)
{
    cout<<"排序结果如下:"<<endl;
    for(int i=1;i<5;i++)
    {
        for(int j=0;j<5-i;j++)
        {
            if(iptr[j]>iptr[j+1])
            {
                   int temp = iptr[j];
                   iptr[j] = iptr[j+1];
                   iptr[j+1] = temp;
            }
        }
    }
    return 0;
}

void free_malloc(int *iptr)
{
    delete []iptr;
    iptr = NULL;
    cout<<"释放成功"<<endl;
}
int *Malloc_add()
{
    int *iptr = new int[5];
    return iptr;
}
int main()
{
    //空间申请
    int * iptr = Malloc_add();

    //成绩的录入
    score_input(iptr);
    score_output(iptr);

    //升序排序
    score_sort(iptr);

    //成绩的输出
    score_output(iptr);

    //释放空间
    free_malloc(iptr);
    return 0;
}
相关推荐
WW_千谷山4_sch1 小时前
洛谷B3688:[语言月赛202212]旋转排列(新解法:deque双端队列)
数据结构·c++·算法
漂流瓶jz1 小时前
UVA-11214 守卫棋盘 题解答案代码 算法竞赛入门经典第二版
c++·算法·dfs·aoapc·算法竞赛入门经典·迭代加深搜索·八皇后
fpcc2 小时前
并行编程实战——CUDA编程的Enhancing Memory Allocation
c++·cuda
白太岁3 小时前
通信:(3) 高并发网络通信:epoll + 边沿触发 + 非阻塞 IO + tcp
c语言·网络·c++·网络协议·tcp/ip
楼田莉子3 小时前
C++项目:日志&&线程池
linux·c++·学习·visual studio code
tankeven4 小时前
HJ93 数组分组
c++·算法
汉克老师5 小时前
GESP2024年3月认证C++二级( 第一部分选择题(9-15))
c++·循环结构·分支结构·gesp二级·gesp2级
plus4s5 小时前
2月19日(85-87题)
c++·算法
白太岁6 小时前
Redis:(2) hiredis 使用、C++ 封装与连接池
c语言·c++·redis·缓存
Desirediscipline6 小时前
cerr << 是C++中用于输出错误信息的标准用法
java·前端·c++·算法