1、一个有10个整数元素的数组,编写一个程序找出其中的最大数和其下标,并在主函数中打印最大数和相应下标
#include<iostream>using namespace std;int main() { void max(int *p, int length); int a[] = { 17,85,67,83,65,49,26,92,38,42 }; int length = sizeof(a) / sizeof(int);//求数组长度 max(a,length); return 0;}void max(int *p,int length) { int index; int i = 0,max = 0; for (; i<length; p++) { if (*p > max) { max = *p; index = i; } i++; } cout << "max= " << max << "/t" << "index= " << index << endl;}/*#include <iostream>using namespace std;void main(){ int a[10]={17,85,67,83,65,49,26,92,38,42}; for(int max=-32768,i=0,j=0;i<10;i++){ if(max<a[i]){ max=a[i]; j=i; } } cout<<"第"<<j+1<<"个元素为最大数,值为:"<<max <<", 下标为:"<<j<<endl;}*/2、编写一个程序,该程序提示用户输入一个字符串,然后程序按照大写字母个数输出该字符串(使用字符串数组来存储字符串)
#include<iostream>#include<cstring>using namespace std;int main() { char a[100]; cout << "请输入字符串:" << endl; cin >> a; int length = strlen(a);//获取字符串长度 for (int i = 0; i < length; i++) { if (a[i] >= 'a'&&a[i] <= 'z') { a[i] -= 32; } else if (a[i] >= 'A'&&a[i] <= 'Z') { //a[i] += 32; } } cout << a << endl; return 0;}3、编写一个程序,该程序提示用户输入学生姓名以及考试分数。该程序输出(假定每个班级中最多有学生30名) 1)班级平均分 2)考试分数低于班级平均分的学生姓名 3)最高分以及最高分的学生姓名
#include<iostream>#include<string>#include<cstdlib>using namespace std;int main() { int i,num; cout << "请输入学生个数" << endl; cin >> num; if (num > 30) { cout << "学生少于30人" << endl; } string *name = new string[num]; double *grade = new double[num]; if (grade == NULL || name==NULL) { cout << "Cannot allocate more memory, exit the PRogram./n"; exit(1); } char cinName[30]; double cinGrade; double maxGrade = 0; double sumGrade = 0; double avgGrade; for (i = 0; i < num; i++) { cout << "请输入学生姓名" << endl; cin >> cinName; cout << "请输入学生成绩" << endl; cin>> cinGrade; name[i] = cinName; grade[i] = cinGrade; sumGrade += grade[i]; } avgGrade = sumGrade/num; cout <<"这"<<num<<"位学生的均分为:" << avgGrade << endl; cout << endl; cout << "低于均分"; for (i = 0; i < num; i++) { if (grade[i] < avgGrade) { cout << "姓名为:" << name[i] << endl; } } cout << endl; cout << "最高分是"; int max; for (i = 0; i < num; i++) { if (grade[i] > maxGrade) { maxGrade = grade[i]; max = i; } } cout << "姓名为:" << name[max] << "/t" << "分数为:" << grade[max]; return 0;}/*#include <iostream>#include <string>using namespace std;struct student{ string name;//姓名 double score;//成绩};int main(int argc, char* argv[]){ cout << "input total number of students:"; int num;//学生总数 cin >> num; student* stus = new student[num]; for (int i = 0; i < num; i++)//输入姓名和成绩 { cout << i + 1 << " name:/t"; cin >> stus[i].name; cout << i + 1 << " score/t"; cin >> stus[i].score; } double sum = 0;//分数总和 double highest = 0;//最高分 string name;//最高分的名字 for (int i = 0; i < num; i++) { sum += stus[i].score; if (stus[i].score > highest) { highest = stus[i].score; name = stus[i].name; } } double avg = sum / num;//平均成绩 cout << "average score of class:" << avg << endl; cout << "higtest score:" << highest << " name:" << name << endl; return 0;}*/4、编写一个程序输入一个n*n的矩阵,求出两条对角线元素值之和
#include <iostream>using namespace std;int main() { int num; cout << "请输入对角矩阵的大小" << endl; cin >> num; int **p = new int*[num]; for (int i = 0; i <num; i++) p[i] = new int[num]; int sum = 0; int number; for (int i = 0; i < num; i++) { cout << "请输入矩阵元素"; cout << "第" << (i + 1) << "行" << endl; for (int j = 0; j < num; j++){ cout << "第" << (j + 1) << "列" << endl; cin >> number; p[i][j] = number; } } for (int i = 0; i < num; i++) { sum += p[i][i] + p[i][num-i-1]; } cout << "sum=" << sum << endl; return 0;}5、初始化一个矩阵F(5*5),元素取自随机函数,将其值传递给函数,实现矩阵转置。在主函数中输出结果。随机函数的原型 包含在stdlib.h中 是int rand(); 它产生一个0–65535的随机数(16位机器中)
#include<iostream>#include<stdlib.h>using namespace std;int main() { void inverse(int (*p)[5]); int F[5][5]; cout << "变化前" << endl; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { F[i][j] = rand(); cout << F[i][j] << "/t"; } cout << endl; } inverse(F); cout << "变化后" << endl; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { cout << F[i][j] << "/t"; } cout << endl; } return 0;}void inverse(int (*p)[5]) { for (int i = 0; i < 5; i++) { for (int j =i; j < 5; j++) { int t = p[i][j]; p[i][j] = p[j][i]; p[j][i] = t; } }}/*int main() { void inverse(int matrix1[5][5], int middle[5][5]); int F[5][5] ,middle[5][5]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { F[i][j] = rand(); } } cout << "变化前" << endl; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { cout << F[i][j] << "/t"; } cout << endl; } inverse(F,middle); cout << "变化后" << endl; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { cout << middle[i][j] << "/t"; } cout << endl; } return 0;}void inverse(int matrix1[5][5], int middle[5][5]) { //转置矩阵 int i, j; for (i = 0; i<5; i++) for (j = 0; j<5; j++) middle[j][i] = matrix1[i][j];}*/6、编写一个函数char* strfind(char*s,char*t),用于查找字符串t在字符串s中最右边出现的位置,如果没有找到则返回NULL。并编写一个主程序来测试该函数,在主程序中按行输入两个字符串,并输出结果。
/*#include<iostream>using namespace std;int main() { char* strfind(char*s, char*t); int num1, num2; char *index; while (1) { cout << "请输入长字符串的长度" << endl; cin >> num1; cout << "请输入短字符串长度" << endl; cin >> num2; if (num2 > num1) { cout << "短字符串长度大于长字符串,请重新输入" << endl; } else { char *s = new char[num1]; char *t = new char[num2]; cout << "请输入长字符串" << endl; cin >> s; cout << "请输入短字符串" << endl; cin >> t; index = strfind(s, t); int lengthI = strlen(index); int lengthT = strlen(t); int xiabiao = lengthI - lengthT; cout << index << endl; cout << "最右边出现的位置:" << xiabiao << endl; break; } } return 0;}char* strfind(char*s, char*t) { char *index; int i, j; int lengthS = strlen(s); int lengthT = strlen(t); char temp; for (i = 0; i < lengthS/2; i++) { temp = s[i]; s[i] = s[lengthS -1 - i]; s[lengthS - i-1] = temp; } for (i = 0; i < lengthT / 2; i++) { temp = t[i]; t[i] = t[lengthT - i-1]; t[lengthT - i-1] = temp; } if (strstr(s, t) == NULL) { index = "Not found!"; } else { index = strstr(s, t); } return index;}*/#include <iostream> #include <string> using namespace std;char *strfind(char *src, char *dst){ int len1 = strlen(src); int len2 = strlen(dst); int start = len1 - 1; int end = len2 - 1; int i, j = len2 - 1; for (i = start; i >= end; i--) { if (src[i] == dst[j]) { j--; if (j == -1) { cout << "the position is: " << i + 1 << endl; return &src[i]; } } else j = len2 - 1; } return NULL;}int main(){ char src[100]; char dst[100]; cout << "请输入一个字符串:" << endl; cin.getline(src, 100, '/n'); cin.clear(); cout << "请输入要查找的字符串:" << endl; cin.getline(dst, 100, '/n'); char *temp = strfind(src, dst); if (temp != NULL) { cout << "所寻找的字符串以及后面的字符串为:" << temp << endl; } else cout << "no such characters!" << endl; system("pause"); return 0;}新闻热点
疑难解答