day36 C++对C的扩充

要求在堆区连续申请5个int的大小空间用于存储5名学生的成绩,分别完成空间的申请、成绩的录入、升序排序、成绩输出函数以及空间释放函数,并在主程序中完成测试

要求使用new和delete完成

复制代码
#include <iostream>

using namespace std;

int main()
{
    int *ptr=new int[5];//申请5个连续的空间
    for(int i=0;i<5;i++)//循环录入成绩
    {
        cout<<"请输入学生的成绩:"<<endl;
        cin>>ptr[i];
    }

    for(int i=1;i<5;i++)//升序
    {
        for(int j=0;j<5-i;j++)
        if(ptr[j]>=ptr[j+1])
        {
            int temp=ptr[j];
            ptr[j]=ptr[j+1];
            ptr[j+1]=temp;
        }
    }
    cout<<"升序后的结果为:"<<endl;
    for(int i=0;i<5;i++)
    {
        cout<<ptr[i]<<'\t';
    }
    cout<<endl;
    for(int i=1;i<5;i++)//降序
    {
        for(int j=0;j<5-i;j++)
        if(ptr[j]<=ptr[j+1])
        {
            int temp=ptr[j];
            ptr[j]=ptr[j+1];
            ptr[j+1]=temp;
        }
    }
    cout<<"降序后的结果为:"<<endl;
    for(int i=0;i<5;i++)
    {
        cout<<ptr[i]<<'\t';
    }

    delete []ptr;
    ptr=NULL;

    return 0;
}

相关推荐
博客180021 小时前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝
郝学胜_神的一滴1 天前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake
众少成多积小致巨2 天前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
clint4566 天前
C++进阶(1)——前景提要
c++
夜悊6 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴6 天前
CMake 021: IF 条件判据详诠
c++·cmake
_wyt0016 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp
玖玥拾7 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
один but you7 天前
constexpr函数
c++
凡人叶枫7 天前
Effective C++ 条款41:了解隐式接口和编译期多态
java·开发语言·c++·effective c++