请您帮我写个程序,十分感谢!

来源:百度知道 编辑:UC知道 时间:2024/06/13 22:19:57
有N行M列的二维数组,1:要求:输入n,计算出第几个元素的行r与列c坐标;
2:要求:输入行row与列col,再计算出row和col所在元素是该二维数组的第几个元素。
谢谢,考试题目,谢谢!
第一个得出的结果不正确,还麻烦看看。谢谢!

第一个
#include "stdafx.h"
#include <stdio.h>
void main()
{
int M;
printf("数组多少列M=");
scanf("%d",&M);
int n;
printf("n=");
scanf("%d",&n);
int r ;
int c ;
int a;
c=n%M ;
a=n/M ;
a++;
r=a;
printf("r=%d\n",r);
printf("c=%d\n",c);
}
第二个
#include "stdafx.h"

#include <stdio.h>

void main()

{
int M ;
int row;
int col;
int s;
printf("数组多少列M=");
scanf("%d",&M);
printf("row=");
scanf("%d",&row);
printf("col=");
scanf("%d",&col);

s=(row-1)*M+col;
printf("n=%d\n",s);