Archive for 2014
Code to make transpose of matrix in C++ (algo)
This program demonstrate the conversion of matrix to its transpose using array and loops. For more detail on int, array, for loop and other basic commands see our other post on respective topic.
#include<iostream>
#include<conio.h>
using namespace std;
void main(){
int Matrix[50][50];
int transpose[50][50];
int x;
int y;
cout << "Enter No. of rows:";
cin >> x;
cout << "Enter No. of column:";
cin >> y;
for (int i = 0; i < x; i++){
for (int j = 0; j < y; j++){
cout << "please enter elements: ";
cin >> Matrix[i][j];
}
}
cout << "\nOrignal matrix:\n";
for (int i = 0; i < x; i++){
for (int j = 0; j < y; j++){
cout << Matrix[i][j] << '\t';
}
cout << endl;
}
for (int i = 0; i < x; i++){
for (int j = 0; j < y; j++){
transpose[j][i] = Matrix[i][j];
}
}
cout << "\nTranpose matrix:\n";
for (int i = 0; i < y; i++){
for (int j = 0; j < x; j++){
cout << transpose[i][j] << '\t';
}
cout << endl;
}
_getch();
}
#include<iostream>
#include<conio.h>
using namespace std;
void main(){
int Matrix[50][50];
int transpose[50][50];
int x;
int y;
cout << "Enter No. of rows:";
cin >> x;
cout << "Enter No. of column:";
cin >> y;
for (int i = 0; i < x; i++){
for (int j = 0; j < y; j++){
cout << "please enter elements: ";
cin >> Matrix[i][j];
}
}
cout << "\nOrignal matrix:\n";
for (int i = 0; i < x; i++){
for (int j = 0; j < y; j++){
cout << Matrix[i][j] << '\t';
}
cout << endl;
}
for (int i = 0; i < x; i++){
for (int j = 0; j < y; j++){
transpose[j][i] = Matrix[i][j];
}
}
cout << "\nTranpose matrix:\n";
for (int i = 0; i < y; i++){
for (int j = 0; j < x; j++){
cout << transpose[i][j] << '\t';
}
cout << endl;
}
_getch();
}
Thursday, 25 December 2014
Posted by Unknown
Program, code to sort array in ascending or descending order in C++
This program demonstrate how to sort a array in ascending or descending order.. in C++.
This program uses function to do this and call the function of ascending or decending function whichever may user selects.
In Ascending algo
The program uses swap algorithm in which 1st and 2nd digits are compared if 2nd is smaller the program swaps them and then compare 2nd and 3rd digit... and continue to do so till the end of array in this manner the greatest digit reaches at end position. This loops is again repeated n-1 times so that all digits attain their respective position.....
#include<iostream>
#include<conio.h>
using namespace std;
const int array_size = 10;
void data(int arr[]) {
char ch[2] = {'s', 't'};
for(int i = 0; i<array_size; i++){
cout << "Please Enter " << i+1 << ch[0] << ch[1] << " Number: ";
cin >> arr[i];
if(i == 0){ch[0] = 'n'; ch[1] = 'd';}
if(i == 1){ch[0] = 'r'; ch[1] = 'd';}
if(i >= 2){ch[0] = 't'; ch[1] = 'h';}
}
cout << endl;
}
void asscending(int arr[]) {
for(int j = 0; j<(array_size-1); j++){
for(int i = 0; i<(array_size-1); i++){
int a;
if(arr[i]>(arr[i+1])){
a = arr[i];
arr[i] = arr[i+1];
arr[i+1] = a;
}
}
}
cout << endl;
}
void descending(int arr[]) {
for(int j = 0; j<(array_size-1); j++){
for(int i = 0; i<(array_size-1); i++){
int a;
if(arr[i]<arr[i+1]){
a = arr[i];
arr[i] = arr[i+1];
arr[i+1] = a;
}
}
}
cout << endl;
}
void print(int arr[]) {
for (int i = 0; i <array_size; i++){
cout << arr[i] << endl;
}
cout << endl;
}
void cases(int arr[]) {
char sort;
cout << "Press a to sort in ascending and b to sort in desending order: ";
cin >> sort;
switch (sort){
case 'A':
case 'a':
asscending(arr);
cout << "The asending order is:" << endl;
print(arr);
break;
case 'B':
case 'b':
descending(arr);
cout << "The desending order is:" << endl;
print(arr);
break;
default:
cout << "wrong input";
cases(arr);
break;
}
cout << endl;
}
void main() {
int arr[array_size] = {0};
char a = 'a';
while(a != 'c'){
if(a == 'a' || a == 'A'){
data(arr);
cases(arr);
}
if(a == 'b' || a == 'B'){
cases(arr);
}
cout << "Press a to renter data press b to rearrange same data, c to terminate: ";
cin >> a;
}
}
This program uses function to do this and call the function of ascending or decending function whichever may user selects.
In Ascending algo
The program uses swap algorithm in which 1st and 2nd digits are compared if 2nd is smaller the program swaps them and then compare 2nd and 3rd digit... and continue to do so till the end of array in this manner the greatest digit reaches at end position. This loops is again repeated n-1 times so that all digits attain their respective position.....
#include<iostream>
#include<conio.h>
using namespace std;
const int array_size = 10;
void data(int arr[]) {
char ch[2] = {'s', 't'};
for(int i = 0; i<array_size; i++){
cout << "Please Enter " << i+1 << ch[0] << ch[1] << " Number: ";
cin >> arr[i];
if(i == 0){ch[0] = 'n'; ch[1] = 'd';}
if(i == 1){ch[0] = 'r'; ch[1] = 'd';}
if(i >= 2){ch[0] = 't'; ch[1] = 'h';}
}
cout << endl;
}
void asscending(int arr[]) {
for(int j = 0; j<(array_size-1); j++){
for(int i = 0; i<(array_size-1); i++){
int a;
if(arr[i]>(arr[i+1])){
a = arr[i];
arr[i] = arr[i+1];
arr[i+1] = a;
}
}
}
cout << endl;
}
void descending(int arr[]) {
for(int j = 0; j<(array_size-1); j++){
for(int i = 0; i<(array_size-1); i++){
int a;
if(arr[i]<arr[i+1]){
a = arr[i];
arr[i] = arr[i+1];
arr[i+1] = a;
}
}
}
cout << endl;
}
void print(int arr[]) {
for (int i = 0; i <array_size; i++){
cout << arr[i] << endl;
}
cout << endl;
}
void cases(int arr[]) {
char sort;
cout << "Press a to sort in ascending and b to sort in desending order: ";
cin >> sort;
switch (sort){
case 'A':
case 'a':
asscending(arr);
cout << "The asending order is:" << endl;
print(arr);
break;
case 'B':
case 'b':
descending(arr);
cout << "The desending order is:" << endl;
print(arr);
break;
default:
cout << "wrong input";
cases(arr);
break;
}
cout << endl;
}
void main() {
int arr[array_size] = {0};
char a = 'a';
while(a != 'c'){
if(a == 'a' || a == 'A'){
data(arr);
cases(arr);
}
if(a == 'b' || a == 'B'){
cases(arr);
}
cout << "Press a to renter data press b to rearrange same data, c to terminate: ";
cin >> a;
}
}
Wednesday, 24 December 2014
Posted by Unknown
Program in C++ to calculate words in sentence
This program demonstrate the calculation of words in sentence in a console application using C++
#include<iostream>
#include<conio.h>
using namespace std;
void main() {
char arr[1000] = { '-' };
int result = 0;
cout << "Enter the sentence use + for spaces and - to end sentence" << endl << endl;
for (int i = 0; i < 1000; i++){
cin >> arr[i];
if (arr[i] == '-'){
i = 2000;
}
}
for (int i = 0; i < 1000; i++){
cout << arr[i];
if (arr[i] == '-'){
i = 2000;
}
}
for (int i = 0; i < 1000; i++){
if (arr[i] == '+') {
result++;
}
}
cout << endl<< "Number of Words are: " << result+1;
_getch();
}
#include<iostream>
#include<conio.h>
using namespace std;
void main() {
char arr[1000] = { '-' };
int result = 0;
cout << "Enter the sentence use + for spaces and - to end sentence" << endl << endl;
for (int i = 0; i < 1000; i++){
cin >> arr[i];
if (arr[i] == '-'){
i = 2000;
}
}
for (int i = 0; i < 1000; i++){
cout << arr[i];
if (arr[i] == '-'){
i = 2000;
}
}
for (int i = 0; i < 1000; i++){
if (arr[i] == '+') {
result++;
}
}
cout << endl<< "Number of Words are: " << result+1;
_getch();
}
Posted by Unknown
code to calculate average
This is a sample code to calculate average using array and for loop... for understanding array and for loop you may consult our explanation on that.
#include<iostream>
#include<conio.h>
using namespace std;
void main() {
int arr[10] = { 0 };
int result = 0;
for (int i = 0; i < 10; i++){
cin >> arr[i];
}
for (int i = 0; i < 10; i++){
result += arr[i];
}
result /= 10;
cout << "The average is: " << result;
}
#include<iostream>
#include<conio.h>
using namespace std;
void main() {
int arr[10] = { 0 };
int result = 0;
for (int i = 0; i < 10; i++){
cin >> arr[i];
}
for (int i = 0; i < 10; i++){
result += arr[i];
}
result /= 10;
cout << "The average is: " << result;
}
sample code to calculate area of a circle
Allah Ditta wants a program that calculates and displays the area of a wheel given its radius. Use the constant value 3.14159 for π.
//area of wheel
#include<stdio.h>;
#include<conio.h>;
//executing main program
void main()
{
//creating vars to store data
int i;
float j = 3.14159;
//get radius from user
printf("Enter the radius of circle:");
scanf("%d", &i);
//displaying and calculating area
printf("The area of wheel is: %f", (i*i)*j);
//for disabling automatic close off of program after execution of program
_getch();
}//end main
Posted by Unknown
Coding sample tip calculator
Allah Ditta wants a program that calculates and displays the amount he should tip a waiter at a restaurant. The program should subtract any cold drink charges from the total bill and then calculate the tip (using a percentage) on the remainder.
#include<stdio.h>;
#include<conio.h>;
//executing main program
void main()
{
//creating integers to store data
int i;
int j;
int k;
//Taking input from user:
//total bill
printf("Enter the total bill: ");
scanf("%d", &i);
//tip percentage
printf("Enter the percentage amount of tip: ");
scanf("%d", &j);
//cold drinks
printf("Enter the cost of cold drinks: ");
scanf("%d", &k);
//calculating Displaying result
printf("\n \nThe tip is: %d ", ((i-k)*j)/100);
//for disabling automatic close off of program after execution of program
_getch();
}
these are coding samples to help you understand more and more.
Arithmetic Operators, Largest & Smallest Value And Odd & Even Numbers
//Arithmetic operation on three numbers
#include<stdio.h>;
#include<conio.h>;
//executing main program
void main()
{
//creating var for storing values
int i;
int j;
int k;
//take input from user
printf("Input three different integers \n");
scanf("%d %d %d", &i, &j, &k);
//calculating and displaying results of
//Adition
printf("\nThe sum is %d \n", i + j + k);
//average
printf("The average is %d \n", (i + j + k)/3);
//product
printf("The product is %d", i * j * k);
//smallest and largest no.
//first number
if (i < j & j<= k)
{
printf("\n \nThe smallest is %d", i);
} /*end if*/
if (j > i & j > k)
{
printf("\n \nThe largest is %d", i);
} /*end if*/
//2nd number
if (j < k & j <= i)
{
printf("\nThe smallest is %d", j);
} /*end if*/
if (j > k & j >= i)
{
printf("\nThe Largest is %d", j);
} /*end if*/
//3rd number
if (k < j & j<= i)
{
printf("\nThe smallest is %d", k);
} /*end if*/
if (k > j & j >= i)
{
printf("\nThe Largest is %d", k);
} /*end if*/
//chk and display even and odd no
//first no.
if ((i%2) == 0)
{
printf("\n \n%d is even", i);
}
else
{
printf("\n \n%d is odd", i);
}
//2nd no.
if (j%2 == 0)
{
printf("\n \n%d is even", j);
}
else
{
printf("\n \n%d is odd", j);
}
//3rd no.
if (k%2 == 0)
{
printf("\n \n%d is even", k);
}
else
{
printf("\n \n%d is odd", k);
}
//for disabling automatic close off of program after execution of program
_getch();
}//end
#include<stdio.h>;
#include<conio.h>;
//executing main program
void main()
{
//creating var for storing values
int i;
int j;
int k;
//take input from user
printf("Input three different integers \n");
scanf("%d %d %d", &i, &j, &k);
//calculating and displaying results of
//Adition
printf("\nThe sum is %d \n", i + j + k);
//average
printf("The average is %d \n", (i + j + k)/3);
//product
printf("The product is %d", i * j * k);
//smallest and largest no.
//first number
if (i < j & j<= k)
{
printf("\n \nThe smallest is %d", i);
} /*end if*/
if (j > i & j > k)
{
printf("\n \nThe largest is %d", i);
} /*end if*/
//2nd number
if (j < k & j <= i)
{
printf("\nThe smallest is %d", j);
} /*end if*/
if (j > k & j >= i)
{
printf("\nThe Largest is %d", j);
} /*end if*/
//3rd number
if (k < j & j<= i)
{
printf("\nThe smallest is %d", k);
} /*end if*/
if (k > j & j >= i)
{
printf("\nThe Largest is %d", k);
} /*end if*/
//chk and display even and odd no
//first no.
if ((i%2) == 0)
{
printf("\n \n%d is even", i);
}
else
{
printf("\n \n%d is odd", i);
}
//2nd no.
if (j%2 == 0)
{
printf("\n \n%d is even", j);
}
else
{
printf("\n \n%d is odd", j);
}
//3rd no.
if (k%2 == 0)
{
printf("\n \n%d is even", k);
}
else
{
printf("\n \n%d is odd", k);
}
//for disabling automatic close off of program after execution of program
_getch();
}//end
Posted by Unknown
Tag :
& Smallest,
Arithmetic,
C,
c++,
code,
coding,
even,
Largest,
Numbers,
odd,
Operators,
program,
project,
project c++(basic),
Value
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
coding for reversing a number and then printing it in c
#include<stdio.h>
#include<conio.h>
void main(){
int a;
int b;
printf("Enter the number You want to reverse: ");
scanf("%d", &a);
printf("\nNumber you Entered is: ");
b = printf("%d", a);
printf("\nIts reverse is: ");
for (int i = 0; i < b; i++){
printf("%d", a%10);
a /= 10;
}
_getch();
}
#include<conio.h>
void main(){
int a;
int b;
printf("Enter the number You want to reverse: ");
scanf("%d", &a);
printf("\nNumber you Entered is: ");
b = printf("%d", a);
printf("\nIts reverse is: ");
for (int i = 0; i < b; i++){
printf("%d", a%10);
a /= 10;
}
_getch();
}
Coding program to print right triangle through odd number in C through asteriks.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
for(int d=1;d<=5;d++){
do {
printf("enter a odd no");
scanf("%d",&a);
}
while(a%2!=1);
int b;
for(b=0;b<a;b++)
{
for(int c=0;c<=b;c++)
printf("*");
printf("\n");
}}
_getch();
}
#include<conio.h>
void main()
{
int a;
for(int d=1;d<=5;d++){
do {
printf("enter a odd no");
scanf("%d",&a);
}
while(a%2!=1);
int b;
for(b=0;b<a;b++)
{
for(int c=0;c<=b;c++)
printf("*");
printf("\n");
}}
_getch();
}
Posted by Unknown
Program, code to multiply 2 matrix using 1-d array in c++
#include<iostream>
#include<conio.h>
using namespace std;
//Entr order of matrixes
void order(int &matrix1_rows, int &matrix1_colomn, int &matrix2_rows,int &matrix2_colomn) {
cout << "Enter no of rows of 1st Matrix: ";
cin >> matrix1_rows;
cout << "Enter no of colomn of 1st Matrix: ";
cin >> matrix1_colomn;
cout << endl;
cout << "Enter no of rows of 2nd Matrix: ";
cin >> matrix2_rows;
cout << "Enter no of colomn of 2nd Matrix: ";
cin >> matrix2_colomn;
cout << endl;
}
//chk if multiplication is possible
int chk(int matrix1_rows,int matrix1_colomn, int matrix2_rows, int matrix2_colomn){
if(matrix1_colomn == matrix2_rows){
cout << "Multiplication possible" << endl;
return 1;
} else {
cout << "Multiplication not possible" << endl << "Multiply any other Matrix" << "\n\n";
}
return 0;
}
void main() {
//variable for Matrix1
int matrix1_rows = 0;
int matrix1_colomn = 0;
int matrix1[100];
int matrix1_max = 0;
//variable for Matrix2
int matrix2_rows = 0;
int matrix2_colomn = 0;
int matrix2[100];
int matrix2_max = 0;
//variable used to count the matrix element in any loop
int displayCounter1 = 0;
int displayCounter2 = 0;
//variable for result matrix
int result_matrix[100];
int result_matrix_rows = 0;
int result_matrix_colomn = 0;
int result_max;
//variable used for calculation of result
int resultCounter = 0;
int x = 0;
int y = 0;
int z = 0;
//start
cout << "Program to multiply two matrix" << endl << endl;
//retaking user input if multiplication is not possible
int a = 0;
do{
order(matrix1_rows, matrix1_colomn, matrix2_rows, matrix2_colomn);
a = chk(matrix1_rows, matrix1_colomn, matrix2_rows, matrix2_colomn);
} while(a !=1);
//Intializing data if multiplication is possible
matrix1_max = matrix1_rows*matrix1_colomn;
matrix2_max = matrix2_rows*matrix2_colomn;
//data entry matrix1
for(int i=0; i < matrix1_max; i++){
cout << "Enter" << "Element NO." << i+1 << " of Matrix1: ";
cin >> matrix1[i];
}
cout << endl;
//data entry matrix2
for(int i=0; i < matrix2_max; i++){
cout << "Enter" << "Element NO." << i+1 << " of Matrix2: ";
cin >> matrix2[i];
}
//matrix1 display
cout << "Matrix-1" << endl;
while(displayCounter1< matrix1_max){
cout << "|\t";
for(int i =0; i<matrix1_colomn; i++){
cout << matrix1[displayCounter1] << "\t";
displayCounter1++;
}
cout << '|' << endl;
}
//print multiplication sybmbol between middle
cout << endl << "\tX" << endl;
//matrix2 display
cout << endl << "Matrix-2" << endl;
while(displayCounter2< matrix2_max){
cout << "|\t";
for(int i =0; i<matrix2_colomn; i++){
cout << matrix2[displayCounter2] << "\t";
displayCounter2++;
}
cout << '|' << endl;
}
//intilizing result matrix
result_matrix_rows = matrix1_rows;
result_matrix_colomn = matrix2_colomn;
result_max = result_matrix_rows*result_matrix_colomn;
resultCounter = 0;
//intializing array 0 value
while(resultCounter < result_max){
result_matrix[resultCounter] = 0;
resultCounter++;
}
resultCounter = 0;
//The main multiplication
for(int i = 0; i < matrix1_rows; i++){
for(int j = 0; j <= matrix2_colomn-1; j++){
for(int k = 0; k < matrix1_colomn; k++){
result_matrix[resultCounter] += ((matrix1[k+x]) * (matrix2[z+y]));
y += matrix2_colomn;
}
z++;
y = 0;
resultCounter++;
}
x += matrix1_colomn;
z = 0;
}
//diplaying result
resultCounter = 0;
cout << endl << "Result";
while(resultCounter < result_max){
cout << "|\t";
for(int i =0; i<result_matrix_colomn; i++){
cout << result_matrix[resultCounter] << "\t";
resultCounter++;
}
cout << '|' << endl;
}
_getch();
}
#include<conio.h>
using namespace std;
//Entr order of matrixes
void order(int &matrix1_rows, int &matrix1_colomn, int &matrix2_rows,int &matrix2_colomn) {
cout << "Enter no of rows of 1st Matrix: ";
cin >> matrix1_rows;
cout << "Enter no of colomn of 1st Matrix: ";
cin >> matrix1_colomn;
cout << endl;
cout << "Enter no of rows of 2nd Matrix: ";
cin >> matrix2_rows;
cout << "Enter no of colomn of 2nd Matrix: ";
cin >> matrix2_colomn;
cout << endl;
}
//chk if multiplication is possible
int chk(int matrix1_rows,int matrix1_colomn, int matrix2_rows, int matrix2_colomn){
if(matrix1_colomn == matrix2_rows){
cout << "Multiplication possible" << endl;
return 1;
} else {
cout << "Multiplication not possible" << endl << "Multiply any other Matrix" << "\n\n";
}
return 0;
}
void main() {
//variable for Matrix1
int matrix1_rows = 0;
int matrix1_colomn = 0;
int matrix1[100];
int matrix1_max = 0;
//variable for Matrix2
int matrix2_rows = 0;
int matrix2_colomn = 0;
int matrix2[100];
int matrix2_max = 0;
//variable used to count the matrix element in any loop
int displayCounter1 = 0;
int displayCounter2 = 0;
//variable for result matrix
int result_matrix[100];
int result_matrix_rows = 0;
int result_matrix_colomn = 0;
int result_max;
//variable used for calculation of result
int resultCounter = 0;
int x = 0;
int y = 0;
int z = 0;
//start
cout << "Program to multiply two matrix" << endl << endl;
//retaking user input if multiplication is not possible
int a = 0;
do{
order(matrix1_rows, matrix1_colomn, matrix2_rows, matrix2_colomn);
a = chk(matrix1_rows, matrix1_colomn, matrix2_rows, matrix2_colomn);
} while(a !=1);
//Intializing data if multiplication is possible
matrix1_max = matrix1_rows*matrix1_colomn;
matrix2_max = matrix2_rows*matrix2_colomn;
//data entry matrix1
for(int i=0; i < matrix1_max; i++){
cout << "Enter" << "Element NO." << i+1 << " of Matrix1: ";
cin >> matrix1[i];
}
cout << endl;
//data entry matrix2
for(int i=0; i < matrix2_max; i++){
cout << "Enter" << "Element NO." << i+1 << " of Matrix2: ";
cin >> matrix2[i];
}
//matrix1 display
cout << "Matrix-1" << endl;
while(displayCounter1< matrix1_max){
cout << "|\t";
for(int i =0; i<matrix1_colomn; i++){
cout << matrix1[displayCounter1] << "\t";
displayCounter1++;
}
cout << '|' << endl;
}
//print multiplication sybmbol between middle
cout << endl << "\tX" << endl;
//matrix2 display
cout << endl << "Matrix-2" << endl;
while(displayCounter2< matrix2_max){
cout << "|\t";
for(int i =0; i<matrix2_colomn; i++){
cout << matrix2[displayCounter2] << "\t";
displayCounter2++;
}
cout << '|' << endl;
}
//intilizing result matrix
result_matrix_rows = matrix1_rows;
result_matrix_colomn = matrix2_colomn;
result_max = result_matrix_rows*result_matrix_colomn;
resultCounter = 0;
//intializing array 0 value
while(resultCounter < result_max){
result_matrix[resultCounter] = 0;
resultCounter++;
}
resultCounter = 0;
//The main multiplication
for(int i = 0; i < matrix1_rows; i++){
for(int j = 0; j <= matrix2_colomn-1; j++){
for(int k = 0; k < matrix1_colomn; k++){
result_matrix[resultCounter] += ((matrix1[k+x]) * (matrix2[z+y]));
y += matrix2_colomn;
}
z++;
y = 0;
resultCounter++;
}
x += matrix1_colomn;
z = 0;
}
//diplaying result
resultCounter = 0;
cout << endl << "Result";
while(resultCounter < result_max){
cout << "|\t";
for(int i =0; i<result_matrix_colomn; i++){
cout << result_matrix[resultCounter] << "\t";
resultCounter++;
}
cout << '|' << endl;
}
_getch();
}
Posted by Unknown
Coing to Generate prime Number in c
#include<stdio.h>;
#include<conio.h>;
//code by waqas javed //main
void main() {
//initializing vars
int i = 1; //prime number founded
int j = 2; //chk start point
int l = 1; //var factor chk var
int m = 0; //number of factor
int number = 0;
//user tell how many prime number to generate
printf("Enter how many prime number you want to generate: ");
scanf("%d", &number);
//printing first 20 prime no.
while (i <= number){
//chk for factors of number
while (j > l){
//if number is factor
if (j%l == 0){
m++;
l++;
}
//if not
else {
l++;
}
}
//chk and print if prime no.
if (j == l) {
//if prime
if (m == 1)
printf("%d.\t%d \n", i++, j);
j++;
l = 1;
m = 0;
}
//if not reinitializing value to chk nxt var
else {
j++;
l = 1;
m = 0;
}
}
_getch();
}
This code work as it starts from 2 and check whether it is a even number or not.. Its checking is designed like the program checks all the number below that number to be checked that whether they are its factor are not... Then the program check how many factor the number has if they are 2 then it take it as prime number and print it..
For example: Program is checking 9 is a prime no or not. it will check all number below it that is 1,2,3,4,5,6,7,8 now the we have 1 and 3 as its factor and 9 itself also so total factor exceeded 2 and the number is not prime...... similarly it will keep checking all number in increasing order starting from 1 until the total prime number too be founded are found.
#include<conio.h>;
//code by waqas javed //main
void main() {
//initializing vars
int i = 1; //prime number founded
int j = 2; //chk start point
int l = 1; //var factor chk var
int m = 0; //number of factor
int number = 0;
//user tell how many prime number to generate
printf("Enter how many prime number you want to generate: ");
scanf("%d", &number);
//printing first 20 prime no.
while (i <= number){
//chk for factors of number
while (j > l){
//if number is factor
if (j%l == 0){
m++;
l++;
}
//if not
else {
l++;
}
}
//chk and print if prime no.
if (j == l) {
//if prime
if (m == 1)
printf("%d.\t%d \n", i++, j);
j++;
l = 1;
m = 0;
}
//if not reinitializing value to chk nxt var
else {
j++;
l = 1;
m = 0;
}
}
_getch();
}
This code work as it starts from 2 and check whether it is a even number or not.. Its checking is designed like the program checks all the number below that number to be checked that whether they are its factor are not... Then the program check how many factor the number has if they are 2 then it take it as prime number and print it..
For example: Program is checking 9 is a prime no or not. it will check all number below it that is 1,2,3,4,5,6,7,8 now the we have 1 and 3 as its factor and 9 itself also so total factor exceeded 2 and the number is not prime...... similarly it will keep checking all number in increasing order starting from 1 until the total prime number too be founded are found.
Posted by Unknown
Fundamental data types, variable and variable scope.
VARIABLE
When we want to use any value or data in our program we want to save it in our memory so that it can be used again and again. For that purpose we store it in a variable. Variable is the actually the address of a particular memory where we can save our data. two location could not have same address that's why we could not create two variable with same name.
We can store create different types of variable depending upon the type of data we want to save. If we want to save a digit we will create a integer var, if we want to store a character we will create a character type var.
A variable must be declared before it could be used in our program. It could be declared by defining its type and its name its value or data is not necessary.
type identifier = initial_value ;(int is type in below example, identifier is name and initial value may or may not be assigned as required)
Line 1 is declaring a integer type variable with name a and it have no value initially.
Line 2 is declaring a integer type variable with name b which have value 3 so b =3.
Line 3 is declaring a integer type two variable with name a and b. we could declare multiple var in single type to reduce our space.
DATA TYPES
Scope of variables
in line 2 we created an integer a which is global variable it scope is around the whole program any where it could be used or modified as desired. No other function could have var with name (a) because (a) is already declared at every place of function.
in line 5 the var (b) and (c) are the local copies inside main. these variable could be used every where in main function but not outside.
in line 9 we have created var (b) and (d) which are the local copies of myFuntion could only be used in this function. but not outside.
In the program you could see that we have created (b) in main and also in myFunction both are the local copies so we can create a var with same name outside the scope of it.
we can not create var (a) any where in our program because its scope is all over the program and vars with same name could not be created within its own scope.
When we want to use any value or data in our program we want to save it in our memory so that it can be used again and again. For that purpose we store it in a variable. Variable is the actually the address of a particular memory where we can save our data. two location could not have same address that's why we could not create two variable with same name.
We can store create different types of variable depending upon the type of data we want to save. If we want to save a digit we will create a integer var, if we want to store a character we will create a character type var.
A variable must be declared before it could be used in our program. It could be declared by defining its type and its name its value or data is not necessary.
type identifier = initial_value ;(int is type in below example, identifier is name and initial value may or may not be assigned as required)
1. int a;
2. int b = 3;
3. int c,d;
Line 1 is declaring a integer type variable with name a and it have no value initially.
Line 2 is declaring a integer type variable with name b which have value 3 so b =3.
Line 3 is declaring a integer type two variable with name a and b. we could declare multiple var in single type to reduce our space.
DATA TYPES
NAME
|
Description
|
Size
|
Range
|
char
|
Character or small
integer.
|
1byte
|
signed: -128 to 127
unsigned: 0 to 255
|
short int
(short)
|
Short Integer.
|
2bytes
|
signed: -32768 to 32767
unsigned: 0 to 65535
|
int
|
Integer.
|
4bytes
|
signed: -2147483648 to
2147483647
unsigned: 0 to 4294967295
|
long int
(long)
|
Long integer.
|
4bytes
|
signed: -2147483648 to
2147483647
unsigned: 0 to 4294967295
|
bool
|
Boolean value. It can take one of two
values: true or false.
|
1byte
|
true or false
|
float
|
Floating point number.
|
4bytes
|
+/- 3.4e +/- 38 (~7 digits)
|
double
|
Double precision floating point number.
|
8bytes
|
+/- 1.7e +/- 308 (~15 digits)
|
long double
|
Long double precision floating point
number.
|
. 8bytes
|
+/- 1.7e +/- 308 (~15 digits)
|
wchar_t
|
Wide character.
|
2 or 4 bytes
|
_ 1 wide character
|
Scope of variables
1. #include <iostream>
2.. int a; //global variable
3.
4. void
main () {
5. int b,c; //local variable
6. }
7
8. void myFunction () {
9. int b,d; //local variable
10. }
in line 2 we created an integer a which is global variable it scope is around the whole program any where it could be used or modified as desired. No other function could have var with name (a) because (a) is already declared at every place of function.
in line 5 the var (b) and (c) are the local copies inside main. these variable could be used every where in main function but not outside.
in line 9 we have created var (b) and (d) which are the local copies of myFuntion could only be used in this function. but not outside.
In the program you could see that we have created (b) in main and also in myFunction both are the local copies so we can create a var with same name outside the scope of it.
we can not create var (a) any where in our program because its scope is all over the program and vars with same name could not be created within its own scope.
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.
Difference between post increment, decrement and pre increment,decrement
Most of the new programmers are stuck between post and pre increment.
What is the difference between ++a and a++. This post will clear your confusion.
++a is a pre increment type in which first of all 1 is added in a and then its value is considered in expression. where ever in pre increment type that is a++ first of all a value is considered for expression than 1 is added to a.
Did'nt Understand?? this example will make the idea clear.
if a = 3;
b = ++a; (In this case there is pre-increment a will increase and then its value is returned to b so b will be 4 and a will also become 4)
if a = 3;
b = --a; (In this case there is pre-decrement a will first decrease and then its value is returned to b. so b will be equal to 2 and a also will be 2)
if a = 3;
b = a++ (In this case there is post-increment a value will be returned to b and then a will increase by 1 that is b will be 3 and a will become 4)
if a = 3;
b = a-- (In this case there is post-decrement a value will be returned to b and then a will decrease by 1 that is b will be 3 and a will become 2)
What is the difference between ++a and a++. This post will clear your confusion.
++a is a pre increment type in which first of all 1 is added in a and then its value is considered in expression. where ever in pre increment type that is a++ first of all a value is considered for expression than 1 is added to a.
Did'nt Understand?? this example will make the idea clear.
if a = 3;
b = ++a; (In this case there is pre-increment a will increase and then its value is returned to b so b will be 4 and a will also become 4)
if a = 3;
b = --a; (In this case there is pre-decrement a will first decrease and then its value is returned to b. so b will be equal to 2 and a also will be 2)
if a = 3;
b = a++ (In this case there is post-increment a value will be returned to b and then a will increase by 1 that is b will be 3 and a will become 4)
if a = 3;
b = a-- (In this case there is post-decrement a value will be returned to b and then a will decrease by 1 that is b will be 3 and a will become 2)
Sunday, 12 October 2014
Posted by Unknown
What is Data hierarchy
Data hierarchy
Data hierarchy is the step by step representation of complex
data in different level starting from basic bits to highest level files.
We will discuss in this post how this hierarchy is formed.
Suppose we have a excel
file in which there are the names of some students and their marks in a subject.
The file is the highest level of data-hierarchy . Next to it
is the record a person name together with its marks is referred to as records.
Inside record the name of the student will be a field. A field is combination
of characters. An individual character will then be represented by a byte. That
byte will be combination of a bit. The picture below will make the idea clear