Programming

Just another WordPress.com weblog

CT211-ใส่ค่าใน Matrix 4×4 และรวมตัวเลขในแนว Matrix ซ้ายบนไปขวาล่าง

แสดงความเห็นโดย จั่น บน กรกฎาคม 14, 2008

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

void main(){

clrscr();

char comma;
int i = 0, j = 0;
int numrows = 4, numcols = 4;
int sumrow = 0, diagonalsum = 0;
int matrix[4][4] = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{13, 14, 15, 16 }
};

cout << “Program Calculate Summation of data in matrix”;
cout << “\nOutPut 1) Summation By Each Row”;
cout << “\nOutput 2) Summation By Diagonal”;
cout << “\n———”;
cout << “\nPlease Insert Data In Each Row”;
cout << “\nFor Example”;
cout << “\n1,2,3,4″;
cout << “\n5,6,7,8″;
cout << “\n10,11,12,13″;
cout << “\n14,15,16,17″;
cout << “\n\n”;

//Loop load data from keyboard into 2 dimension array

cout << “Please Insert Input\n”;
cout << “For example\n”;
cout << “1,2,3,4\n”;
cout << “and Press Enter\n”;
cout << “————\n”;
for(i=0; i<numrows; i++){

for(j=0; j<numcols; j++){

cin >> matrix[i][j];

//If it’s the last column, we will not keep comma
if(j <numcols-1){
cin >> comma;
}

}

cout << “\n”;

}

cout << “Output\n———-\n”;

for(i=0; i<numrows; i++){

for(j=0; j<numcols; j++){

sumrow += matrix[i][j];

if(i == j){

diagonalsum += matrix[i][j];

}

}

cout << “Sum Row ” << (i+1) << ” = ” << sumrow << “\n”;
sumrow = 0;

}
cout << “Diagonal Sum = ” << diagonalsum << “\n”;

getch();

}

ใส่ความเห็น

XHTML: คุณสามารถใช้แท็กเหล่านี้ได้: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>