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;
}
相关推荐
cch89181 分钟前
易语言与C++:编程语言终极对决
开发语言·c++
HABuo4 分钟前
【linux线程(三)】生产者消费者模型(条件变量阻塞队列版本、信号量环形队列版本)详细剖析
linux·运维·服务器·c语言·c++·ubuntu·centos
小肝一下32 分钟前
每日两道力扣,day2
c++·算法·leetcode·职场和发展
CheerWWW3 小时前
C++学习笔记——this关键字、对象生命周期(栈作用域)、智能指针、复制与拷贝构造函数
c++·笔记·学习
lucky九年3 小时前
GO语言模拟C++封装,继承,多态
开发语言·c++·golang
漫随流水3 小时前
c++编程:D进制的A+B(1022-PAT乙级)
数据结构·c++·算法
tankeven3 小时前
HJ159 没挡住洪水
c++·算法
charlie1145141914 小时前
嵌入式C++教程实战之Linux下的单片机编程:从零搭建 STM32 开发工具链(5):调试进阶篇 —— 从 printf 到完整 GDB 调试环境
linux·c++·单片机·学习·嵌入式·c
paeamecium4 小时前
【PAT】 - Course List for Student (25)
数据结构·c++·算法·pat考试
小黄人软件4 小时前
MFC为什么不报空指针异常 2宏定义不改源码 用替换 用Log函数替换printf等 #define printf Log 优化版底层类Log显示
c++·mfc