Click here to Skip to main content
12,248,339 members (42,987 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ C
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;
   }
}
Posted 7-Jan-13 23:30pm
Comments
Richard MacCutchan 8-Jan-13 4:38am
   
What exactly are you trying to do? Most of this code is difficult to understand so it is also difficult to make any suggestions.
Argonia 8-Jan-13 4:39am
   
You have to return a value with the same type of the function if its different from void
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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];
}

By the way: you are apparently using an old C++ compiler. Couldn't use a more updated one?
  Permalink  
Comments
Member 9411249 8-Jan-13 6:53am
   
yes i am using turboC compiler
CPallini 8-Jan-13 8:01am
   
There are more modern alternatives. For instance you may use Visual C++ Express Edition.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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 ??
  Permalink  
Comments
Member 9411249 8-Jan-13 6:43am
   
no its not my home work i am preparing for my test and i will not use pointers for finding 2d array
qPCR4vir 8-Jan-13 7:07am
   
OK. Anyway, meanwhile, the CPallini´s solution shows exactly what I mean: convert your main function into a call to 3 “reusable” functions: for input, sum and output.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100