帮忙写这几个C++程序

来源:百度知道 编辑:UC知道 时间:2024/06/18 14:16:05
1. C++ a character string is stored as an array of chars and terminated with the special “null character” ‘\0’. Write a function
int string_length( char* s)
that determines the string length of s where s is a null-terminated array of chars.

2. Write rogram that will:

· Generate 20 random integers in the range 50-100 and store them into an array. (10 points)
· sorts the array of 20 integers (10 pts) and
· writes the 20 numbers from the array to a file c:\cs111

4. Write a function
int max_sum_to_n(int n)
which inputs a two digit integer n less than 28, and returns the largest three digit number whose digits add up to n.
For example if n=14 you function would return 950

5. Write a function called “mult” that takes in three integer matrices, each 3x3, A, B, and C and places in C the matrix multiplication of A and B.

写得很辛苦,楼主加点分吧。。

#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

// 1.....
int string_length( char* s) { return strlen(s); }

// 2.....
void gen() {
int A[20];
for(int i = 0; i < 20; ++i)
A[i] = rand() % 51 + 50;
sort(A, A + 20);
ofstream of("c:\\cs111");
for(int i = 0; i < 20; ++i)
of << A[i] << ' ';
}

// 4...
int max_sum_to_n(int n) {
int a, b, c;
for(a = 9; a >= 0; --a)
for(b = 9; b >= 0; --b)
for(c = 9; c >= 0; --c)
if(a + b + c == n)
return a * 100 + b * 10 + c;
}

// 5...
void mult(int A[3][3], int B[3][3], int C[3][3]) {
memset(C, 0, sizeof(C));
for(int i = 0; i < 3; ++i) {
for(int j