c++作业2

利用函数重载实现冒泡排序:

cpp 复制代码
#include <iostream>

using namespace std;
void sort(int a[],int len)
{
    int i,j,t;

    for(i=1;i<len;i++)
    {
        for(j=0;j<len-i;j++)
        {
            if(a[j]>a[j+1])
            {
                t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }
        }
    }
    cout<<"整数数组:";
    for(i=0;i<len;i++)
    {
        cout<<a[i]<<"\t";
    }
    cout<<endl;

}
void sort(float a[],int len)
{
    int i,j;
    float t;

    for(i=1;i<len;i++)
    {
        for(j=0;j<len-i;j++)
        {
            if(a[j]>a[j+1])
            {
                t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }
        }
    }
    cout<<"浮点数数组:";
    for(i=0;i<len;i++)
    {
        cout<<a[i]<<"\t";
    }
    cout<<endl;



}
int main()
{
    int a[5]={7,9,1,3,6};
    int len1=sizeof(a)/sizeof(int);
    float b[5]={3.5,4.1,1,6.6,0};
    int len2=sizeof(b)/sizeof(float);
    sort(a,len1);
    sort(b,len2);

    cout << "Hello World!" << endl;
    return 0;
}

在堆区申请一个数组的空间,并完成对该数组中数据的输入和输出,程序结束释放堆区空间

cpp 复制代码
#include <iostream>

using namespace std;

int main()
{
    int *p=new int[5];
    int i;
    for(i=0;i<5;i++)
    {
        cin>>p[i];
    }
    for(i=0;i<5;i++)
    {
        cout<<p[i]<<"\t";
    }
    cout<<endl;
    delete []p;
    cout << "Hello World!" << endl;
    return 0;
}

已知一个数组table,用宏定义,求出数据的元素个数

c 复制代码
#include <myhead.h>
#define ARR_SIZE(arr) sizeof(arr)/sizeof(arr[0])
int main(int argc, const char *argv[])
{
	int table[]={9,7,0,2,1,88};
	int size=ARR_SIZE(table);
	printf("%d\n",size);
	return 0;
}

给定一个整型变量a,写两段代码,第一个设置a的bit3,第二个清除a的bit3

c 复制代码
#include <myhead.h>
int main(int argc, const char *argv[])
{
	int a=0;
	int mask=1<<3;
	a=a|mask;
	printf("a=%d\n",a);

	a=31;
	mask=1<<3;
	mask=~mask;
	a=a&mask;
	printf("a=%d\n",a);
	return 0;
}
相关推荐
端平入洛10 小时前
delete又未完全delete
c++
端平入洛1 天前
auto有时不auto
c++
郑州光合科技余经理2 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1232 天前
matlab画图工具
开发语言·matlab
dustcell.2 天前
haproxy七层代理
java·开发语言·前端
norlan_jame2 天前
C-PHY与D-PHY差异
c语言·开发语言
哇哈哈20212 天前
信号量和信号
linux·c++
多恩Stone2 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054962 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月2 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js