第六章作业

  • 学习要求

理解指针、地址、指针类型等相关概念。

掌握指针变量的定义、初始化、访问、指针运算和表达式。

掌握指针和数组、字符串的联系,灵活使用指针来处理它们。

掌握函数的传址调用。

掌握指针和数组作为函数参数。

进一步积累程序调试经验。
  •  实验作业

1.调试分析课本每一个例题,有可能的话更改成2-3个方法的新程序;

2.编程实现课本每一个编程习题。

3. 有n个人围成一个圆圈,从第一个人开始报数,从1到3循环,凡是报3的退出圈子,问最后退出圈子的人是原来的几号。

4.一个班有5个学生,每一个学生有几门成绩(到底是几门自已定,或3或4或5或6),定义函数来完成下面功能:

    计算第2门课成的平均分;

    找出有2门以上成绩不及格的学生,并输出其学号和全部课程成绩;

    找出平均成绩在90以上或者全部课程在85分以上的学生。

  • 作业要求

网上提交各个程序和相关要求的文字。作业不能拷贝。


/****************************
***       例6.1           ***
****************************/

#include<iostream>
using namespace std;

int main()
{
	int a =10;
	int *p;
	p=&a;
	cout<<"a = "<<a<<endl;
	cout<<"p = "<<p<<endl;
	cout<<"&a ="<<&a<<endl;
	cout<<"*p ="<<*p<<endl;
	cout<<"&p ="<<&p<<endl;
	cout<<endl;
	*p =15;
	cout<<"a = "<<a<<endl;
	cout<<"p = "<<p<<endl;
	cout<<"&a = "<<&a<<endl;
	cout<<"*p ="<<*p<<endl;
	cout<<"&p ="<<&p<<endl;

	return 0;

}


/*************************
***       例题6.2      ***
*************************/


#include<iostream>
using namespace std;

int main()
{
	int a,b;
	int *p=&a,*q =&b, *tp;
	cout<<"请输入两个数:";
	cin>>a>>b;
	if(a<b)
	{
		tp =p; p= q; q= tp;
	}
	cout<<"初始的两个数为:"<<a<<" "<<b<<endl;
	cout<<"从大到小排序后的数为:"<<*p<<" "<<*q<<endl;

	return 0;
}

/****************************
**         例题6.3         **
*****************************/

#include<iostream>
using namespace std;

int main()
{
	int a,b,t;
	int *p =&a,*q=&b;
	cout<<"请输入a和b的值:";
	cin>>a>>b;
	cout<<"初始的两个数分别为:"<<a<<" "<<b<<endl;
	if(a<b)
	{
		t =*p;*p=*q; *q=t;
	}
	cout<<"从大到小排序后的数为:"<<*p<<"  "<<*q<<endl;

	return 0;

}

/*********************************
***          例题6.4           ***
*********************************/

#include<iostream>
using namespace std;

int main()
{
	int a = 10, b=19,*p1,*p2;
	p1 =&a;
	p2 =&b;
	cout<<boolalpha<<(*p1 == *p2)<<endl;

	cout<<boolalpha<<(p1 == p2)<<endl;

	return 0;
}

/*******************************
**          例题6.5           **
*******************************/

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
	int a[10];
	for(int i =0;i<10;i++)
		a[i]=2*(i+1);
	for(i = 0;i<10;i++)
		cout<<setw(4)<<a[i];
	cout<<endl;

	return 0;
}

/*****************************
***         例题6.6        ***
*****************************/


#include<iostream>
using namespace std;

int main()
{
	char a[]="Hello world", b[20], *p, *q;
	p= a, q = b;
	for( ; *p!='\0';p++,q++)
		*q = *p;
	*q ='\0';
	cout<<"string1 is:";
	cout<<a<<endl;
	cout<<"string2 is:";
	cout<<b<<endl;

	return 0;
}

/************************
**       例题6.9       **
************************/


#include<iostream>
using namespace std;

int main()
{
	char s[50];
	char *p;
	cout<<"Please input a string:";
	cin.get(s,50);
	p = s;
	for( ;*p != '\0';)
		p++;
	cout<<"the length is :  "<<p-s<<endl;

	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值