PropellerAds

Archive for December 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();

}
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;
}
}

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();
}

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;
}

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

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

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
Monday, 22 December 2014
Posted by Unknown

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();
}

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();
}

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();
}

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.

PropellerAds
PropellerAds

Popular Post

Powered by Blogger.

- Copyright © BSCS -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -