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;
}
相关推荐
草莓熊Lotso19 小时前
Qt 进阶核心:UI 开发 + 项目解析 + 内存管理实战(从 Hello World 到对象树)
运维·开发语言·c++·人工智能·qt·ui·智能手机
TonyLee0171 天前
LLVM安装(ubuntu22)
c++
weixin_445054721 天前
力扣热题51
c++·python·算法·leetcode
汉克老师1 天前
GESP2025年12月认证C++七级真题与解析(单选题8-15)
c++·dfs·bfs·二分·强联通分量·gesp7级·gesp七级
fqbqrr1 天前
2601C++,pmr管理内存
c++
君义_noip1 天前
【模板:矩阵加速递推】信息学奥赛一本通 1642:【例 2】Fibonacci 第 n 项
c++·线性代数·矩阵·信息学奥赛·csp-s
宠..1 天前
优化文件结构
java·服务器·开发语言·前端·c++·qt
编程之路,妙趣横生1 天前
C++11(上)
c++
微露清风1 天前
系统性学习C++-第十六讲-AVL树实现
java·c++·学习
cpp_25011 天前
P1583 魔法照片
数据结构·c++·算法·题解·洛谷