I'm suppose to write a program that will get from the user's keyboard one sets of numbers that will be read into two 2 dimensional arrays of size 4 by 3. Below is an example.
Array A
column 1 column 2 column 3
2 3 5
4 5 7
1 2 1
2 3 3
-Write a function that will add all of the numbers in the array and reutrn only the sum to the main.
-Print out the array and the sum.
-Use at least three functions.
This is what I have so far.....
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- typedef int ar_t [3][4];
- //! Enter array from user.
- //! Return by reference array of enter.
- void
- enter_array( ar_t & array ) {
- std::cout << "Enter array 3x4." << std::endl;
- // Proc all elements.
- for( int i = 0; i < 3; ++i )
- for( int j = 0; j < 4; ++j ) {
- std::cout << "[" << i+1 << "," << j+1 << "]:";
- std::cin >> array[i][j];
- }
- return;
- }
- //! Print array.
- void
- print_array( ar_t & array ) {
- std::cout << "Array 3x4." << std::endl;
- // Proc all elements.
- for( int i = 0; i < 3; ++i ) {
- for( int j = 0; j < 4; ++j ) {
- std::cout << array[i][j] << "\t";
- }
- std::cout << std::endl;
- }
- return;
- }
- //! Calc sum of array.
- int
- calc_sum( ar_t & array ) {
- // Sum element.
- int sum = 0;
- // Proc all elements.
- for( int i = 0; i < 3; ++i )
- for( int j = 0; j < 4; ++j )
- sum += array[i][j];
- return sum;
- }
- int main(int argc, char* argv[])
- {
- ar_t array;
- enter_array(array);
- print_array(array);
- std::cout << "Sum of array:" << calc_sum(array) << std::endl;
- return 0;
- }
Array A
2 3 5
4 5 7
1 2 1
2 3 3
Array B
4 4 4
2 3 4
1 2 1
8 9 8
program will use at least three functions to:
-Take the two arrays and generate a third array whose elements are the sum of the corresponding elements of the first two arrays then print both array and the array containing the sum print them one after the other on a column
This is what I have so far
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- // Array type for all arrays of task.
- typedef int ar_t [3][4];
- //! Enter array from user.
- //! Return by reference array of enter.
- void
- enter_array( ar_t & array ) {
- std::cout << "Enter array 3x4." << std::endl;
- // Proc all elements.
- for( int i = 0; i < 3; ++i )
- for( int j = 0; j < 4; ++j ) {
- std::cout << "[" << i+1 << "," << j+1 << "]:";
- std::cin >> array[i][j];
- }
- return;
- }
- //! Print array.
- void
- print_array( ar_t & array ) {
- std::cout << "Array 3x4." << std::endl;
- // Proc all elements.
- for( int i = 0; i < 3; ++i ) {
- for( int j = 0; j < 4; ++j ) {
- std::cout << array[i][j] << "\t";
- }
- std::cout << std::endl;
- }
- return;
- }
- //! Calc sum of array.
- int
- calc_sum( ar_t & array ) {
- // Sum element.
- int sum = 0;
- // Proc all elements.
- for( int i = 0; i < 3; ++i )
- for( int j = 0; j < 4; ++j )
- sum += array[i][j];
- return sum;
- }
- //! Calc sum of two arrays into new array by all positions.
- void
- sum_two_arrays( ar_t & one, ar_t & two, ar_t & sum ) {
- // Proc all elements.
- for( int i = 0; i < 3; ++i )
- for( int j = 0; j < 4; ++j )
- // sum all by index.
- sum[i][j] = one[i][j] + two[i][j];
- return;
- }
- // Do all second task requements.
- void
- sum_of_two_arrays_by_user( ar_t & one, ar_t & two, ar_t & sum ) {
- // User enter first array.
- std::cout << "Enter first array." << std::endl;
- enter_array( one );
- // User enter second array.
- std::cout << "Enter second array." << std::endl;
- enter_array( two );
- // Forming array of sum.
- sum_two_arrays( one, two, sum );
- // Print entered data.
- std::cout << "Entered arrays is:" << std::endl;
- // First array.
- print_array( one );
- std::cout << std::endl;
- // Second array.
- print_array( two );
- std::cout << std::endl;
- // Print result data.
- std::cout << "Sum of entered arrays is:" << std::endl;
- // Print sum array.
- print_array( sum );
- }
- void
- do_second_task() {
- // first array.
- ar_t one;
- // second array.
- ar_t two;
- // array of sum.
- ar_t sum;
- sum_of_two_arrays_by_user( one, two, sum );
- }
- int main(int argc, char* argv[])
- {
- // Do task function.
- do_second_task();
- return 0;
- }