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;
}
相关推荐
端平入洛10 小时前
auto有时不auto
c++
哇哈哈20211 天前
信号量和信号
linux·c++
多恩Stone1 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马1 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝1 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc1 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
问好眼1 天前
《算法竞赛进阶指南》0x01 位运算-3.64位整数乘法
c++·算法·位运算·信息学奥赛
yyjtx1 天前
DHU上机打卡D31
开发语言·c++·算法
czxyvX1 天前
020-C++之unordered容器
数据结构·c++
会编程的土豆1 天前
2.25 做题
数据结构·c++·算法