Showing posts with label how. Show all posts
how to sort a 2d array in C++
This program demonstrate how to sort 2d array in ascending order. The program explain it in 3x3 array you could change the values as required to meet your requirement
//prog for 2d array sort
#include<iostream>
#include<conio.h>
using namespace std;
void main() {
int arr[3][3];
int temp;
cout << "Enter 9 elements\n\n";
//Enter Element
for(int j = 0; j < 3; j++){
for(int i = 0; i < 3; i++){
cout << "Enter Elements no: " << i+j+1 << " : ";
cin >> arr[i][j];
}
}
cout << endl;
//print array
for(int j = 0; j < 3; j++){
for(int i = 0; i < 3; i++){
cout << arr[i][j] << "\t" ;
}
cout << endl;
}
// run the loop double times the number of row or column which ever is greater
for (int k = 0; k <6; k++){
//arrage elemnt in ascending order in a rows
for (int i = 0; i < 2; i++){
for (int j = 0; j < 3; j++){
if(arr[i][j] > arr[i+1][j]){
temp = arr[i][j];
arr[i][j] = arr[i+1][j];
arr[i+1][j] = temp;
}
}
//compare the last element of each row with first element of its next row
for (int i = 0; i < 2; i++){
if(arr[2][i] > arr[0][i+1]){
temp = arr[2][i];
arr[2][i] = arr[0][i+1];
arr[0][i+1] = temp;
}
}
}
}
cout << endl << endl;
// print array
for(int j = 0; j < 3; j++){
for(int i = 0; i < 3; i++){
cout << arr[i][j] << "\t" ;
}
cout << endl;
}
_getch();
}
This program arrange the rows value in in itself and then compare the last value of a row with the first value of the next row. And if it is greater it swaps them and again arrange the the row. The program continue to do it unless all the value get sorted. Go through the program and you will get it how it works
//prog for 2d array sort
#include<iostream>
#include<conio.h>
using namespace std;
void main() {
int arr[3][3];
int temp;
cout << "Enter 9 elements\n\n";
//Enter Element
for(int j = 0; j < 3; j++){
for(int i = 0; i < 3; i++){
cout << "Enter Elements no: " << i+j+1 << " : ";
cin >> arr[i][j];
}
}
cout << endl;
//print array
for(int j = 0; j < 3; j++){
for(int i = 0; i < 3; i++){
cout << arr[i][j] << "\t" ;
}
cout << endl;
}
// run the loop double times the number of row or column which ever is greater
for (int k = 0; k <6; k++){
//arrage elemnt in ascending order in a rows
for (int i = 0; i < 2; i++){
for (int j = 0; j < 3; j++){
if(arr[i][j] > arr[i+1][j]){
temp = arr[i][j];
arr[i][j] = arr[i+1][j];
arr[i+1][j] = temp;
}
}
//compare the last element of each row with first element of its next row
for (int i = 0; i < 2; i++){
if(arr[2][i] > arr[0][i+1]){
temp = arr[2][i];
arr[2][i] = arr[0][i+1];
arr[0][i+1] = temp;
}
}
}
}
cout << endl << endl;
// print array
for(int j = 0; j < 3; j++){
for(int i = 0; i < 3; i++){
cout << arr[i][j] << "\t" ;
}
cout << endl;
}
_getch();
}
This program arrange the rows value in in itself and then compare the last value of a row with the first value of the next row. And if it is greater it swaps them and again arrange the the row. The program continue to do it unless all the value get sorted. Go through the program and you will get it how it works
Lets Get Started with programming.
Writing your
first program in C/C++
According to me programming is not something to learn it
comes to you automatically by practicing. Writing program is more affectionate
in this respect then becoming bookish.
So here is your first program to start with try writing it
and understanding it.
1. // my first
program in C
2. #include <stdio.h>
3. void
main () {
4. printf("My
first program");
5. }
1. // my
first program in C++
2. #include <iostream>
3. using namespace std;
4. void
main () {
5. cout <<
"My first program";
6. }
OUTPUT PANEL
My first program
Explaining of program in C
Line
1: // my first program in C++
// indicates the start of comment all
the text written in the line next too this will be ignored as programming line
and itt will juxt be added in programming as it is. This is used for adding a
comment to tell the other programmer about any instruction or guiding him.It is
also used for the programmer to remember its own program. In this particular
case our comment is about the description of our program.
Line 2: #include
<stdio.h>
Lines that start with # are not the
regular programming lines these are the directives for pre-processor to add the
piece of code already made by different programmer which are most often used so
they are already added to the software so that time may be saved and all
programmer don’t have to write that same code every time. A programmer could
also make its own directives.
In our particular case we have added
<stdio.h> which is standard input
output header file. As we have to produce output on screen.
Line 3: void main () {
Every
program must have at least one function that is its main function. No program
could run without a main function you may add multiple function in a
program but the program will start from
its main function. Consider the construction of our main function in this
program void
main () {}
main
is the name of our function followed by parenthesis () because it is the
declaration of function.
In
parenthesis we may define any parameter for our function they are empty because
our program have no parameter. void is the return type of function return type
of any function must be defined it is the expression or value which the
function will return after its execution. We have used (void) which mean empty
our function will not return any value after its execution. The next thing is
{} anything written inside this will be the statement of our main program and
our function consist of these instruction.
It
is also called body of the function.
Line
4: printf("My first program");
This
line is statement. A statement is expression that produces some effect. In our
program this is the only line that produces some effect. printf() is command to
take out put in C lang. inside parenthesis () we write what to output. Anything written within ”” will be shown as it. printf is the function
statement that comes from the <stdio.h> that is why we have included that
file. If we want to take output in any other form other than standard output on
screen then we have to use the different directive or make one of our own.
;
is the most important part of the statement this is the indication of the end
of the statement to the compiler every single statement must be followed by
this semi colon otherwise there will be an error.
Line
5: }
Is
the end of our main function body.
Explanation in C++ for same program.
All
the things remains the same as in C. the changes are as follow.
·
Iostream is the standard input output directive
in C++.
·
Instead of printf(“”); structure
cout << “ ” ; syntax is used.
·
3rd line in C++ that is using
namespace std;
All the elements
of standard C++ library are stored in namespace std so most of source file will
contain this statement in start as we want to use C++ element cout is one of
them used in our program.
Try writing this
program to check your self. Practice make a man perfect. Best of luck. Hope our
blog will help you to reach to the destination of a good programmer.
How does Keyboard works.
Working of keyboard
How computer takes input from keyboard
The process of taking input from keyboard is accomplished in 5 steps. It is not actually the case that you enter key on keyboard and cpu just know it that which key you have pressed.
steps
1. You press a key on keyboard it activates the connection of that key circuit.
2. That circuit sends a specific scan code particular to every key from memory of keyboard called keyboard controller to the its part of memory called keyboard buffer.
Keyboard controller is that part of keyboard memory in which all scan codes of every key is already stored.
Keyboard buffer saves the scan code of that key which was pressed. It can save many keys code at once.
3. Keyboard controller sends an interrupt request to the sys. software.
4. The system software respond to the interrupt by reading the scan code from buffer.
5. System software passes this scan code to the CPU which processes the code according to the application software and performs the function.
How computer takes input from keyboard
The process of taking input from keyboard is accomplished in 5 steps. It is not actually the case that you enter key on keyboard and cpu just know it that which key you have pressed.
steps
1. You press a key on keyboard it activates the connection of that key circuit.
2. That circuit sends a specific scan code particular to every key from memory of keyboard called keyboard controller to the its part of memory called keyboard buffer.
Keyboard controller is that part of keyboard memory in which all scan codes of every key is already stored.
Keyboard buffer saves the scan code of that key which was pressed. It can save many keys code at once.
3. Keyboard controller sends an interrupt request to the sys. software.
4. The system software respond to the interrupt by reading the scan code from buffer.
5. System software passes this scan code to the CPU which processes the code according to the application software and performs the function.
Saturday, 27 September 2014
Posted by Unknown