Object-Oriented Programming and VC ++ implementation-liwei_2_3 (sum of row elements and diagonal lines of the matrix)

Source: Internet
Author: User

// Object-Oriented Programming and VC ++ implementation -- liwei_2_3 (sum of row elements and diagonal lines of the matrix)
// -- Object-Oriented Programming and VC ++ implementation
// -- Liwei_2_3
// -- Calculate the sum of the row elements and diagonal lines of the matrix.
// -- 11/5/2005 sat. Morning
// -- Computer lab

# Include <iostream>
Using namespace STD;

Const int size = 2;
Void matrix1 (int * seq, int * result_p, Int & cross_p );
Void matrix2 (INT seq [size] [size], int * result_p, Int & cross_p );
Void matrix3 (INT (* SEQ) [size], int * result_p, Int & cross_p );

Int main ()
{
Int array [size] [size];
Int result [size], cross = 0; // result stores the result of adding row elements; Cross stores the result of adding diagonal elements.
Int I, J;
Cout <"Please enter numbers" <size * size <":" <Endl;
For (I = 0; I <size; I ++) // data input
{
Result [I] = 0; // remember to initialize
For (j = 0; j <size; j ++)
Cin> array [I] [J];
}
// Matrix1 (* array, result, Cross); // The row address cross is a reference parameter.
// Matrix2 (array, result, Cross); // transmits an array
Matrix3 (array, result, Cross); // transmits an array
Cout <Endl <"================================== ========== "<Endl;
For (I = 0; I <size; I ++) // data output
Cout <"row" <I <"sum is:" <result [I] <Endl ;;
Cout <Endl <"Cross sum:" <cross <Endl;
Return 0;
}

Void matrix1 (int * seq, int * result_p, Int & cross_p)
{
Int I, J;
For (I = 0; I <size; I ++)
For (j = 0; j <size; j ++)
{
Result_p [I] = result_p [I] + * (SEQ + I * size) + J );
If (I = J)
Cross_p = cross_p + * (SEQ + I * size) + J );
}
}

Void matrix2 (INT seq [size] [size], int * result_p, Int & cross_p)
{
Int I, J;
For (I = 0; I <size; I ++)
For (j = 0; j <size; j ++)
{
Result_p [I] = result_p [I] + seq [I] [J];
If (I = J)
Cross_p = cross_p + seq [I] [J];
}
}

Void matrix3 (INT (* SEQ) [size], int * result_p, Int & cross_p) // array pointer
{
Int I, J;
For (I = 0; I <size; I ++)
For (j = 0; j <size; j ++)
{
Result_p [I] = result_p [I] + * (SEQ + I) + J );
If (I = J)
Cross_p = cross_p + * (SEQ + I) + J );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.