latest Post

Program read and write dengan C++

Berikut contoh program read and write (matriks) dengan menggunakan Dev-C++

#include <iostream>
#include <fstream>

using namespace std;
int main()
{
	ifstream baca;
	ofstream tulis;
	baca.open("D:/readwrite.txt",ios::app);
	
	while(baca.fail() ){
		cout<<"file tidak dapat dibuka"<<endl;
		return 1;
	}
	int nilai [4][2];
	for (int i=0;i<4;i++){
		for(int j=0;j<2;j++){
			baca>>nilai[i][j];
		}
	}
	
	for (int i=0;i<4;i++){
		cout<<nilai[i][0]<<"\t"<<nilai[i][1]<<endl;
	}
	
tulis.open("D:/readwrite.txt",ios::app);

cout<<"\nProgram penjumlahan matriks"<<endl;
tulis<<"\nProgram penjumlahan matriks\n";
for (int i=0;i<4;i++){
cout<<nilai[i][0]<<"+"<<nilai[i][1]<<"="<<nilai[i][0]+nilai[i][1]<<endl; 
tulis<<nilai[i][0]<<"+"<<nilai[i][1]<<"="<<nilai[i][0]+nilai[i][1]<<endl; 		
}

cout<<"\nProgram pengurangan matriks"<<endl;
tulis<<"\nProgram pengurangan matriks\n";
for(int i=0;i<4;i++){
	cout<<nilai[i][0]<<"-"<<nilai[i][1]<<"="<<nilai[i][0]-nilai[i][1]<<endl; 
	tulis<<nilai[i][0]<<"-"<<nilai[i][1]<<"="<<nilai[i][0]-nilai[i][1]<<endl; 
}

cout<<"\nProgram perkalian matriks"<<endl;
tulis<<"\nProgram perkalian matriks\n";
for(int i=0;i<4;i++){
	cout<<nilai[i][0]<<"*"<<nilai[i][1]<<"="<<nilai[i][0]*nilai[i][1]<<endl; 
	tulis<<nilai[i][0]<<"*"<<nilai[i][1]<<"="<<nilai[i][0]*nilai[i][1]<<endl; 
}

baca.close();
tulis.close();
}

About Unknown

Unknown
Recommended Posts × +

0 comments:

Post a Comment