Sunday, June 19, 2016

C++ Input and Output

Since I have a thing about competitive programming like HackerRank and TopCoder, I decide to start blog about it while learning C++ again after 8 years not using it.
I wont explain much unless needed, since the code explains itself
The first one is pretty simple, it starts with input and output



#include 
#include 

using namespace std;

/*
Second one is a matrix input
5 2
1 2 3 4 5
6 7 8 9 10
*/
void sampleIO_02() {
 int column, row;
 cin >> column >> row;

 //Create the matrix first
 int** matrix = new int *[row];
 for (int i = 0; i < row; i++) {
  matrix[i] = new int[column];
 }

 //Start pulling all the value
 for (int i = 0; i < row; i++) {
  for (int j = 0; j < column; j++) {
   cin >> matrix[i][j];
  }
 }

 //Test function
 cout << "SampleIO_02" << endl;
 for (int i = 0; i < row; i++) {
  for (int j = 0; j < column; j++) {
   cout << matrix[i][j] << " ";
  }
  cout << endl;
 }
}
/*
First input sample is to get 5 numbers and store it on an array
5
1 2 3 4 5
*/
void sampleIO_01() {
 int n;
 cin >> n;

 int arr[5];
 for (int i = 0; i < n; i++) {
  cin >> arr[i];
 }

 // test function
 cout << "SampleIO_01" << endl;
 for (int i = 0; i < n; i++) {
  cout << arr[i] << " ";
 }
 cout << endl;
}
/*
just some hello world input
*/
void sampleIO_00() {
 char name[50];
 cout << "please input your name" << endl;
 cin >> name;

 cout << "Well hello " << name << endl;
}
int main() {
 sampleIO_00();
 sampleIO_01();
 sampleIO_02();

 getchar();
 getchar();
}

/*
Andri
5
1 2 3 4 5
5 2
1 2 3 4 5
6 7 8 9 10
*/

No comments :

Post a Comment