You are here:

C++/help me with this program.

Advertisement


Question
write a c++ program that adds equivalent elements of the two -dimensional; arrays named first and second. both arrays sholud have two rows and three columns. for example, element [1][2] of the resulting array should be the sum of first [1][2]. the first and second arrays should bi initialized as follows:
FIRST     SECOND
16 18 23  24 52 77
54 91 11  16 19 59


#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
  int i,j;
  int val[2][3]={{16,23,52},{54,11,19}};

   int value[2][3]={{18,24,77},{91,16,59}};

  cout<<"       FIRST";
   cout<<"\t\t" << "SECOND";
  for(i=0;i<2;i++)
  {
     cout << endl;
     for(j=0;j<3;j++)
     {
        
        cout<<setw(5)<<val[i][j];
         cout<<setw(6)<<value[i][j];
  
     }
  }
  cout<<endl << endl;
  return 0;
}


Answer
Some pointers:

1) Instructions say "2 arrays named first and second".

2) You need an array to hold the result (the problem is to add 2 arrays, right?)

3) The 2 loops are good, but you need to add the elements as you go, into the result array.  You could output the result array as you go if you wanted to.

Bill

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.