想要在函数中转换一个简单的数组代码 [英] want to convert a simple array code in function

查看:77
本文介绍了想要在函数中转换一个简单的数组代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试转换我的代码或用作函数,这里是下面的代码





i am trying to convert my code or use as a function here is the code below


#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main(void)
{
int a[3][3],b[3][3],c[3][3],i,j;
for (i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
    cout<<"Enter value at a[ "<<i<<" ] [ "<<j<<" ] "<<endl;
    cin>>a[i][j];
    }
}
for (i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
    cout<<"Enter value at a[ "<<i<<" ] [ "<<j<<" ] "<<endl;
    cin>>b[i][j];
    }
}
for (i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
    c[i][j]=a[i][j]+b[i][j];
    }
}
for (i=0;i<=2;i++)
{
    for( j=0;j<=2;j++)
    {
//  1
    cout<<c[i][j]<<"  ";
    }
    cout<<endl;
}

getch();
}







//////我的代码是我试图转换的在功能//////////












////// My code is which i m trying to convert in function//////////




#include<iostream.h>
#include<conio.h>
#include<stdio.h>

int my(int[3][3],int[3][3],int[3][3]);

void main(void)
{
int d=my(int[3][3],int[3][3],int[3][3]);

getch();
}

int my(int a[3][3],int b[3][3],int c[3][3])
{
   for (int i=0;i<=2;i++)
   {
    for (int j=0;j<=2;j++)
    {
    cout<<"Enter a number:"<<endl;
    cin>>a[i][j];
    }
   }


   for (int l=0;l<=2;l++)
   {
    for (int k=0;k<=2;k++)
    {
    cout<<"Enter a number:"<<endl;
    cin>>b[l][k];

//c[l][k]=a[l][k]+b[l][k];
    }
   }


   for (int q=0;q<=2;q++)
   {
    for (int w=0;w<=2;w++)
    {
    int c;
    c[q][w]=a[i][j]+b[l][k];
//cout<<c[q][w]<<"  "<<endl;
    }
   }
   for(q=0;q<=2;q++)
   {
    for(w=0;w<=2;w++)
    {
    cout<<c[q][w]<<" ";
    }
    cout<<endl;
   }
}

推荐答案

一个可能更好的程序版本:

A possibly nicer version of your program:
#include<iostream.h>
#include<stdio.h>

void input_array(const char * name, int v[3][3]);
void output_array(const char *name, int v[3][3]);
void sum_array(int[3][3],int[3][3],int[3][3]);

int main(void)
{
  int a[3][3], b[3][3], c[3][3];

  input_array("a", a);
  input_array("b", b);

  sum_array(a,b,c);

  output_array("a", a);
  output_array("b", b);
  output_array("c", c);

  getchar();
  return 0;
}

void input_array(const char * name, int v[3][3])
{
  for (int i=0; i<3; i++)
    for (int j=0; j<3; j++)
    {
      cout<< name << "[" << i << "][" << j << "],  enter a number:"<< endl;
      cin>>v[i][j];
    }
}
void output_array(const char * name, int v[3][3])
{
  cout << name << " = {";

  for (int i=0; i<3; i++)
  {
    if ( i ) cout << ", ";
    cout << "{ ";
    for (int j=0; j<3; j++)
    {
      if ( j ) cout << ", ";
      cout << v[i][j];
    }
    cout << "}";

  }
  cout << "}" << endl;
}

void sum_array(int a[3][3],int b[3][3],int c[3][3])
{
   for (int i=0;i<=2;i++)
    for (int j=0;j<=2;j++)
      c[i][j]=a[i][j]+b[i][j];
}





顺便说一下:你显然使用旧的 C ++ 编译器。难道不能使用更新的吗?



By the way: you are apparently using an old C++ compiler. Couldn''t use a more updated one?


你的作业?

你需要查看/决定你在代码中找到的公共性,或者只是部分与更好地组织成一个功能。可能是:enter3x3,summe3x3,print3x3 ??
your homework?
You need to see/decide what are the "communality" you find in your code, or just part with are better organized into a funtion. Could be: enter3x3, summe3x3, print3x3 ??


这篇关于想要在函数中转换一个简单的数组代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆