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;
}
/******例4.2******/
#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<<"90分以上人数为:"<<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 number:\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 = 0;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 number:\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(i = 0;i<10;i++)
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;
}
/******例4.6******/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i;
int j;
int a[5][5];
for(i = 0;i<5;i++)
{
for(j = 0;j<5;j++)
{
if(i%2==0)
a[i][j] = i*5+j+1;
else
a[i][4-j] = i*5+j+1;
}
}
for(i = 0;i<5;i++)
{
for(j = 0;j<5;j++)
cout<<setw(4)<<a[i][j];
cout<<endl;
}
return 0;
}
/******例4.7******/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i, j, t, n, m;
int a[2][3];
cout<<"请输入二维数组的元素值:"<<endl;
for(i = 0;i<2;i++)
{
for(j = 0;j<3;j++)
{
cout<<"a"<<"["<<i<<"]"<<"["<<j<<"] = ";
cin>>a[i][j];
}
}
cout<<"该二维数组为:"<<endl;
t = a[0][0];
for(i = 0;i<2;i++)
{
for(j = 0;j<3;j++)
{
cout<<setw(4)<<a[i][j];
if(a[i][j]>t)
{
t = a[i][j];
n = i, m = j;
}
}
cout<<endl;
}
cout<<"该数组中最大的元素值为:"<<"a"<<"["<<n<<"]"<<"["<<m<<"] = "<<t<<endl;
return 0;
}
/******例4.8******/
#include <iostream>
using namespace std;
int main()
{
char str[50];
cout<<"Please input string: ";
cin.get (str,50);
cout<<"The string is: ";
cout<<str<<endl;
return 0;
}
/******例4.9******/
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[100];
cout<<"Please input string: ";
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>
using namespace std;
int main()
{
char s[] = "The 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;
}
/******例4.11******/
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[50];
cout<<"Please input a string:";
cin.get (str,50);
cout<<"The length of sring"<<str<<"is"<<strlen(str)<<endl;
return 0;
}
/******例4.12******/
#include <iostream>
#include <cstring>
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<<"请输入一个字符串: "<<endl;
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()
{
float a[5];
int i, j, t;
cout<<"请输入5个数:";
for(i = 0;i<5;i++)
cin>>a[i];
for(i = 0;i<5;i++)
{
for(j = 0;j<5-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<5;i++)
{
cout<<setw(4)<<a[i];
}
cout<<endl;
return 0;
}
/******习题2******/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i, j, t, n, m;
int a[2][3];
cout<<"请输入二维数组的元素值:"<<endl;
for(i = 0;i<2;i++)
{
for(j = 0;j<3;j++)
{
cout<<"a"<<"["<<i<<"]"<<"["<<j<<"] = ";
cin>>a[i][j];
}
}
cout<<"该二维数组为:"<<endl;
t = a[0][0];
for(i = 0;i<2;i++)
{
for(j = 0;j<3;j++)
{
cout<<setw(4)<<a[i][j];
if(a[i][j]>t)
{
t = a[i][j];
n = i, m = j;
}
}
cout<<endl;
}
cout<<"该数组中最大的元素值为:"<<"a"<<"["<<n<<"]"<<"["<<m<<"] = "<<t<<endl;
return 0;
}
/******习题3******/
#include <iostream>
using namespace std;
int main()
{
int i,j = 0, f[20] = {1,1};
for(i = 2;i<20;i++)
f[i] = f[i-1]+f[i-2];
for(i = 0;i<20;i++)
{
if(100<=f[i]&&f[i]<1000)
{
j++;
}
}
cout<<"前20个数中有 "<<j<<" 个3位数。"<<endl;
cout<<"第16项为:"<<f[15]<<endl;
return 0;
}
/******习题4******/
#include <iostream>
using namespace std;
int main()
{
int i,j, n, m, k, t;
j = n = m = k = t = 0;
char str[100];
cout<<"请输入一行文字:";
cin.get(str,100);
for(i = 0;i<100&&str[i]!='\0';i++)
{
if(str[i] >= 'A'&&str[i] <= 'Z')
j++;
else if(str[i] >= 'a'&&str[i] <='z')
n++;
else if(str[i] >='0'&&str[i] <= '9')
m++;
else if(str[i] == ' ')
k++;
else
t++;
}
cout<<"大写字母有 "<<j<<" 个。"<<endl;
cout<<"小写字母有 "<<n<<" 个。"<<endl;
cout<<"空格有 "<<k<<" 个。"<<endl;
cout<<"数字有 "<<m<<" 个。"<<endl;
cout<<"其他字符有 "<<t<<" 个。"<<endl;
return 0;
}
/******习题5******/
#include <iostream>
using namespace std;
int main()
{
int i;
char str[100];
cout<<"请输入字符串:";
cin.get(str,100);
cout<<"字符串"<<str<<"的反向字符串为:";
for(i = strlen(str)-1;i>=0;i--)
{
cout<<str[i];
}
cout<<endl;
cout<<"字符串的长度为:"<<strlen(str)<<endl;
return 0;
}
/******习题6******/
#include <iostream>
using namespace std;
int main()
{
int i, j =0;
char str1[100], str2[100];
cout<<"请输入字符串:";
cin.get(str1,100);
for(i = 0;i<100&&str1[i]!='\0';i++)
{
if(!(str1[i]>='0'&&str1[i]<='9'))
{
str2[j] = str1[i];
j++;
}
}
str2[j+1] = '\0';
cout<<"原字符串:"<<str1<<endl;
cout<<"改动后的字符串:"<<str2<<endl;
cout<<"改动后字符串的长度:"<<j<<endl;
return 0;
}
矩阵
/******矩阵******/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a[4][5], b[5][3], c[4][3];
int i, j, k, t = 0;
cout<<"请输入A矩阵:"<<endl;
for(i = 0;i<4;i++)
{
for(j = 0;j<5;j++)
{
cout<<"a["<<i<<"]"<<"["<<j<<"] = ";
cin>>a[i][j];
}
}
cout<<"请输入B矩阵:"<<endl;
for(i = 0;i<5;i++)
{
for(j = 0;j<3;j++)
{
cout<<"b["<<i<<"]"<<"["<<j<<"] = ";
cin>>b[i][j];
}
}
cout<<"A矩阵是:"<<endl;
for(i = 0;i<4;i++)
{
for(j = 0;j<5;j++)
{
cout<<setw(4)<<a[i][j];
}
cout<<endl;
}
cout<<"B矩阵是:"<<endl;
for(i = 0;i<5;i++)
{
for(j = 0;j<3;j++)
{
cout<<setw(4)<<b[i][j];
}
cout<<endl;
}
for(i = 0;i<5;i++)
for(j = 0;j<5;j++)
{
for(k = 0;k<5;k++)
{
t += a[i][k] * b[k][j];
}
c[i][j] = t;
}
cout<<"A矩阵与B矩阵的积为:"<<endl;
for(i = 0;i<4;i++)
{
for(j = 0;j<3;j++)
{
cout<<setw(4)<<c[i][j];
}
cout<<endl;
}
return 0;
}
魔方阵
/******魔方阵******/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i,k,c,N;
int m[50][50]={0};
cout<<"请输入魔方阵的阶数(范围在1~49):";
cin>>N;
i=0; k=N/2;
for(c=1;c<=N*N;c++)
{
if(m[i][k]!=0)
{
i++;
if(i>N-1)i=0;
i++;
if(i>N-1)i=0;
k--;
if(k<0)
k=N-1;
}
m[i][k]=c;
i--, k++;
if(i<0)i=N-1;
if(k>N-1)k=0;
}
cout<<N<<" 阶魔方阵"<<endl<<endl;
for(i=0;i<N;i++)
{
for(k=0;k<N;k++)
cout<<setw(4)<<m[i][k];
cout<<endl;
}
return 0;
}