Filing a name and searching it again to match from file in C.
This code demonstrate the filing part of C language. This program is built to save name of users in a file on hard disk you can also retrieve that file from hard disk using this program and could also search how many times a name is found in a file.
The program also uses some basic function of C++ that are just for convenience you can replace them by C language
printf("some text"); is used as cout << "some text";
scanf(&d) id used as cin >> d;
so dont worry if you need code in C only you could replace it
The special data type FILE is used used to create stream for input or output data on hard disk.
in fopen function a mean file is opened in append mode, r mean read mode, w mean write mode.
system("cls"); is iostream part so if you want avoid C++ you could simply delete it that will not effect the code. this function just delete all data from console and clear it.
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
void main() {
FILE* myFile;
FILE* readFile;
char name[20];
int con = 1;
char temp;
while (true){
myFile = fopen("names.txt", "a");
cout << "Press 1 to Enter name..2 to search name.. 3 to display file: ";
cin >> con;
system("cls");
//enter name
if (con == 1){
cout << "Enter A name: ";
cin >> name;
fprintf(myFile, "%s", name);
fprintf(myFile, "%c", '-');
cout << "\nNAME ENTERED\n";
}
fclose(myFile);
//search in file
int counter = 0;
readFile = fopen("names.txt", "r");
if (con == 2){
cout << "Enter a name to search:";
cin >> name;
int matched = 0;
int chars = strlen(name);
while (fscanf(readFile, "%c", &temp) == 1){
if(temp!= '-'){
if(name[matched] == temp){
matched++;
} else{
matched = 0;
}
} else if(matched == chars){
counter++;
matched = 0;
} else{
matched = 0;
}
}
}
if(counter > 0){
cout << "\n\aName is found in file " << counter << " times.\n";
} else if(con == 2) {
cout << "Name is not in file...\n";
}
if(con == 3){
//display file
fclose(readFile);
fopen("names.txt", "r");
cout << "Display File text:\n";
while (fscanf(readFile, "%c", &temp)== 1){
if(temp != '-'){
printf("%c", temp);
} else {
printf("\n");
}
}
}
printf("\n");
}
_getch();
}
The program also uses some basic function of C++ that are just for convenience you can replace them by C language
printf("some text"); is used as cout << "some text";
scanf(&d) id used as cin >> d;
so dont worry if you need code in C only you could replace it
The special data type FILE is used used to create stream for input or output data on hard disk.
in fopen function a mean file is opened in append mode, r mean read mode, w mean write mode.
system("cls"); is iostream part so if you want avoid C++ you could simply delete it that will not effect the code. this function just delete all data from console and clear it.
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
void main() {
FILE* myFile;
FILE* readFile;
char name[20];
int con = 1;
char temp;
while (true){
myFile = fopen("names.txt", "a");
cout << "Press 1 to Enter name..2 to search name.. 3 to display file: ";
cin >> con;
system("cls");
//enter name
if (con == 1){
cout << "Enter A name: ";
cin >> name;
fprintf(myFile, "%s", name);
fprintf(myFile, "%c", '-');
cout << "\nNAME ENTERED\n";
}
fclose(myFile);
//search in file
int counter = 0;
readFile = fopen("names.txt", "r");
if (con == 2){
cout << "Enter a name to search:";
cin >> name;
int matched = 0;
int chars = strlen(name);
while (fscanf(readFile, "%c", &temp) == 1){
if(temp!= '-'){
if(name[matched] == temp){
matched++;
} else{
matched = 0;
}
} else if(matched == chars){
counter++;
matched = 0;
} else{
matched = 0;
}
}
}
if(counter > 0){
cout << "\n\aName is found in file " << counter << " times.\n";
} else if(con == 2) {
cout << "Name is not in file...\n";
}
if(con == 3){
//display file
fclose(readFile);
fopen("names.txt", "r");
cout << "Display File text:\n";
while (fscanf(readFile, "%c", &temp)== 1){
if(temp != '-'){
printf("%c", temp);
} else {
printf("\n");
}
}
}
printf("\n");
}
_getch();
}
Saturday, 17 January 2015
Posted by Unknown
Tic tac coding/program in C language.(without using array)
This coding demonstrate the usage of only basic data type and basic command(if, for, while functions, etc) to make a practical game that could be run. The code just uses the basic commands. This program also demonstrate the usage of function.
Comments are added to let you understand the program still if you have any further query you can comment below. Thanks hope this will help you.
#include<stdio.h>
#include<conio.h>
//Player turns
int turn(int &user, int &t, int &a1, int &a2, int &a3, int &a4, int &a5, int &a6, int &a7, int &a8, int &a9) {
int pos;
if(user%2 == 1){printf("Player1\n");}
if(user%2 == 0){printf("Player2\n");}
printf("Enter the postion You Want to selct: ");
scanf_s("%d", &pos);
if(pos == 1 && a1 == 0){//chk avalability of player selected box
if(user%2 == 1){//if player1 chosed box
a1 = 1; //box = 1 for p1 chek the box
user++; //chng user for next turn
t++; //turn count
} else if (user%2 == 0) {//if player 2 chosed box
a1 = 2; //box = 2 for p2 checked the box
user++;
t++;
}
} else if (pos == 1 && a1 != 0){//if box is already filled
printf("Already checked!\n");
}
//same structure for next 9 boxes
if(pos == 2 && a2 == 0){
if(user%2 == 1){
a2 = 1;
user++;
t++;
} else if (user%2 == 0) {
a2 = 2;
user++;
t++;
}
} else if (pos == 2 && a2 != 0){
printf("Already checked!\n");
}
if(pos == 3 && a3 == 0){
if(user%2 == 1){
a3 = 1;
user++;
t++;
} else if (user%2 == 0) {
a3 = 2;
user++;
t++;
}
} else if (pos == 3 && a3 != 0){
printf("Already checked!\n");
}
if(pos == 4 && a4 == 0){
if(user%2 == 1){
a4 = 1;
user++;
t++;
} else if (user%2 == 0) {
a4 = 2;
user++;
t++;
}
} else if (pos == 4 && a4 != 0){
printf("Already checked!\n");
}
if(pos == 5 && a5 == 0){
if(user%2 == 1){
a5 = 1;
user++;
t++;
} else if (user%2 == 0) {
a5 = 2;
user++;
t++;
}
} else if (pos == 5 && a5 != 0){
printf("Already checked!\n");
}
if(pos == 6 && a6 == 0){
if(user%2 == 1){
a6 = 1;
t++;
user++;
} else if (user%2 == 0) {
a6 = 2;
user++;
t++;
}
} else if (pos == 6 && a6 != 0){
printf("Already checked!\n");
}
if(pos == 7 && a7 == 0){
if(user%2 == 1){
a7 = 1;
user++;
t++;
} else if (user%2 == 0) {
a7 = 2;
user++;
t++;
}
} else if (pos == 7 && a7 != 0){
printf("Already checked!\n");
}
if(pos == 8 && a8 == 0){
if(user%2 == 1){
a8 = 1;
user++;
t++;
} else if (user%2 == 0) {
a8 = 2;
user++;
t++;
}
} else if (pos == 8 && a8 != 0){
printf("Already checked!\n");
}
if(pos == 9 && a9 == 0){
if(user%2 == 1){
a9 = 1;
user++;
t++;
} else if (user%2 == 0) {
a9 = 2;
user++;
t++;
}
} else if (pos == 9 && a9 != 0){
printf("Already checked!\n");
}
return 0;
}
//check if any player had made the combination
int win_cheker(int &a1, int &a2, int &a3, int &a4, int &a5, int &a6, int &a7, int &a8, int &a9){
//chk first row for p1 else p2
if(a1 == a2 && a2 == a3 && a3 == 1){
printf("winning combination\n");
printf("\n%d %d %d\n* * *\n* * *\n ", a1, a2, a3);
return 1;
} else if (a1 == a2 && a2 == a3 && a3 == 2) {
printf("winning combination\n");
printf("\n%d %d %d\n* * *\n* * *\n ", a1, a2, a3);
return 2;
}
//chk 2nd row for p1 else p2
if(a4 == a5 && a5 == a6 && a6 == 1){
printf("winning combination\n");
printf("\n* * *\n%d %d %d\n* * *\n ", a4, a5, a6);
return 1;
} else if(a4 == a5 && a5 == a6 && a6 == 2) {
printf("winning combination\n");
printf("\n* * *\n%d %d %d\n* * *\n ", a4, a5, a6);
return 2;
}
//chk 3rd row for p1 else p2
if(a7 == a8 && a8 == a9 && a9 == 1){
printf("winning combination\n");
printf("\n|* * *\n|* * *\n|%d %d %d\n ", a7, a8, a9);
return 1;
} else if(a7 == a8 && a8 == a9 && a9 == 2) {
printf("winning combination\n");
printf("\n* * *\n* * *\n%d %d %d\n ", a7, a8, a9);
return 2;
}
//chk 1st column for p1 else p2
if(a1 == a4 && a4 == a7 && a7 == 1){
printf("winning combination\n");
printf("\n%d * *\n%d * *\n%d * *\n ", a1, a4, a7);
return 1;
} else if(a1 == a4 && a4 == a7 && a7 == 2) {
printf("winning combination\n");
printf("\n%d * *\n%d * *\n%d * *\n ", a1, a4, a7);
return 2;
}
//chk 2nd column for p1 else p2
if(a2 == a5 && a5 == a8 && a8 == 1){
printf("winning combination\n");
printf("\n* %d *\n* %d *\n* %d *\n ", a2, a5, a8);
return 1;
} else if(a2 == a5 && a5 == a8 && a8 == 2) {
printf("winning combination\n");
printf("\n* %d *\n* %d *\n* %d *\n ", a2, a5, a8);
return 2;
}
//chk 3st column for p1 else p2
if(a3 == a6 && a6 == a9 && a9 == 1){
printf("winning combination\n");
printf("\n* * %d\n* * %d\n* * %d\n", a3, a6, a9);
return 1;
} else if(a3 == a6 && a6 == a9 && a9 == 2) {
printf("winning combination\n");
printf("\n* * %d\n* * %d\n* * %d\n", a3, a6, a9);
return 2;
}
//chk diagnol-1 for p1 else p2
if(a1 == a5 && a5 == a9 && a9 == 1){
printf("winning combination\n");
printf("\n%d * *\n* %d *\n* * %d\n", a1, a5, a9);
return 1;
} else if(a1 == a5 && a5 == a9 && a9 == 2) {
printf("winning combination\n");
printf("\n%d * *\n* %d *\n* * %d\n", a1, a5, a9);
return 2;
}
//chk diagnol-2 for p1 else p2
if(a3 == a5 && a5 == a7 && a7 == 1){
printf("winning combination\n");
printf("\n* * %d\n* %d *\n%d * *\n", a3, a5, a7);
return 1;
} else if(a3 == a5 && a5 == a7 && a7 == 2) {
printf("winning combination\n");
printf("\n* * %d\n* %d *\n%d * *\n", a3, a5, a7);
return 2;
}
printf("\n");
return 0;
}
//the main structure of progrmam
void main() {
int chk = 0; //turn to 1 if p1 wins. or turns to 2 if p2 wins
int user = 1; /*user determination by remainder method, incrementing user value after
every turn, using user%2 formula 0 for p-1, 1 for player-2*/
int t = 0; //count turns made
int a1 = 0; //a1-a9 position of box. 0 for empty box, 1 for p1 checked, 2 for p2 checked
int a2 = 0;
int a3 = 0;
int a4 = 0;
int a5 = 0;
int a6 = 0;
int a7 = 0;
int a8 = 0;
int a9 = 0;
printf("\n%d %d %d\n%d %d %d\n%d %d %d\n\n", a1, a2, a3, a4, a5, a6, a7, a8, a9);
while(chk == 0 && t < 9){
//call turn function
turn(user, t, a1, a2, a3, a4, a5, a6, a7, a8, a9);
//show the grid
printf("\n%d %d %d\n%d %d %d\n%d %d %d\n ", a1, a2, a3, a4, a5, a6, a7, a8, a9);
//chk for win combination
chk = win_cheker(a1, a2, a3, a4, a5, a6, a7, a8, a9);
}
if(chk == 1) {
printf("player1 you won");
} else if (chk == 2) {
printf("player2 you won");
} else {
printf("Match Tie");
}
_getch();
}
Comments are added to let you understand the program still if you have any further query you can comment below. Thanks hope this will help you.
#include<stdio.h>
#include<conio.h>
//Player turns
int turn(int &user, int &t, int &a1, int &a2, int &a3, int &a4, int &a5, int &a6, int &a7, int &a8, int &a9) {
int pos;
if(user%2 == 1){printf("Player1\n");}
if(user%2 == 0){printf("Player2\n");}
printf("Enter the postion You Want to selct: ");
scanf_s("%d", &pos);
if(pos == 1 && a1 == 0){//chk avalability of player selected box
if(user%2 == 1){//if player1 chosed box
a1 = 1; //box = 1 for p1 chek the box
user++; //chng user for next turn
t++; //turn count
} else if (user%2 == 0) {//if player 2 chosed box
a1 = 2; //box = 2 for p2 checked the box
user++;
t++;
}
} else if (pos == 1 && a1 != 0){//if box is already filled
printf("Already checked!\n");
}
//same structure for next 9 boxes
if(pos == 2 && a2 == 0){
if(user%2 == 1){
a2 = 1;
user++;
t++;
} else if (user%2 == 0) {
a2 = 2;
user++;
t++;
}
} else if (pos == 2 && a2 != 0){
printf("Already checked!\n");
}
if(pos == 3 && a3 == 0){
if(user%2 == 1){
a3 = 1;
user++;
t++;
} else if (user%2 == 0) {
a3 = 2;
user++;
t++;
}
} else if (pos == 3 && a3 != 0){
printf("Already checked!\n");
}
if(pos == 4 && a4 == 0){
if(user%2 == 1){
a4 = 1;
user++;
t++;
} else if (user%2 == 0) {
a4 = 2;
user++;
t++;
}
} else if (pos == 4 && a4 != 0){
printf("Already checked!\n");
}
if(pos == 5 && a5 == 0){
if(user%2 == 1){
a5 = 1;
user++;
t++;
} else if (user%2 == 0) {
a5 = 2;
user++;
t++;
}
} else if (pos == 5 && a5 != 0){
printf("Already checked!\n");
}
if(pos == 6 && a6 == 0){
if(user%2 == 1){
a6 = 1;
t++;
user++;
} else if (user%2 == 0) {
a6 = 2;
user++;
t++;
}
} else if (pos == 6 && a6 != 0){
printf("Already checked!\n");
}
if(pos == 7 && a7 == 0){
if(user%2 == 1){
a7 = 1;
user++;
t++;
} else if (user%2 == 0) {
a7 = 2;
user++;
t++;
}
} else if (pos == 7 && a7 != 0){
printf("Already checked!\n");
}
if(pos == 8 && a8 == 0){
if(user%2 == 1){
a8 = 1;
user++;
t++;
} else if (user%2 == 0) {
a8 = 2;
user++;
t++;
}
} else if (pos == 8 && a8 != 0){
printf("Already checked!\n");
}
if(pos == 9 && a9 == 0){
if(user%2 == 1){
a9 = 1;
user++;
t++;
} else if (user%2 == 0) {
a9 = 2;
user++;
t++;
}
} else if (pos == 9 && a9 != 0){
printf("Already checked!\n");
}
return 0;
}
//check if any player had made the combination
int win_cheker(int &a1, int &a2, int &a3, int &a4, int &a5, int &a6, int &a7, int &a8, int &a9){
//chk first row for p1 else p2
if(a1 == a2 && a2 == a3 && a3 == 1){
printf("winning combination\n");
printf("\n%d %d %d\n* * *\n* * *\n ", a1, a2, a3);
return 1;
} else if (a1 == a2 && a2 == a3 && a3 == 2) {
printf("winning combination\n");
printf("\n%d %d %d\n* * *\n* * *\n ", a1, a2, a3);
return 2;
}
//chk 2nd row for p1 else p2
if(a4 == a5 && a5 == a6 && a6 == 1){
printf("winning combination\n");
printf("\n* * *\n%d %d %d\n* * *\n ", a4, a5, a6);
return 1;
} else if(a4 == a5 && a5 == a6 && a6 == 2) {
printf("winning combination\n");
printf("\n* * *\n%d %d %d\n* * *\n ", a4, a5, a6);
return 2;
}
//chk 3rd row for p1 else p2
if(a7 == a8 && a8 == a9 && a9 == 1){
printf("winning combination\n");
printf("\n|* * *\n|* * *\n|%d %d %d\n ", a7, a8, a9);
return 1;
} else if(a7 == a8 && a8 == a9 && a9 == 2) {
printf("winning combination\n");
printf("\n* * *\n* * *\n%d %d %d\n ", a7, a8, a9);
return 2;
}
//chk 1st column for p1 else p2
if(a1 == a4 && a4 == a7 && a7 == 1){
printf("winning combination\n");
printf("\n%d * *\n%d * *\n%d * *\n ", a1, a4, a7);
return 1;
} else if(a1 == a4 && a4 == a7 && a7 == 2) {
printf("winning combination\n");
printf("\n%d * *\n%d * *\n%d * *\n ", a1, a4, a7);
return 2;
}
//chk 2nd column for p1 else p2
if(a2 == a5 && a5 == a8 && a8 == 1){
printf("winning combination\n");
printf("\n* %d *\n* %d *\n* %d *\n ", a2, a5, a8);
return 1;
} else if(a2 == a5 && a5 == a8 && a8 == 2) {
printf("winning combination\n");
printf("\n* %d *\n* %d *\n* %d *\n ", a2, a5, a8);
return 2;
}
//chk 3st column for p1 else p2
if(a3 == a6 && a6 == a9 && a9 == 1){
printf("winning combination\n");
printf("\n* * %d\n* * %d\n* * %d\n", a3, a6, a9);
return 1;
} else if(a3 == a6 && a6 == a9 && a9 == 2) {
printf("winning combination\n");
printf("\n* * %d\n* * %d\n* * %d\n", a3, a6, a9);
return 2;
}
//chk diagnol-1 for p1 else p2
if(a1 == a5 && a5 == a9 && a9 == 1){
printf("winning combination\n");
printf("\n%d * *\n* %d *\n* * %d\n", a1, a5, a9);
return 1;
} else if(a1 == a5 && a5 == a9 && a9 == 2) {
printf("winning combination\n");
printf("\n%d * *\n* %d *\n* * %d\n", a1, a5, a9);
return 2;
}
//chk diagnol-2 for p1 else p2
if(a3 == a5 && a5 == a7 && a7 == 1){
printf("winning combination\n");
printf("\n* * %d\n* %d *\n%d * *\n", a3, a5, a7);
return 1;
} else if(a3 == a5 && a5 == a7 && a7 == 2) {
printf("winning combination\n");
printf("\n* * %d\n* %d *\n%d * *\n", a3, a5, a7);
return 2;
}
printf("\n");
return 0;
}
//the main structure of progrmam
void main() {
int chk = 0; //turn to 1 if p1 wins. or turns to 2 if p2 wins
int user = 1; /*user determination by remainder method, incrementing user value after
every turn, using user%2 formula 0 for p-1, 1 for player-2*/
int t = 0; //count turns made
int a1 = 0; //a1-a9 position of box. 0 for empty box, 1 for p1 checked, 2 for p2 checked
int a2 = 0;
int a3 = 0;
int a4 = 0;
int a5 = 0;
int a6 = 0;
int a7 = 0;
int a8 = 0;
int a9 = 0;
printf("\n%d %d %d\n%d %d %d\n%d %d %d\n\n", a1, a2, a3, a4, a5, a6, a7, a8, a9);
while(chk == 0 && t < 9){
//call turn function
turn(user, t, a1, a2, a3, a4, a5, a6, a7, a8, a9);
//show the grid
printf("\n%d %d %d\n%d %d %d\n%d %d %d\n ", a1, a2, a3, a4, a5, a6, a7, a8, a9);
//chk for win combination
chk = win_cheker(a1, a2, a3, a4, a5, a6, a7, a8, a9);
}
if(chk == 1) {
printf("player1 you won");
} else if (chk == 2) {
printf("player2 you won");
} else {
printf("Match Tie");
}
_getch();
}
Count number of sunday on 1st of every month in C/C++ programing code
Problem: Write a code to calculte number of sunday on first of every month from 1st Jan 1901 to 31st Dec 2000 wherease the at 1st Jan 1900 the day is Monday.
The following code demonstrate the working of program. in this program there is use of nested loops 1st for total years thats is 1900 - 2000 inside this each year have 12 months, so inside year loop their is loop from 1 - 12 which is for months. inside this we have if structure which decides which month is this if that months have 31 days it assign z = 31 if that month have 30 days it assign z = 30; if it is February it checks whether it is a leap year or not and then assign respective value to z.
now inside this loop we have a counter day which is controlled through if to have value from 1-7 where 1 represent Monday , 2 Tuesday and so on in this manner 7 represent Sunday.
now we have a variable total Sunday which is initially 0 in final if structure there are 3 condition
1. day ==7 that is the day should be Sunday
2. z == 1 that is the date of month should be 1st
3. the year should be greater than 1900 as we have to count from 1901.
if these 3 condition satisfies then the counter for counting that is total Sunday increased by 1.
and after all loops successfully executed there is a statement to print the value in console .
Hope u have understood it very well for any question or ambiguity plz comment below.
#include<iostream>
#include<conio.h>
using namespace std;
void main(){
int x[3] = {7, 1, 1900};//first sunday
int y[3];
int z;
int day = 0;
int total_sunday = 0;
for (int i = 1900; i <= 2000; i++){
for (int j = 1; j <= 12; j++){
if (j == 1 || j == 3 || j == 5 || j == 7 || j == 8 || j == 12){
z = 31;
}
else if (j == 4 || j == 6 || j == 9 || j == 10 || j == 11) {
z = 30;
}
else if (j==2 && i%4 == 0)
{
z = 29;
}
else{
z = 28;
}
for (int k = 1; k <= z; k++){
y[0] = k;
y[1] = j;
y[2] = i;
day++;
if (day > 7){
day = 1;
}
if (day == 7 && y[0] == 1 && y[2] > 1900){
total_sunday++;
}
}
}
}
cout << "Total sunday on 1st of month during 1 jan 1901 to 31 dec 2000 are: " << total_sunday;
_getch();
}
The following code demonstrate the working of program. in this program there is use of nested loops 1st for total years thats is 1900 - 2000 inside this each year have 12 months, so inside year loop their is loop from 1 - 12 which is for months. inside this we have if structure which decides which month is this if that months have 31 days it assign z = 31 if that month have 30 days it assign z = 30; if it is February it checks whether it is a leap year or not and then assign respective value to z.
now inside this loop we have a counter day which is controlled through if to have value from 1-7 where 1 represent Monday , 2 Tuesday and so on in this manner 7 represent Sunday.
now we have a variable total Sunday which is initially 0 in final if structure there are 3 condition
1. day ==7 that is the day should be Sunday
2. z == 1 that is the date of month should be 1st
3. the year should be greater than 1900 as we have to count from 1901.
if these 3 condition satisfies then the counter for counting that is total Sunday increased by 1.
and after all loops successfully executed there is a statement to print the value in console .
Hope u have understood it very well for any question or ambiguity plz comment below.
#include<iostream>
#include<conio.h>
using namespace std;
void main(){
int x[3] = {7, 1, 1900};//first sunday
int y[3];
int z;
int day = 0;
int total_sunday = 0;
for (int i = 1900; i <= 2000; i++){
for (int j = 1; j <= 12; j++){
if (j == 1 || j == 3 || j == 5 || j == 7 || j == 8 || j == 12){
z = 31;
}
else if (j == 4 || j == 6 || j == 9 || j == 10 || j == 11) {
z = 30;
}
else if (j==2 && i%4 == 0)
{
z = 29;
}
else{
z = 28;
}
for (int k = 1; k <= z; k++){
y[0] = k;
y[1] = j;
y[2] = i;
day++;
if (day > 7){
day = 1;
}
if (day == 7 && y[0] == 1 && y[2] > 1900){
total_sunday++;
}
}
}
}
cout << "Total sunday on 1st of month during 1 jan 1901 to 31 dec 2000 are: " << total_sunday;
_getch();
}
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;
}