C++输入一个n*n的字符矩阵,把它左转90度后c语言输出矩阵.

用c++生成一个5*5的随机数矩阵,输出主对角线的和_百度知道
用c++生成一个5*5的随机数矩阵,输出主对角线的和
提问者采纳
#include&cstdlib&#include&ctime&using&namespace&int&main(){&srand(unsigned&(time(NULL)));&&&///初始化随机种&&int&array[5][5];&int&sum&=0;&&///求&&int&i,j;&for(i=0;i&5;i++)&&for(j=0;j&5;j++)&&&array[i][j]=rand();&&&///随机数&&cout&&&随机5*5数列&&&&cout&&&for(i=0;i&5;i++)&{&&for(int&j=0;j&5;j++)&&{&&&cout&&array[i][j]&&&\t&;&&&if(i&==&j)&&&&&&sum&+=array[i][j];&&&}&&cout&&&}&cout&&&sum=&&&sum&&&cin.get();&return&0;}
提问者评价
其他类似问题
随机数的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁C++输入一个n*n的字符矩阵,把它左转90度后输出._百度知道
C++输入一个n*n的字符矩阵,把它左转90度后输出.
输入仅包含一个测试样例。第一行是一个正整数n(0&n&=10)。接下来的n行输入n*n的字符矩阵,每行n个字符,字符之间没有分隔。输出格式将左转90度的矩阵分n行输出,每行n个字符,字符之间没有分隔。样例输入3abcdefghi
#include &iostream&void print_matrix(char *matrix, int size){
for (int row = 0; row & row ++)
for (int col = 0; col & col ++)
cout && matrix[row * size + col];
cout &&}void input_matrix(char *&matrix, int& size){
matrix = new char[size*size];
for (int i = 0; i & size* i ++)
cin && matrix[i];}void transpose_matrix(char *matrix, int size){
for (int row = 0; row & row ++)
for (int col = 0; col & col ++) if (row & col)
char temp = matrix[row * size + col];
matrix[row * size + col] = matrix[col * size + row];
matrix[col * size + row] =
}}void test_data(char *&matrix, int &size){
matrix = new char[size*size];
for (int i = 0; i & size* i ++)
matrix[i] = ((int)'a') +}int main(){
//test_data(matrix, size);
input_matrix(matrix, size);
print_matrix(matrix, size);
transpose_matrix(matrix, size);
print_matrix(matrix, size);
return 0;}
其他类似问题
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁C++写一段代码,由用户输入一个n阶矩阵元素值,用什么方法较好?如果想一次性输入,除了在代码中列出每一_百度知道
C++写一段代码,由用户输入一个n阶矩阵元素值,用什么方法较好?如果想一次性输入,除了在代码中列出每一
我知道可以先把元素值列在文本文档中,通过读操作用循环匹配录入,但我希望通过控制台输入,二重循环我也会,已经实现了,但还是要输入一个数回车一次,我是说输入所有数,然后回车,这样没办法实现吗?谢谢了,望高手指教另外,因为开始不确定数组大小,所以我使用了动态存储,但动态存储我只会用一维数组,不过我用一维数组也实现了这个功能,但我还是想知道动态存储能不能使用二维或多维数组。求解……
提问者采纳
下面是我写的两种方法,第一种是利用scanf的特性,获得非常好的容错输入,格式可以多样,最好的格式是一行全部输入,以空格为分割,或者直接按照矩阵的输出自然格式进行输入。第二种方式是自己解析字符串,显得比较麻烦,但灵活,可以自主调整,缺点是如果自己处理字符串不好的话,容错性会不好。注:gcc下编译的,C99,纯C吧,呵呵,C++ = C with class,兼容C,话说C\C++不分家的。如果需要可以改成C++的某些特性实现,很容易做的。但这种比较底层处理用C还是很方便的。/* * ===================================================================================== * *
read_matrix.c * *
Description:
Read N*N matrix from console. * *
08/14/:43 AM *
BUPT * * ===================================================================================== */#include&stdio.h&#include&stdlib.h&#include&ctype.h&#define DIM 3 /* the dimention of the matrix *//* function declarations */void LoopRead(int [][DIM]);void ParseRead(int [][DIM]);void printMatrix(int [][DIM]);int main(){ int matrix[DIM][DIM]; /* note: the other choice is using array instead of marix */ /* basically there are two methods: one is using double loops, the other is string-parsing */ /* here assume elements are all integer type, of course you can easilly extend to be double or float */ printf(&Please input your matrix: %d * %d\n&, DIM, DIM); LoopRead(matrix);/* method one */ printMatrix(matrix);/* print it */ printf(&Please input your matrix: %d * %d\n&, DIM, DIM); ParseRead(matrix);/* method two */ printMatrix(matrix);/* print it */ return 0;}/* due to the property of scanf(), you can either use &enter& or &spaces& as seperaters *//* in other words, you can input like : * 1 2 3 4 5 6 7 8 9 * to get * 1 2 3 * 4 5 6 * 7 8 9 * or just input in the form of a printed matrix . Or input like: * 1 * 2 * 3 * 4 * to get * 1 2 * 3 4 * Note: there are other possible formats to input. * */void LoopRead(int mat[][DIM]){
for(i = 0; i & DIM; i++) {
for(j = 0; j & DIM; j++)
scanf(&%d&, &mat[i][j]);/* read in one row */
} }}void ParseRead(int mat[][DIM]){/* read one line (or say one row) one time*//* the format is like: * 1 2 3 * 4 5 6 * 7 8 9 * elements in one row are separated by spaces * rows are separated by &Enter& * */ char *str = (char*)malloc(1000);/* caution: the 100 may not be enough */ if(NULL == str) {
fprintf(stderr, &Memory failure!&);
exit(EXIT_FAILURE); } char *start, *
for(i = 0; i & DIM; i++)/* read one line(row) */ {
fflush(stdin);
fgets(str, 1000, stdin);/* read one line (row)*/
for(start = isspace(*start); start++);/* skip the prefixed spaces */
int j = 0;
while(*end)/* loop until reach the end of line */
for(start = !isspace(*end); end++);/* find one element */
*end = '\0';/* mark the end for converting from string to int */
mat[i][j++] = atoi(start);/* convert to int and store it in matrix*/
while(isspace(*++end));/* skip more than one spaces */
} } free(str);}void printMatrix(int mat[][DIM]){ printf(&The matrix is:\n&);
for(i = 0; i & DIM; i++) {
for(j = 0; j &DIM; j++)
printf(&%d\t&, mat[i][j]);
printf(&\n&); }}
其他类似问题
按默认排序
其他2条回答
上三角矩阵即主对角线以下的元素都为0的矩阵,主对角线为从矩阵的左上角#include
就是比如N&1000 那就开m这个二维数组 拿正方形为例 边长为n的正方形有n平方个元素那就二重循环int m[],n;/*这地方你输入n*/for(i=0i&n;i++)
for(j=0;j&n;j++)
{/*输入m[i][j]*/} 就完了 我不是学C++的 输入语句我不会
其他类似问题
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁C++定义一个m行n列的矩阵类_百度知道
C++定义一个m行n列的矩阵类
要求用到构造函数、析构函数、拷贝构造、缺省构造等急求,明天要交
分少可以再加
提问者采纳
class matrix{ public:
matrix():r(0), p(0), j(NULL) {}
matrix(int m,int n):r(m),p(n)
j = new int*[m];
for(int i = 0; i & i++)
j[i] = new int[n];
for(int i = 0; i & i++)
delete[] j[i];
matrix(matrix& a) : r(a.r), p(a.p)
j = new int*[r];
for(int i = 0; i & i++)
j[i] = new int[p];
for(int k = 0; k & k++)
j[i][k] = a.j[i][k];
} private:
其他类似问题
按默认排序
其他1条回答
切,没分谁给你做啊。。。
您可能关注的推广回答者:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁c++ 输入n 输出n*n的数字矩阵 如 输入4输出 10 11 12 1 9 16 13 2 8 15 14 3 7 6 5 4_百度知道
c++ 输入n 输出n*n的数字矩阵 如 输入4输出 10 11 12 1 9 16 13 2 8 15 14 3 7 6 5 4
提问者采纳
#include&iostream&int main(){int i,j,n;cin&&n;int **a=new int*[n];for(i=0;i&n;++i){int *b=new int[n];for(j=0;j&n;++j)cin&&b[j];a[i]=b;}cout&&"数组: "&&for(i=0;i&n;++i){for(j=0;j&n;++j)cout&&a[i][j]&&" ";cout&&}for(i=0;i&n;++i)//释放数组delete [] a[i];delete[]return 0;}
提问者评价
虽然你的不对 但自己还是搞出来了
其他类似问题
按默认排序
其他1条回答
就是螺旋装输出是吧?这个写起来是有点烦,这个电脑里现在没有编程工具。给你讲一下思路吧。定义四个边界(四个整型数),分别代表上/下/左/右四个边界,如上面的话四个边界分别是0/3/0/3.然后定义count = n * n。
然后每输出一个数字count-1,外面是一个大循环当count不等与0.里面是四个while循环表示现场甫冠何攉蛊圭坍氦开在是向哪个方向走,并且一直走到这个方向的边界。你可以再定义一个move,等于1往下走,等于2往左走,以此类推。走到边界的时候改变方向并且相应那个边界+1或者是-1看是哪个边界了。这样你能明白么?
数字矩阵的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多关于 matlab输出矩阵 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信