1.调试分析课本每一个例题,有可能的话更改成2-3个方法的新程序;
2.编程实现课本每一个编程习题。
3. 编程定义一个4X5矩阵和5X3矩阵,并计算它们的积。
4.编程输出1到n的平方的自然数构成的“魔方阵”,魔方阵是指它每一行和每一列及对角线之和均相等的方阵。
5.积累调试程序经验,收集错误信息原因(每个同学收集3-5条错误信息原因,并输入电脑形成文字)。
4.1
#include<iostream>
using namespace std;
int main()
{
int a[10];
int i;
for(i=0;i<10;i++)
a[i]=i*2+2;
for (i=0;i<10;i++)
{
cout<<a[i]<<'\t';
if((i+1)%5==0)
cout<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int i,math [40],n;
float aver = 0.0;
int unpassedcount = 0;
int highscorecount = 0;
cout<<"请输入学生人数:";
cin>>n;
cout<<"请输入成绩:";
for (i=0;i<n;i++)
{
cin>>math[i];
aver +=math[i];
}
aver/=n;
for (i=0;i<n;i++)
{
if (math[i]<60)unpassedcount++;
if (math[i]>=90)highscorecount++;
}
cout<<"平均分为:"<<aver<<endl;
cout<<"九十分以上的认数为:"<<highscorecount<<endl;
cout<<"不及格的认数为:"<<unpassedcount<<endl;
return 0 ;
}
4.3
#include<iostream>
#include <iomanip>
using namespace std;
int main()
{
int a[10],i,big;
cout<<"please input 10 numbers :\n";
for (i=0;i<10;i++)
cin>>a[i];
cout<<"the numbers are:";
for (i=0;i<10;i++)
cout <<setw(4)<<a[i];
cout<<endl;
big=a[0];
for (i=1;i<10;i++)
if(a[i]>big)
big=a[i];
cout<<"the big number is :"<<big<<endl;
return 0 ;
}
4.4
#include<iostream>
#include <iomanip>
using namespace std;
int main()
{
int a[10];
int i,j,t;
cout<<"please input 10 numbers :\n";
for (i=0;i<10;i++)
cin>>a[i];
cout<<"the numbers are:";
for (i=0;i<10;i++)
cout <<setw(4)<<a[i];
cout<<endl;
for (j=0;j<9;j++)
for (j=0;j<9-i;j++)
if (a[j]>a[j+1])
{t = a[j];a[j]=a[j+1];a[j+1]=t;}
cout <<"the sorted numbers are :";
for (i=0;i<10;i++)
cout<<setw(4)<<a[i];
cout<<endl;
return 0;
}
4.5
#include<iostream>
#include <iomanip>
using namespace std;
int main()
{
int i;
int f[40] = {1,1};
for (i=2;i<40;i++)
f[i] = f[i-2]+f[i-1];
for (i=0;i<40;i++)
{
if (i%4==0)
cout<<endl;
cout<<setw(12)<<f[i];
}
cout<<endl;
return 0;
}
#include<iostream>
#include <iomanip>
using namespace std;
int main()
{
int i,j;
int a[5][5];
for (i=0;i<5;i++)
for (j=0;j<5;j++);
for (i % 2==0)
a[i][j]=i*5+j+1;
else
a[i][4-j]=i*5+j+1;
for (i=0;j<5;j++)
{ for (j=0;j<5;j++)
cout<<setw(4)<<a[i][j];
cout<<endl;
}
return 0;
}
4.9
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
char str[50];
cout<<"please input strings:";
cin.get(str,100);
cout<<"字符串"<<str<<"的反向字符是:";
for (int i = strlen(str) - 1;i>=0;i--)
cout<<str[i];
cout<<endl;
return 0;
}
4.10
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
char s[] = "this is C programming test.:";
int i = 0,plen = 0,maxlen = 0,pSeat = 0;
while(s[i]!='\0')
{ while (s[i]!=''&&s[i]!='\0')
{
plen++;
i++;
}
if(plen>maxlen)
{
pSeat = i-plen;
maxlen=plen;
}
while(s[i] =='')
i++;
plen=0;
}
cout<<"最长的单词为:";
for (i=0;i<maxlen;i++)
cout<<s[pSeat+i];
cout<<endl;
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
char str[10];
cout<<"请输入字符串,直到输入hello后程序结束:"<<endl;
do{
cin>>str;
}
while(strcmp(str,"hello")!=0);
return 0;
}
4.13
#include <iostream>
using namespace std;
int main ()
{
char str[50];
int len = 0;
cout<<"请输入一个字符串:";
cin.get(str,50);
while(str[len]!='\0')
len++;
cout<<"字符串"<<str<<"的长度为:"<<len<<endl;
return 0;
}
课本习题:
1
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int a[5];
int i,big;
cout<<"please input five numbers:";
for(i = 0;i<5;i++)
cin>>a[i];
cout<<"the numbers are:";
for(i=0;i<5;i++)
cout<<setw(4)<<a[i];
cout<<endl;
big=a[0];
for(i=0;i<5;i++)
if(a[i]>big)
big=a[i];
cout<<"the big number is :"<<big<<endl;
return 0;
}
题目2为例题4.7,请老师看上面
题目3