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;
}
相关推荐
And_Ii1 小时前
LCR 168. 丑数
c++
CoderMeijun1 小时前
C++ 时间处理与格式化输出:从 Linux 时间函数到 Timestamp 封装
c++·printf·stringstream·时间处理·clock_gettime
tankeven5 小时前
HJ176 【模板】滑动窗口
c++·算法
OxyTheCrack5 小时前
【C++】一文详解C++智能指针自定义删除器(以Redis连接池为例)
c++·redis
whitelbwwww6 小时前
C++基础--类型、函数、作用域、指针、引用、文件
开发语言·c++
leaves falling6 小时前
C/C++ const:修饰变量和指针的区别(和引用底层关系)
c语言·开发语言·c++
tod1136 小时前
深入解析ext2文件系统架构
linux·服务器·c++·文件系统·ext
不想写代码的星星6 小时前
C++ 类型萃取:重生之我在幼儿园修炼类型学
c++
比昨天多敲两行6 小时前
C++11新特性
开发语言·c++
xiaoye-duck6 小时前
【C++:C++11】核心特性实战:详解C++11列表初始化、右值引用与移动语义
开发语言·c++·c++11