Nowadays, Excel has become an integral part of our day to day life.
Whether you are an analyst, sales executive, project manager or technical architect, you need excel to perform a variety of functions ranging from rudimentary calculation to sophisticated dashboards. Excel makes your life easier and lets you store and organize financial data securely.
Programming World by Arslan Malik
Tuesday, March 3, 2020
Monday, May 30, 2016
Wednesday, March 16, 2016
Stack Implementation using Linked List
Description:
We can implement stack using array as well as linked list. But array has fixed size therefore we prefer to use linked list because it does not have predefined size.
#include <iostream>
using namespace std;
struct Node{
int value;
Node *next;
};
int main(){
for( int i=0;i<=79;i++){
cout<<'*';
}
cout<<" Built & Designed by Arslan Malik Aareez"<<endl;
cout<<" www.CWorldbyAS.blogspot.com"<<endl;
cout<<" Facebook : www.facebook.com/arslan.ud.din"<<endl;
cout<<" Twitter : www.twitter.com/itsaareez"<<endl;
for( int i=0;i<=79;i++){
cout<<'*';
}
int menu;
int sel;
struct Node *p = new Node;
struct Node *head = new Node;
struct Node *r = new Node;
head->next = NULL;
head->value=0;
ask:
cout<<"Enter Your Choice\n"<<endl;
cout<<"1. Push"<<endl;
cout<<"2. Pop"<<endl;
cout<<"3. Print"<<endl;
cout<<"4. Exit"<<endl;
cin>>menu;
system("cls");
switch (menu){
case 1:
cout<<"Enter Value to Push : ";
cin>>p->value;
p->next = NULL;
system("cls");
head = p;
cout<<"Do you want to push another value?"<<endl;
cout<<"1. Yes"<<endl;
cout<<"2. No"<<endl;
cin>>sel;
system("cls");
while (sel!=2){
struct Node *q = new Node;
cout<<"\nEnter Value to Push : ";
cin>>q->value;
q->next=NULL;
p->next = q;
p = p->next;
system("cls");
cout<<"Do you want to push another value?"<<endl;
cout<<"1. Yes"<<endl;
cout<<"2. No"<<endl;
cin>>sel;
system("cls");
}
goto ask;
break;
case 2:
p = head;
if (p->next==NULL && p->value == 0){
cout<<"\nStack is Empty\n\n";
}
while (p->next!=NULL){
r = p;
p = p->next;
}
r->next = NULL;
p->value = 0;
goto ask;
break;
case 3:
system("cls");
p= head;
cout<<"Stack contains the following values "<<endl;
while (p->next!=NULL){
if (p->value==0)
{
cout<<" ";
}
else
{
cout<<" "<<p->value;
}
p=p->next;
}
cout<<" "<<p->value;
cout<<"\n";
goto ask;
break;
case 4:
return 0;
break;
}
}
We can implement stack using array as well as linked list. But array has fixed size therefore we prefer to use linked list because it does not have predefined size.
#include <iostream>
using namespace std;
struct Node{
int value;
Node *next;
};
int main(){
for( int i=0;i<=79;i++){
cout<<'*';
}
cout<<" Built & Designed by Arslan Malik Aareez"<<endl;
cout<<" www.CWorldbyAS.blogspot.com"<<endl;
cout<<" Facebook : www.facebook.com/arslan.ud.din"<<endl;
cout<<" Twitter : www.twitter.com/itsaareez"<<endl;
for( int i=0;i<=79;i++){
cout<<'*';
}
int menu;
int sel;
struct Node *p = new Node;
struct Node *head = new Node;
struct Node *r = new Node;
head->next = NULL;
head->value=0;
ask:
cout<<"Enter Your Choice\n"<<endl;
cout<<"1. Push"<<endl;
cout<<"2. Pop"<<endl;
cout<<"3. Print"<<endl;
cout<<"4. Exit"<<endl;
cin>>menu;
system("cls");
switch (menu){
case 1:
cout<<"Enter Value to Push : ";
cin>>p->value;
p->next = NULL;
system("cls");
head = p;
cout<<"Do you want to push another value?"<<endl;
cout<<"1. Yes"<<endl;
cout<<"2. No"<<endl;
cin>>sel;
system("cls");
while (sel!=2){
struct Node *q = new Node;
cout<<"\nEnter Value to Push : ";
cin>>q->value;
q->next=NULL;
p->next = q;
p = p->next;
system("cls");
cout<<"Do you want to push another value?"<<endl;
cout<<"1. Yes"<<endl;
cout<<"2. No"<<endl;
cin>>sel;
system("cls");
}
goto ask;
break;
case 2:
p = head;
if (p->next==NULL && p->value == 0){
cout<<"\nStack is Empty\n\n";
}
while (p->next!=NULL){
r = p;
p = p->next;
}
r->next = NULL;
p->value = 0;
goto ask;
break;
case 3:
system("cls");
p= head;
cout<<"Stack contains the following values "<<endl;
while (p->next!=NULL){
if (p->value==0)
{
cout<<" ";
}
else
{
cout<<" "<<p->value;
}
p=p->next;
}
cout<<" "<<p->value;
cout<<"\n";
goto ask;
break;
case 4:
return 0;
break;
}
}
Tips to generate a reasonable revenue from your website
We are living in the age of technology. Now a days we face technology everywhere. To earn money through internet is not a new thing. The people have been earning money on internet in different ways. The most easiest way that people think is earn money through website or mobile app by monetizing their app or website with ads by google or any other network.
Earning money is not much easy as some people think. It demands a lot of time and hard work with determination and devotion.
There is a question which arrives in mind whenever we talk about to earn through a website. Which type of websites can generate revenue? What are the companies who pay for displaying their ads?
First of all I would like to elaborate the key points which should be kept in mind while development of a website. Every website is not able to generate revenue. To generate revenue from website is a fun if your website has a large range of daily visitors. But if you have very small number of visitors, it can be a fatigue for you or just a source of disappointment.
The following points are essential while development of a website. If these points are followed, you are almost near to generate revenue through a website.
- First phase in development of website is idea for a website. You should have a mind blowing idea for making a website.
- Your website should be unique.
- An idea can change your life.
- While searching an idea it should kept in mind that your idea is feasible.
- Hire a great programmer to build your website or make a blog using different CMS like blogger.com, wordpress.com, webs.com etc.
- Your website template should be eye catching and attractive.
- Website design should be unique.
- Website design should be modern but simple.
- Simple is faster and complicated is slower.
- So choose a simple and user friendly design.
- A user should be able to use it easily.
- Your website should have interesting material on it.
- Material can be related to any category but it should be completely your own work.
- It should be informative and helpful for others.
- Visitors should love to spend time on your website.
- After getting a well developed and designed website, buy a good domain name and fast hosting service.
- Optimize your website for search engines (SEO)
- SEO is the only way to gain a lot of visitors daily
- The stronger the SEO of your website, the more will be the number of visitors visiting your website.
- Place ads on your website.
- Keep posting and updating your website or provide any services to your visitors.
- Avoid any type of copyright infringement.
In this way you will be owner of a well designed and well developed website and you will be able to generate revenue from it.
Friday, December 25, 2015
Parallel Computing
Parallel
computing is collection of processing elements that communicate and co-operate
to solve large problems fast. It is the use of two or more processors
(cores, computers) in combination to solve a single problem.
Examples of Parallel
System:
An example of parallel computing
would be two servers that share the workload of routing mail, managing
connections to an accounting system or database, solving a mathematical problem
etc. Supercomputers are usually placed in parallel system
architecture, Terminals connected to single server.
Advantages of Parallel System:
They provide concurrency, help in taking advantage
of non-local resources, It is cost saving.
It is overcoming memory constraints. It saves time
and money. It has global address space which provides a user-friendly programming
perspective to memory.
Disadvantages of Parallel System:
Primary
disadvantage is the lack of scalability between memory and CPUs.
Programmer
responsibility for synchronization constructs that ensure "correct"
access of global memory.
It becomes
increasingly difficult and expensive to design and produce shared memory
machines with ever increasing numbers of processors.
Types of Parallel Computing:
·
Data-parallel: Same operations on different data. It is also called SIMD.
·
SPMD: Same program, different data.
MIMD: Different programs, different data
_________________________________________________________________________________
Arslan ud Din Shafiq
COMSATS Institute of Information Technology
CS Department
Distributed Computing
A distributed
system is a software system in which
components located on networked computers communicate and coordinate their
actions by passing messages. The components interact with each other in order
to achieve a common goal.
Examples of
Distributed System:
World Wide Web, Sharing Clusters, Network of workstations,
Network of branch office computers, Network of embedded systems, New Cell
processor (PlayStation 3) etc are examples of distributed system.
Advantages of Distributed System:
Distributed Systems are
information sharing, resource sharing, extensible, shorter response time and
higher output, higher reliability, better flexibility’s in meeting user’s needs,
better price/performance ratio, scalability, transparency.
Disadvantages of Distributed System:
Distributed systems
have difficulties of developing distributed software, networking problem,
security problems, performance is less, openness, less reliability and less
fault tolerance.
Types of Distributed Computing:
·
Host-Based
Hierarchy
·
Decentralized
Stand-Alone System
·
Peer-to-Peer
LAN-based System
·
Hybrid
Enterprise-wide System
·
Client-Server
System
·
Internet-centric
Computing Systems
Goals of Distributed System:
In distributed system,
we work to achieve the following goals:
·
Transparency
·
Openness
·
Reliability
·
Performance
·
Scalability
_________________________________________________________________________________
Arslan ud Din Shafiq
COMSATS Institute of Information Technology
CS Department
Friday, April 17, 2015
Queues Implementation using Single Link List
Queues Implementation using Single Link List
#include <iostream>
using namespace std;
struct Node {
int value;
Node *next;
};
int main(){
int menu;
int sel;
int count=0;
struct Node *p = new Node;
struct Node *head = new Node;
for( int i=0;i<=79;i++){
cout<<'*';
}
cout<<" Built & Designed by Arslan Malik Aareez"<<endl;
cout<<" www.CWorldbyAS.blogspot.com"<<endl;
cout<<" Facebook : www.facebook.com/arslan.ud.din"<<endl;
cout<<" Twitter : www.twitter.com/itsaareez"<<endl;
for( int i=0;i<=79;i++){
cout<<'*';
}
ask:
cout<<"1. Insert\n2. Delete\n3. Print\n4. Exit"<<endl;
cin>>menu;
switch(menu){
case 1:
count++;
cout<<"Enter Value to insert : ";
cin>>p->value;
p->next = NULL;
head = p;
system("cls");
cout<<"Do you want to enter any other value?\n\n1. Yes\n2. No";
cin>>sel;
system("cls");
while (sel!=2){
struct Node *q = new Node;
count++;
cout<<"Enter Value to insert : ";
cin>>q->value;
q->next = NULL;
p->next = q;
p = p->next;
system("cls");
cout<<"Do you want to enter any other value?\n\n1. Yes\n2. No";
cin>>sel;
system("cls");
}
goto ask;
break;
case 2:
count--;
p = head;
head = p->next;
p->value = 0;
p->next = NULL;
system("cls");
goto ask;
break;
case 3:
system("cls");
p = head;
if (count==0){
system("cls");
cout<<"Queue is empty.";
}
else{
cout<<"Your queue has the following values"<<endl;
while (p->next!=NULL){
cout<<" "<<p->value;
p = p->next;
}
cout<<" "<<p->value;
}
cout<<"\n\n";
system("pause");
system("cls");
goto ask;
break;
case 4:
return 0;
break;
}
}
#include <iostream>
using namespace std;
struct Node {
int value;
Node *next;
};
int main(){
int menu;
int sel;
int count=0;
struct Node *p = new Node;
struct Node *head = new Node;
for( int i=0;i<=79;i++){
cout<<'*';
}
cout<<" Built & Designed by Arslan Malik Aareez"<<endl;
cout<<" www.CWorldbyAS.blogspot.com"<<endl;
cout<<" Facebook : www.facebook.com/arslan.ud.din"<<endl;
cout<<" Twitter : www.twitter.com/itsaareez"<<endl;
for( int i=0;i<=79;i++){
cout<<'*';
}
ask:
cout<<"1. Insert\n2. Delete\n3. Print\n4. Exit"<<endl;
cin>>menu;
switch(menu){
case 1:
count++;
cout<<"Enter Value to insert : ";
cin>>p->value;
p->next = NULL;
head = p;
system("cls");
cout<<"Do you want to enter any other value?\n\n1. Yes\n2. No";
cin>>sel;
system("cls");
while (sel!=2){
struct Node *q = new Node;
count++;
cout<<"Enter Value to insert : ";
cin>>q->value;
q->next = NULL;
p->next = q;
p = p->next;
system("cls");
cout<<"Do you want to enter any other value?\n\n1. Yes\n2. No";
cin>>sel;
system("cls");
}
goto ask;
break;
case 2:
count--;
p = head;
head = p->next;
p->value = 0;
p->next = NULL;
system("cls");
goto ask;
break;
case 3:
system("cls");
p = head;
if (count==0){
system("cls");
cout<<"Queue is empty.";
}
else{
cout<<"Your queue has the following values"<<endl;
while (p->next!=NULL){
cout<<" "<<p->value;
p = p->next;
}
cout<<" "<<p->value;
}
cout<<"\n\n";
system("pause");
system("cls");
goto ask;
break;
case 4:
return 0;
break;
}
}
Stack Implementation using Single Link List
Stack Implementation using Single Link List
#include <iostream>
using namespace std;
struct Node{
int value;
Node *next;
};
int main(){
for( int i=0;i<=79;i++){
cout<<'*';
}
cout<<" Built & Designed by Arslan Malik Aareez"<<endl;
cout<<" www.CWorldbyAS.blogspot.com"<<endl;
cout<<" Facebook : www.facebook.com/arslan.ud.din"<<endl;
cout<<" Twitter : www.twitter.com/itsaareez"<<endl;
for( int i=0;i<=79;i++){
cout<<'*';
}
int menu;
int sel;
struct Node *p = new Node;
struct Node *head = new Node;
struct Node *r = new Node;
head->next = NULL;
head->value=0;
ask:
cout<<"Enter Your Choice\n"<<endl;
cout<<"1. Push"<<endl;
cout<<"2. Pop"<<endl;
cout<<"3. Print"<<endl;
cout<<"4. Exit"<<endl;
cin>>menu;
system("cls");
switch (menu){
case 1:
cout<<"Enter Value to Push : ";
cin>>p->value;
p->next = NULL;
system("cls");
head = p;
cout<<"Do you want to push another value?"<<endl;
cout<<"1. Yes"<<endl;
cout<<"2. No"<<endl;
cin>>sel;
system("cls");
while (sel!=2){
struct Node *q = new Node;
cout<<"\nEnter Value to Push : ";
cin>>q->value;
q->next=NULL;
p->next = q;
p = p->next;
system("cls");
cout<<"Do you want to push another value?"<<endl;
cout<<"1. Yes"<<endl;
cout<<"2. No"<<endl;
cin>>sel;
system("cls");
}
goto ask;
break;
case 2:
p = head;
if (p->next==NULL && p->value == 0){
cout<<"\nStack is Empty\n\n";
}
while (p->next!=NULL){
r = p;
p = p->next;
}
r->next = NULL;
p->value = 0;
goto ask;
break;
case 3:
system("cls");
p= head;
cout<<"Stack contains the following values "<<endl;
while (p->next!=NULL){
if (p->value==0)
{
cout<<" ";
}
else
{
cout<<" "<<p->value;
}
p=p->next;
}
cout<<" "<<p->value;
cout<<"\n";
goto ask;
break;
case 4:
return 0;
break;
}
}
#include <iostream>
using namespace std;
struct Node{
int value;
Node *next;
};
int main(){
for( int i=0;i<=79;i++){
cout<<'*';
}
cout<<" Built & Designed by Arslan Malik Aareez"<<endl;
cout<<" www.CWorldbyAS.blogspot.com"<<endl;
cout<<" Facebook : www.facebook.com/arslan.ud.din"<<endl;
cout<<" Twitter : www.twitter.com/itsaareez"<<endl;
for( int i=0;i<=79;i++){
cout<<'*';
}
int menu;
int sel;
struct Node *p = new Node;
struct Node *head = new Node;
struct Node *r = new Node;
head->next = NULL;
head->value=0;
ask:
cout<<"Enter Your Choice\n"<<endl;
cout<<"1. Push"<<endl;
cout<<"2. Pop"<<endl;
cout<<"3. Print"<<endl;
cout<<"4. Exit"<<endl;
cin>>menu;
system("cls");
switch (menu){
case 1:
cout<<"Enter Value to Push : ";
cin>>p->value;
p->next = NULL;
system("cls");
head = p;
cout<<"Do you want to push another value?"<<endl;
cout<<"1. Yes"<<endl;
cout<<"2. No"<<endl;
cin>>sel;
system("cls");
while (sel!=2){
struct Node *q = new Node;
cout<<"\nEnter Value to Push : ";
cin>>q->value;
q->next=NULL;
p->next = q;
p = p->next;
system("cls");
cout<<"Do you want to push another value?"<<endl;
cout<<"1. Yes"<<endl;
cout<<"2. No"<<endl;
cin>>sel;
system("cls");
}
goto ask;
break;
case 2:
p = head;
if (p->next==NULL && p->value == 0){
cout<<"\nStack is Empty\n\n";
}
while (p->next!=NULL){
r = p;
p = p->next;
}
r->next = NULL;
p->value = 0;
goto ask;
break;
case 3:
system("cls");
p= head;
cout<<"Stack contains the following values "<<endl;
while (p->next!=NULL){
if (p->value==0)
{
cout<<" ";
}
else
{
cout<<" "<<p->value;
}
p=p->next;
}
cout<<" "<<p->value;
cout<<"\n";
goto ask;
break;
case 4:
return 0;
break;
}
}
Friday, June 20, 2014
Get Abbreviation
Get abbreviation of a given name entered by user in C Programming language by Project Free TV .
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
char name[100];
int i=0;
header();
system("pause");
system("cls");
printf("Enter Name : ");
gets(name);
system("cls");
printf("You have entered %s ",name);
printf("\n\n\n\t\tAbbreviation");
printf("\n\n\t\t %c",name[i]);
for(i=0;name[i]!='\0';i++)
{
if (name[i]==' ')
{
i++;
printf("%c",name[i]);
}
}
printf("\n\n\n\n");
}
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
char name[100];
int i=0;
header();
system("pause");
system("cls");
printf("Enter Name : ");
gets(name);
system("cls");
printf("You have entered %s ",name);
printf("\n\n\n\t\tAbbreviation");
printf("\n\n\t\t %c",name[i]);
for(i=0;name[i]!='\0';i++)
{
if (name[i]==' ')
{
i++;
printf("%c",name[i]);
}
}
printf("\n\n\n\n");
}
Strings Swapping using pointers , malloc() & strcpy()
We know that to swap two numbers in c programming language is much easy but can we swap two strings? Of course you can perform swapping on strings as well. The following program swap two strings using pointers, malloc() and strcpy().
#include<stdio.h>
#include<string.h>
#include<malloc.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
char s1[100],s2[100];
char *hold;
header();
system("pause");
system("cls");
while (!feof(stdin))
{
printf("Enter String 1 : ");
gets(s1);
printf("\nEnter String 2 : ");
gets(s2);
hold=malloc(0); //is used to provide memory for pointer *hold
strcpy(hold,s1);
strcpy(s1,s2);
strcpy(s2,hold);
printf("\n\n After Swapping \n\n ");
printf(" String 1 : %s ; String 2 : %s \n\n\n\n",s1,s2);
}
}
#include<stdio.h>
#include<string.h>
#include<malloc.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
char s1[100],s2[100];
char *hold;
header();
system("pause");
system("cls");
while (!feof(stdin))
{
printf("Enter String 1 : ");
gets(s1);
printf("\nEnter String 2 : ");
gets(s2);
hold=malloc(0); //is used to provide memory for pointer *hold
strcpy(hold,s1);
strcpy(s1,s2);
strcpy(s2,hold);
printf("\n\n After Swapping \n\n ");
printf(" String 1 : %s ; String 2 : %s \n\n\n\n",s1,s2);
}
}
Functions to calculate string length using arrays
There are many programs in which we need to calculate the length of a string. To calculate string length we can use built in function in C Programming to calculate length but we should know what is going on in back end of this function or what is the structure of this function? The following code defines the structure of function used to calculate the length of a string.
#include<stdio.h>
int strln(char a[])
{
int i;
for(i=0;a[i]!='\0';i++);
return i;
}
int main()
{
int a[100000];
puts("Enter String : ");
gets(a);
system("pause");
system("cls");
printf("\n\n\nString Length is %d \n\n\n\n\n",strln(a));
}
#include<stdio.h>
int strln(char a[])
{
int i;
for(i=0;a[i]!='\0';i++);
return i;
}
int main()
{
int a[100000];
puts("Enter String : ");
gets(a);
system("pause");
system("cls");
printf("\n\n\nString Length is %d \n\n\n\n\n",strln(a));
}
Function to copy one string into another using pointers
To copy one string to another string, we have built-in function defined in <string.h> library but to be a good programmer, we should have knowledge about the algorithm of function and the story of back end code. The following code is the implementation of function to copy one string into another using pointers in C Programming Language.
#include<stdio.h>
void strcp(char *a , char *b)
{
int i;
for(i=0;*b!='\0';i++)
{
*a=*b;
a++;
b++;
}
}
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
char a[100];
char b[100];
header();
system("pause");
system("cls");
puts("Enter String 1: ");
gets(a);
puts("Enter String 2 : ");
gets(b);
strcp(a,b);
printf("\nAfter Copying String 2 in String 1 \n\n");
printf("String 1 is : %s \n\n\n\n\n",a);
}
#include<stdio.h>
void strcp(char *a , char *b)
{
int i;
for(i=0;*b!='\0';i++)
{
*a=*b;
a++;
b++;
}
}
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
char a[100];
char b[100];
header();
system("pause");
system("cls");
puts("Enter String 1: ");
gets(a);
puts("Enter String 2 : ");
gets(b);
strcp(a,b);
printf("\nAfter Copying String 2 in String 1 \n\n");
printf("String 1 is : %s \n\n\n\n\n",a);
}
String Comparison using pointers , functions , loops and if selection statements
You can comparison two strings in C Programming language by using built-in function defined in <String.h>. The back end code working behind the function in <String.h> library to comparison two strings works as follows:
#include<stdio.h>
int stcp(char *a , char *b)
{
int i;
while(*a==*b)
{
if (*a=='\0' || *b=='\0')
break;
a++;
b++;
}
if (*a=='\0' && *b=='\0')
return 0;
else return -1;
}
int main()
{
char a[20],b[20];
printf("Enter String 1 : ");
scanf("%s",a);
printf("Enter String 2 : ");
scanf("%s",b);
if(stcp(a,b)==0)
{
printf("String 1 = String 2");
}
else if (stcp(a,b)==-1)
{
printf("String 1 is not matching string 2");
}
}
#include<stdio.h>
int stcp(char *a , char *b)
{
int i;
while(*a==*b)
{
if (*a=='\0' || *b=='\0')
break;
a++;
b++;
}
if (*a=='\0' && *b=='\0')
return 0;
else return -1;
}
int main()
{
char a[20],b[20];
printf("Enter String 1 : ");
scanf("%s",a);
printf("Enter String 2 : ");
scanf("%s",b);
if(stcp(a,b)==0)
{
printf("String 1 = String 2");
}
else if (stcp(a,b)==-1)
{
printf("String 1 is not matching string 2");
}
}
String Addition using pointers, functions and loop
String concatenation is a way to add two strings. For example if we have two strings "My" and "Name" then to produce these two strings as one string that is "My Name", we will concatenate these two strings. We can do concatenation in C Programming Language by using built-in string concatenation function defined in <String.h> library but we can also make our own function to concatenate two strings. The following code do concatenation of two strings using pointers, functions and loops.
#include <stdio.h>
#include<conio.h>
void concatenate_string(char*, char*);
int main()
{
char original[100], add[100];
printf("Enter source string\n");
gets(original);
printf("Enter string to concatenate\n");
gets(add);
concatenate_string(original, add);
printf("String after concatenation is \"%s\"\n", original);
getch();
return 0;
}
void concatenate_string(char *original, char *add)
{
while(*original)
original++;
while(*add)
{
*original = *add;
add++;
original++;
}
*original = '\0';
}
printing spaced letters in a string like ABC to A B C
#include<stdio.h>
#include<conio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
char a[30]={0};
int i;
header();
system("pause");
system("cls");
printf("Enter String : ");
scanf("%s",a);
printf("\n");
for(i=0;a[i]!='\0';i++)
{
printf(" %c ",a[i]);
}
printf("\n\n\n\n");
getch();
}
#include<conio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
char a[30]={0};
int i;
header();
system("pause");
system("cls");
printf("Enter String : ");
scanf("%s",a);
printf("\n");
for(i=0;a[i]!='\0';i++)
{
printf(" %c ",a[i]);
}
printf("\n\n\n\n");
getch();
}
Distinguish between vowels and consonants in strings
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
char name[100]={0};
printf("Enter String : ");
scanf("%[^aeiou]",name);
printf("\n%s",name);
}
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
char name[100]={0};
printf("Enter String : ");
scanf("%[^aeiou]",name);
printf("\n%s",name);
}
Search vowels in a string
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
char name[100]={0};
header();
system("pause");
system("cls");
printf("Enter String : ");
scanf("%[aeiou]",name);
printf("\n%s",name);
}
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
char name[100]={0};
header();
system("pause");
system("cls");
printf("Enter String : ");
scanf("%[aeiou]",name);
printf("\n%s",name);
}
Reversing String
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
char name[30];
int i,j;
header();
system("pause");
system("cls");
printf("Enter String : ");
gets(name);
printf("You have Entered : ");
for(i=0;name[i]!='\0';i++)
printf("%c",name[i]);
printf("\n\n\n");
for(i=0;name[i]!='\0';i++);
printf("String Length : %d ",i);
printf("\n\n\n");
printf("Reversed String is : ");
for(j=i;j>=0;j--)
printf("%c",name[j]);
printf("\n\n\n");
}
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
char name[30];
int i,j;
header();
system("pause");
system("cls");
printf("Enter String : ");
gets(name);
printf("You have Entered : ");
for(i=0;name[i]!='\0';i++)
printf("%c",name[i]);
printf("\n\n\n");
for(i=0;name[i]!='\0';i++);
printf("String Length : %d ",i);
printf("\n\n\n");
printf("Reversed String is : ");
for(j=i;j>=0;j--)
printf("%c",name[j]);
printf("\n\n\n");
}
gets() and puts() function in String
#include<stdio.h>
int main()
{
char c1[100];
puts("Enter String : ");
gets(c1);
printf("You Have Entered : %s",c1);
}
int main()
{
char c1[100];
puts("Enter String : ");
gets(c1);
printf("You Have Entered : %s",c1);
}
Get String and Print string
#include<stdio.h>
int main()
{
char c1[100],c2[100],c3[100],c4[100];
puts("Enter Name of 1st City : ");
gets(c1);
puts("Enter Name of 2nd City : ");
gets(c2);
puts("Enter Name of 3rd City : ");
gets(c3);
puts("Enter Name of 4th City : ");
gets(c4);
printf("\n\n\n\n\n");
puts(c1);
puts(c2);
puts(c3);
puts(c4);
}
int main()
{
char c1[100],c2[100],c3[100],c4[100];
puts("Enter Name of 1st City : ");
gets(c1);
puts("Enter Name of 2nd City : ");
gets(c2);
puts("Enter Name of 3rd City : ");
gets(c3);
puts("Enter Name of 4th City : ");
gets(c4);
printf("\n\n\n\n\n");
puts(c1);
puts(c2);
puts(c3);
puts(c4);
}
//Exercise 1: Write a C Program to create the data for some students
//(roll, name, mark1, mark2, mark3, term mark) and then find the total
//marks for each student and average mark of each student.
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
struct student
{
int rollno;
char name[500];
float mark1;
float mark2;
float mark3;
float term_mark;
float total;
float average;
}st;
int main()
{
header();
system("pause");
system("cls");
printf("Enter Roll No. : ");
scanf("%d",&st.rollno);
fflush(stdin);
printf("\nEnter Name : ");
gets(st.name);
printf("\nEnter Marks 1 : ");
scanf("%f",&st.mark1);
printf("\nEnter Marks 2 : ");
scanf("%f",&st.mark2);
printf("\nEnter Marks 3 : ");
scanf("%f",&st.mark3);
printf("\nEnter Term Marks : ");
scanf("%f",&st.term_mark);
system("cls");
fflush(stdin);
printf("Name : %s ",st.name);
printf("\nMarks 1 : %.2f",st.mark1);
printf("\nMarks 2 : %.2f",st.mark2);
printf("\nMarks 3 : %.2f",st.mark3);
printf("\nTerm Marks : %.2f",st.term_mark);
st.total=0;
st.total=st.mark1+st.mark2+st.mark3+st.term_mark;
st.average=0;
st.average=st.total/4;
printf("\nAverage : %.2f",st.average);
printf("\nTotal : %.2f ",st.total);
printf("\n\n\n");
return 0;
}
//(roll, name, mark1, mark2, mark3, term mark) and then find the total
//marks for each student and average mark of each student.
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
struct student
{
int rollno;
char name[500];
float mark1;
float mark2;
float mark3;
float term_mark;
float total;
float average;
}st;
int main()
{
header();
system("pause");
system("cls");
printf("Enter Roll No. : ");
scanf("%d",&st.rollno);
fflush(stdin);
printf("\nEnter Name : ");
gets(st.name);
printf("\nEnter Marks 1 : ");
scanf("%f",&st.mark1);
printf("\nEnter Marks 2 : ");
scanf("%f",&st.mark2);
printf("\nEnter Marks 3 : ");
scanf("%f",&st.mark3);
printf("\nEnter Term Marks : ");
scanf("%f",&st.term_mark);
system("cls");
fflush(stdin);
printf("Name : %s ",st.name);
printf("\nMarks 1 : %.2f",st.mark1);
printf("\nMarks 2 : %.2f",st.mark2);
printf("\nMarks 3 : %.2f",st.mark3);
printf("\nTerm Marks : %.2f",st.term_mark);
st.total=0;
st.total=st.mark1+st.mark2+st.mark3+st.term_mark;
st.average=0;
st.average=st.total/4;
printf("\nAverage : %.2f",st.average);
printf("\nTotal : %.2f ",st.total);
printf("\n\n\n");
return 0;
}
Getting and Printing Data using structs and pointers together
#include<stdio.h>
struct student
{
int rollno;
char name[100];
float marks[5];
float average;
}s[20];
void get(struct student *a)
{
int i;
float j=0.0;
printf("Enter Roll No. of Student : ");
scanf("%d",&a->rollno);
printf("Enter Name of Student : ");
fflush(stdin);
scanf("%s",&a->name);
for(i=0;i<5;i++)
{
printf("Enter Marks of %d Subject : ",i+1);
scanf("%f",&a->marks[i]);
}
for(i=0;i<5;i++)
{
j+=a->marks[i];
}
a->average=j/5;
}
void print(struct student *a)
{
int i;
printf("\nRoll No : %d",a->rollno);
printf("\nName : %s",a->name);
for(i=0;i<5;i++)
{
printf("\nMarks of Subject %d : %.2f ",i+1,a->marks[i]);
}
printf("\nAverage : %.2f ",a->average);
}
int main()
{
int i;
for(i=0;i<2;i++)
{
get(&s[i]);
}
for(i=0;i<2;i++)
{
if(s[i].average>70)
print(&s[i]);
}
}
struct student
{
int rollno;
char name[100];
float marks[5];
float average;
}s[20];
void get(struct student *a)
{
int i;
float j=0.0;
printf("Enter Roll No. of Student : ");
scanf("%d",&a->rollno);
printf("Enter Name of Student : ");
fflush(stdin);
scanf("%s",&a->name);
for(i=0;i<5;i++)
{
printf("Enter Marks of %d Subject : ",i+1);
scanf("%f",&a->marks[i]);
}
for(i=0;i<5;i++)
{
j+=a->marks[i];
}
a->average=j/5;
}
void print(struct student *a)
{
int i;
printf("\nRoll No : %d",a->rollno);
printf("\nName : %s",a->name);
for(i=0;i<5;i++)
{
printf("\nMarks of Subject %d : %.2f ",i+1,a->marks[i]);
}
printf("\nAverage : %.2f ",a->average);
}
int main()
{
int i;
for(i=0;i<2;i++)
{
get(&s[i]);
}
for(i=0;i<2;i++)
{
if(s[i].average>70)
print(&s[i]);
}
}
Getting and Printing Data using structs
#include<stdio.h>
struct account
{
char name[100];
int balance;
}ac;
void get(struct account *a)
{
printf("Enter Name : ");
fflush(stdin);
scanf("%s",&a->name);
printf("\nEnter Account Balance : ");
scanf("%d",&a->balance);
}
void print(struct account *b)
{
printf("Name : %s",b->name);
printf("\nBalance : %d",b->balance);
}
int main()
{
get(&ac);
print(&ac);
}
struct account
{
char name[100];
int balance;
}ac;
void get(struct account *a)
{
printf("Enter Name : ");
fflush(stdin);
scanf("%s",&a->name);
printf("\nEnter Account Balance : ");
scanf("%d",&a->balance);
}
void print(struct account *b)
{
printf("Name : %s",b->name);
printf("\nBalance : %d",b->balance);
}
int main()
{
get(&ac);
print(&ac);
}
Changes 'a' & 'b' to '-' in file using file handling
#include<stdio.h>
int main()
{
FILE *p1, *p2;
char f1[20], f2[20];
char ch;
printf("Enter File name which has to be copied: ");
gets(f1);
p1=fopen(f1,"r");
if(p1==NULL)
{
printf("\nUnable to open %s",f1);
return 1;
}
printf("Enter Destination file");
gets(f2);
p2=fopen(f2,"w");
if(p2==NULL)
{
printf("Cannot open %s",f2);
return 1;
}
while((ch=fgetc(p1))!=EOF)
{
if(' ' || 'a' || 'b')
ch='-';
fputc(ch,p2);
}
printf("Completed");
fclose(p1);
fclose(p2);
return 0;
}
int main()
{
FILE *p1, *p2;
char f1[20], f2[20];
char ch;
printf("Enter File name which has to be copied: ");
gets(f1);
p1=fopen(f1,"r");
if(p1==NULL)
{
printf("\nUnable to open %s",f1);
return 1;
}
printf("Enter Destination file");
gets(f2);
p2=fopen(f2,"w");
if(p2==NULL)
{
printf("Cannot open %s",f2);
return 1;
}
while((ch=fgetc(p1))!=EOF)
{
if(' ' || 'a' || 'b')
ch='-';
fputc(ch,p2);
}
printf("Completed");
fclose(p1);
fclose(p2);
return 0;
}
Calculate 'A' , 'B' & ' space ' in created file
#include<stdio.h>
int main()
{
FILE *p1,*p2,*p3;
char f1[20], f2[20],f[30];
char ch;
int count1=0,count2=0,count3=0;
printf("Enter File name which has to be copied: ");
gets(f1);
p1=fopen(f1,"r");
if(p1==NULL)
{
printf("\nUnable to open %s",f1);
return 1;
}
printf("\nEnter Name of File in which you want to copy Data : ");
gets(f2);
p2=fopen(f2,"w");
if(p2==NULL)
{
printf("Cannot open %s",f2);
return 1;
}
rewind(p1);
while((ch=fgetc(p1))!=EOF)
{
if(ch=='A' || ch=='a')
{
count1++;
}
if(ch=='b' || ch=='B')
{
count2++;
}
if(ch==' ')
{
count3++;
}
fputc(ch,p2);
}
printf("a = %d ,b = %d , space = %d",count1,count2,count3);
printf("\n\nData of File 1 Has been copied to File 2\n\n\n");
fclose(p1);
fclose(p2);
return 0;
}
int main()
{
FILE *p1,*p2,*p3;
char f1[20], f2[20],f[30];
char ch;
int count1=0,count2=0,count3=0;
printf("Enter File name which has to be copied: ");
gets(f1);
p1=fopen(f1,"r");
if(p1==NULL)
{
printf("\nUnable to open %s",f1);
return 1;
}
printf("\nEnter Name of File in which you want to copy Data : ");
gets(f2);
p2=fopen(f2,"w");
if(p2==NULL)
{
printf("Cannot open %s",f2);
return 1;
}
rewind(p1);
while((ch=fgetc(p1))!=EOF)
{
if(ch=='A' || ch=='a')
{
count1++;
}
if(ch=='b' || ch=='B')
{
count2++;
}
if(ch==' ')
{
count3++;
}
fputc(ch,p2);
}
printf("a = %d ,b = %d , space = %d",count1,count2,count3);
printf("\n\nData of File 1 Has been copied to File 2\n\n\n");
fclose(p1);
fclose(p2);
return 0;
}
Creating File & Writing data in file
//Exercise 1: Write a C Program to open a file named “DATA” and write a line of text in it by reading the text from the keyboard.
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
FILE *f;
char a[500];
header();
system("pause");
system("cls");
f=fopen("DATA.txt" , "w");
if (f==NULL)
{
printf("\nUnable to Open File ....!!");
return 1;
}
printf("\nEnter any Line to Write in File : ");
gets(a);
while(!feof(stdin))
{
fprintf(f,"%s",a);
printf("\nEnter any Line to Write in File Ctrl + Z to exit the program : ");
gets(a);
}
fclose(f);
printf("\nFile Has been created Successfully....!!!\n\n\n\n");
}
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
FILE *f;
char a[500];
header();
system("pause");
system("cls");
f=fopen("DATA.txt" , "w");
if (f==NULL)
{
printf("\nUnable to Open File ....!!");
return 1;
}
printf("\nEnter any Line to Write in File : ");
gets(a);
while(!feof(stdin))
{
fprintf(f,"%s",a);
printf("\nEnter any Line to Write in File Ctrl + Z to exit the program : ");
gets(a);
}
fclose(f);
printf("\nFile Has been created Successfully....!!!\n\n\n\n");
}
Key Search in File Handling
#include<stdio.h>
#include<string.h>
int main()
{
FILE *f,*e;
char a[100];
int roll;
char key[100];
f=fopen("abc.dat" , "w");
if (f==NULL)
{
printf("\nUnable to Open File....!!!");
return 1;
}
printf("\nEnter any Text to Write in File : ");
gets(a);
fflush(stdin);
printf("\nEnter Roll No. : ");
scanf("%d",&roll);
while(!feof(stdin))
{
fprintf(f,"%s %d\n",a,roll);
printf("\nEnter any Text to Write in File : ");
fflush(stdin);
gets(a);
printf("\nEnter Roll No. : ");
scanf("%d",&roll);
}
fclose(f);
e=fopen("abc.dat" , "r");
if (e==NULL)
{
printf("\nUnable to Open File....!!!!");
return 1;
}
printf("\n\nEnter key : ");
gets(key);
fscanf(e,"%s%d",a,&roll);
while(!feof(e))
{
if (strcmp(key,a)==0)
{
printf("%s %d",a,roll);
}
fscanf(e,"%s%d",a,&roll);
}
fclose(e);
}
#include<string.h>
int main()
{
FILE *f,*e;
char a[100];
int roll;
char key[100];
f=fopen("abc.dat" , "w");
if (f==NULL)
{
printf("\nUnable to Open File....!!!");
return 1;
}
printf("\nEnter any Text to Write in File : ");
gets(a);
fflush(stdin);
printf("\nEnter Roll No. : ");
scanf("%d",&roll);
while(!feof(stdin))
{
fprintf(f,"%s %d\n",a,roll);
printf("\nEnter any Text to Write in File : ");
fflush(stdin);
gets(a);
printf("\nEnter Roll No. : ");
scanf("%d",&roll);
}
fclose(f);
e=fopen("abc.dat" , "r");
if (e==NULL)
{
printf("\nUnable to Open File....!!!!");
return 1;
}
printf("\n\nEnter key : ");
gets(key);
fscanf(e,"%s%d",a,&roll);
while(!feof(e))
{
if (strcmp(key,a)==0)
{
printf("%s %d",a,roll);
}
fscanf(e,"%s%d",a,&roll);
}
fclose(e);
}
//Exercise 2: Write a C Program to read the contents of file ‘File1’ and paste the contents at the beginning of another file ‘File2’ without taking help of any extra file.
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
FILE *a,*b;
char f_1[100],f_2[100],ch;
header();
system("pause");
system("cls");
a=fopen("source.txt" , "w");
if(a==NULL)
{
printf("\nOooppsss Unable to Open.....!!!");
return 1;
}
printf("Write any text in File or Ctrl+Z to Exit the Program : ");
gets(f_1);
while(!feof(stdin))
{
fprintf(a,"%s",f_1);
gets(f_1);
}
fclose(a);
printf("\n\nEnter the name of Source File : ");
gets(f_1);
a=fopen(f_1 , "r");
if (a==NULL)
{
printf("\nOooppsss .... Unable to Open the File....!!!");
return 1;
}
printf("\n\nEnter the name of Target File in which you want to copy the Data : ");
gets(f_2);
b=fopen(f_2,"w");
if(b==NULL)
{
printf("\nOoooOOoopsss... Unable to Open the File....!!!");
return 1;
}
while(!feof(a))
{
ch=fgetc(a);
putc(ch,b);
}
fclose(a);
fclose(b);
printf("\n\n\t\tCongratulations....!!!!");
printf("\n\n\t\tFile Has Been Copied Successfully...!!!\n\n");
}
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
FILE *a,*b;
char f_1[100],f_2[100],ch;
header();
system("pause");
system("cls");
a=fopen("source.txt" , "w");
if(a==NULL)
{
printf("\nOooppsss Unable to Open.....!!!");
return 1;
}
printf("Write any text in File or Ctrl+Z to Exit the Program : ");
gets(f_1);
while(!feof(stdin))
{
fprintf(a,"%s",f_1);
gets(f_1);
}
fclose(a);
printf("\n\nEnter the name of Source File : ");
gets(f_1);
a=fopen(f_1 , "r");
if (a==NULL)
{
printf("\nOooppsss .... Unable to Open the File....!!!");
return 1;
}
printf("\n\nEnter the name of Target File in which you want to copy the Data : ");
gets(f_2);
b=fopen(f_2,"w");
if(b==NULL)
{
printf("\nOoooOOoopsss... Unable to Open the File....!!!");
return 1;
}
while(!feof(a))
{
ch=fgetc(a);
putc(ch,b);
}
fclose(a);
fclose(b);
printf("\n\n\t\tCongratulations....!!!!");
printf("\n\n\t\tFile Has Been Copied Successfully...!!!\n\n");
}
Calculating Spaces Lines Words Letters
//Exercise 3: Write a C Program to count the number of characters, words, lines, spaces and tabs in the input supplied from a file.
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
FILE *f,*e;
char a[500],ch;
int chr=0,sp=0,words=0,lines=0,tabs=0;
header();
system("pause");
system("cls");
f=fopen("abc.txt" , "w");
if(f==NULL)
{
printf("\nUnable to Open File...!!");
return 1;
}
printf("\nWrite on File or CTRL+Z to exit : ");
gets(a);
while(!feof(stdin))
{
fprintf(f,"%s\n",a);
gets(a);
}
fclose(f);
e=fopen("abc.txt" , "r");
if(e==NULL)
{
printf("\nUnable to Open File...!!");
return 1;
}
rewind(e);
while((ch=fgetc(e))!=EOF)
{
if (ch>='A' || ch>='a' && ch<='z' || ch<='Z')
{
chr++;
}
if(ch==' ')
{
sp++;
words++;
}
if(ch=='\t')
{
tabs++;
words++;
}
if(ch=='\n')
{
lines++;
words++;
}
}
printf("\nCharacters : %d\nSpaces : %d \nWords : %d\nLines : %d\nTabs : %d ",chr,sp,words,lines,tabs);
}
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
FILE *f,*e;
char a[500],ch;
int chr=0,sp=0,words=0,lines=0,tabs=0;
header();
system("pause");
system("cls");
f=fopen("abc.txt" , "w");
if(f==NULL)
{
printf("\nUnable to Open File...!!");
return 1;
}
printf("\nWrite on File or CTRL+Z to exit : ");
gets(a);
while(!feof(stdin))
{
fprintf(f,"%s\n",a);
gets(a);
}
fclose(f);
e=fopen("abc.txt" , "r");
if(e==NULL)
{
printf("\nUnable to Open File...!!");
return 1;
}
rewind(e);
while((ch=fgetc(e))!=EOF)
{
if (ch>='A' || ch>='a' && ch<='z' || ch<='Z')
{
chr++;
}
if(ch==' ')
{
sp++;
words++;
}
if(ch=='\t')
{
tabs++;
words++;
}
if(ch=='\n')
{
lines++;
words++;
}
}
printf("\nCharacters : %d\nSpaces : %d \nWords : %d\nLines : %d\nTabs : %d ",chr,sp,words,lines,tabs);
}
Saturday, May 31, 2014
Write a program to declare an integer array of 50 elements. A. Write a function getArray() to get array input from the user that will be used to initialize the first thirty five (35) elements of the array by getting input from the user. The rest of the 15 entries would be set to zero (0). B. Write a function FindEven() to find the total numbers of even numbers in the given array. C. Write a function ModifyArray() to make the each array element to a multiple of four(04). D. Write a function named ‘FindMin()’ that will find the smallest element in the given array and return the smallest element.
Write a program to declare an integer array of 50 elements.
A. Write a function getArray() to get array input from the user that will be used to initialize the first thirty five (35) elements of the array by getting input from the user. The rest of the 15 entries would be set to zero (0).
B. Write a function FindEven() to find the total numbers of even numbers in the given array.
C. Write a function ModifyArray() to make the each array element to a multiple of four(04).
D. Write a function named ‘FindMin()’ that will find the smallest element in the given array and return the smallest element.
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
void getArray(int a[])
{
int i;
for(i=0;i<35;i++)
{
printf("Enter %d Number : ",i+1);
scanf("%d",&a[i]);
printf("\n");
}
printf("\nYou Array's Elements are");
for(i=0;i<50;i++)
printf(" %d ",a[i]);
}
void even(int a[])
{
int i;
for (i=0;i<35;i++)
{
if (a[i]%2==0)
printf("%d is Even\n",a[i]);
}
}
int multiply(int a[])
{
int i;
for(i=0;i<35;i++)
{
printf("%d X 4 is %d\n",a[i],a[i]*4);
}
}
int FindMin(int a[])
{
int i,j,hold;
for(j=0;j<35;j++)
{
for(i=0;i<35-1;i++)
{
if (a[i]>a[i+1])
{
hold=a[i];
a[i]=a[i+1];
a[i+1]=a[i];
}
}
}
printf(" Minimum Number You have Entered is %d ",a[0]);
printf("\n\n\n\n\n");
}
int main()
{
int a[50]={0},i;
header();
system("pause");
system("cls");
getArray(a);
system("pause");
system("cls");
even(a);
system("pause");
system("cls");
multiply(a);
system("pause");
system("cls");
FindMin(a);
}
A. Write a function getArray() to get array input from the user that will be used to initialize the first thirty five (35) elements of the array by getting input from the user. The rest of the 15 entries would be set to zero (0).
B. Write a function FindEven() to find the total numbers of even numbers in the given array.
C. Write a function ModifyArray() to make the each array element to a multiple of four(04).
D. Write a function named ‘FindMin()’ that will find the smallest element in the given array and return the smallest element.
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
void getArray(int a[])
{
int i;
for(i=0;i<35;i++)
{
printf("Enter %d Number : ",i+1);
scanf("%d",&a[i]);
printf("\n");
}
printf("\nYou Array's Elements are");
for(i=0;i<50;i++)
printf(" %d ",a[i]);
}
void even(int a[])
{
int i;
for (i=0;i<35;i++)
{
if (a[i]%2==0)
printf("%d is Even\n",a[i]);
}
}
int multiply(int a[])
{
int i;
for(i=0;i<35;i++)
{
printf("%d X 4 is %d\n",a[i],a[i]*4);
}
}
int FindMin(int a[])
{
int i,j,hold;
for(j=0;j<35;j++)
{
for(i=0;i<35-1;i++)
{
if (a[i]>a[i+1])
{
hold=a[i];
a[i]=a[i+1];
a[i+1]=a[i];
}
}
}
printf(" Minimum Number You have Entered is %d ",a[0]);
printf("\n\n\n\n\n");
}
int main()
{
int a[50]={0},i;
header();
system("pause");
system("cls");
getArray(a);
system("pause");
system("cls");
even(a);
system("pause");
system("cls");
multiply(a);
system("pause");
system("cls");
FindMin(a);
}
Write a program to declare an integer array of 50 elements which gets 35 numbers from user and print rest of 15 as ZEROS.
//Exercise 1:Write a program to declare an integer array of 50 elements.
//A. Write a function getArray() to get array input from the user that will be used to initialize the first thirty five (35) elements of the array by getting input from the user. The rest of the 15 entries would be set to zero (0).
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
int a[50]={0},i;
header();
system("pause");
system("cls");
for(i=0;i<35;i++)
{
printf("%d Number : ",i+1);
scanf("%d",&a[i]);
printf("\n");
}
for(i=0;i<50;i++)
printf(" %d ",a[i]);
getch();
}
//A. Write a function getArray() to get array input from the user that will be used to initialize the first thirty five (35) elements of the array by getting input from the user. The rest of the 15 entries would be set to zero (0).
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
int a[50]={0},i;
header();
system("pause");
system("cls");
for(i=0;i<35;i++)
{
printf("%d Number : ",i+1);
scanf("%d",&a[i]);
printf("\n");
}
for(i=0;i<50;i++)
printf(" %d ",a[i]);
getch();
}
Exercise 1: Write a C Program to create the data for some students (roll, name, mark1, mark2, mark3, term mark) and then find the total marks for each student and average mark of each student.
//Exercise 1: Write a C Program to create the data for some students
//(roll, name, mark1, mark2, mark3, term mark) and then find the total
//marks for each student and average mark of each student.
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
struct student
{
int rollno;
char name[500];
float mark1;
float mark2;
float mark3;
float term_mark;
float total;
float average;
}st;
int main()
{
header();
system("pause");
system("cls");
printf("Enter Roll No. : ");
scanf("%d",&st.rollno);
fflush(stdin);
printf("\nEnter Name : ");
gets(st.name);
printf("\nEnter Marks 1 : ");
scanf("%f",&st.mark1);
printf("\nEnter Marks 2 : ");
scanf("%f",&st.mark2);
printf("\nEnter Marks 3 : ");
scanf("%f",&st.mark3);
printf("\nEnter Term Marks : ");
scanf("%f",&st.term_mark);
system("cls");
fflush(stdin);
printf("Name : %s ",st.name);
printf("\nMarks 1 : %.2f",st.mark1);
printf("\nMarks 2 : %.2f",st.mark2);
printf("\nMarks 3 : %.2f",st.mark3);
printf("\nTerm Marks : %.2f",st.term_mark);
st.total=0;
st.total=st.mark1+st.mark2+st.mark3+st.term_mark;
st.average=0;
st.average=st.total/4;
printf("\nAverage : %.2f",st.average);
printf("\nTotal : %.2f ",st.total);
printf("\n\n\n");
return 0;
}
//(roll, name, mark1, mark2, mark3, term mark) and then find the total
//marks for each student and average mark of each student.
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
struct student
{
int rollno;
char name[500];
float mark1;
float mark2;
float mark3;
float term_mark;
float total;
float average;
}st;
int main()
{
header();
system("pause");
system("cls");
printf("Enter Roll No. : ");
scanf("%d",&st.rollno);
fflush(stdin);
printf("\nEnter Name : ");
gets(st.name);
printf("\nEnter Marks 1 : ");
scanf("%f",&st.mark1);
printf("\nEnter Marks 2 : ");
scanf("%f",&st.mark2);
printf("\nEnter Marks 3 : ");
scanf("%f",&st.mark3);
printf("\nEnter Term Marks : ");
scanf("%f",&st.term_mark);
system("cls");
fflush(stdin);
printf("Name : %s ",st.name);
printf("\nMarks 1 : %.2f",st.mark1);
printf("\nMarks 2 : %.2f",st.mark2);
printf("\nMarks 3 : %.2f",st.mark3);
printf("\nTerm Marks : %.2f",st.term_mark);
st.total=0;
st.total=st.mark1+st.mark2+st.mark3+st.term_mark;
st.average=0;
st.average=st.total/4;
printf("\nAverage : %.2f",st.average);
printf("\nTotal : %.2f ",st.total);
printf("\n\n\n");
return 0;
}
//Exercise 3: Write a C Program to count the number of characters, words, lines, spaces and tabs in the input supplied from a file.
//Exercise 3: Write a C Program to count the number of characters, words, lines, spaces and tabs in the input supplied from a file.
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
FILE *f,*e;
char a[500],ch;
int chr=0,sp=0,words=0,lines=0,tabs=0;
header();
system("pause");
system("cls");
f=fopen("abc.txt" , "w");
if(f==NULL)
{
printf("\nUnable to Open File...!!");
return 1;
}
printf("\nWrite on File or CTRL+Z to exit : ");
gets(a);
while(!feof(stdin))
{
fprintf(f,"%s\n",a);
gets(a);
}
fclose(f);
e=fopen("abc.txt" , "r");
if(e==NULL)
{
printf("\nUnable to Open File...!!");
return 1;
}
rewind(e);
while((ch=fgetc(e))!=EOF)
{
if (ch>='A' || ch>='a' && ch<='z' || ch<='Z')
{
chr++;
}
if(ch==' ')
{
sp++;
words++;
}
if(ch=='\t')
{
tabs++;
words++;
}
if(ch=='\n')
{
lines++;
words++;
}
}
printf("\nCharacters : %d\nSpaces : %d \nWords : %d\nLines : %d\nTabs : %d ",chr,sp,words,lines,tabs);
}
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
FILE *f,*e;
char a[500],ch;
int chr=0,sp=0,words=0,lines=0,tabs=0;
header();
system("pause");
system("cls");
f=fopen("abc.txt" , "w");
if(f==NULL)
{
printf("\nUnable to Open File...!!");
return 1;
}
printf("\nWrite on File or CTRL+Z to exit : ");
gets(a);
while(!feof(stdin))
{
fprintf(f,"%s\n",a);
gets(a);
}
fclose(f);
e=fopen("abc.txt" , "r");
if(e==NULL)
{
printf("\nUnable to Open File...!!");
return 1;
}
rewind(e);
while((ch=fgetc(e))!=EOF)
{
if (ch>='A' || ch>='a' && ch<='z' || ch<='Z')
{
chr++;
}
if(ch==' ')
{
sp++;
words++;
}
if(ch=='\t')
{
tabs++;
words++;
}
if(ch=='\n')
{
lines++;
words++;
}
}
printf("\nCharacters : %d\nSpaces : %d \nWords : %d\nLines : %d\nTabs : %d ",chr,sp,words,lines,tabs);
}
//Name Arslan ud Din Shafiq
//Reg.No. DDP-SP14-BSE-005
//Section A
//Assignment # 3
//Submitted to Sir A.K.Shahid
//COMSATS Institute of Information Technology, Lahore, Pakistan
#include<stdio.h> //hearder file for I/O
char a[3][3]={0},i,j; //a 2D array of 3X3 is globally declared and initialized to zero so that it doesn't give any junk value, variable i and j are also globally declared. They are declared globally so that they can be used throughout the program.
void header(); //prototype of header funtion
void name(char *pt1,char *pt2); //prototype of name function
void filled(); //prototype of filled funtion
int condition1(); //prototype of condition1 funtion
int condition2(); //prototype of condition2 funtion
int win1(); //prototype of win1 funtion
int win2(); //prototype of win2 funtion
void toe(); //prototype of toe funtion
void print1(); //prototype of print1 funtion
void design(); //prototype of design funtion
void gameloading(); //prototype of gameloading funtion
struct std //data structure is declared as std
{
int ch; //object of data structure
char s1[100],s2[100]; //character type object declaration in data structure
}st;
int main() //main funtion
{
int menu; //declaring menu variable
header(); //calling header funtion
system("pause"); //to pause screen
again: //pointing for goto statement
gameloading(); //loading funtion is called
printf("\n\n\n\n\n\n\n\n\n\t\t\t1. Start Game\n\n\t\t\t2. Exit \n\t\t\t");
menu=getche()-48; //getting integer value from user
system("cls"); //clearing screen
switch(menu) //switch selection statement is applied on menu
{
case 1: //first case for selection statement
if(menu==1) //if user press 1 then game is started
design(); //design() funtion is called
break; //break statement for case 1
case 2:
if(menu==2) //if user select 2 then game is exited
return 0; //terminating condition
break;
default: //if user select any option except 1 and 2 then default condition is performed
printf("\n\n\nYou have Selected Wrong Option.");
printf("\n\n\n\n\n\n");
system("pause"); //pausing screen
system("cls"); //clearing screen
goto again; //if default condition is performed this statement goes to again pointing value.
break;
}
}
void header() //a function which does not return any value used to show header file in main function
{
printf("\n\t\t\t TIC TAC TOE"); //printf statement is used for printing & \n for next line and \t for tab or space
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
void name(char *pt1,char *pt2) //pointer funtion which gets the name of user and pass the address where it called
{
puts("\n\n\t\t\tEnter Name of Player 1 : "); //asking player 1 to enter name ...puts is used to print string
printf("\n\t\t\t");
gets(pt1); //gets is used to get the string from user
puts("\n\n\t\t\tEnter Name of Player 2 : "); //asking player 2 to enter name
printf("\n\t\t\t");
gets(pt2); //getting name of 2nd player
}
void filled() //a function which tell us about the already filled box
{
condition1(); //it checks the conditions given in funtion condition1() and decrement of i-- is used to identify is the box already filled
i--;
printf("\n\n\n\nOooOpppss This Block is already filled....!!!\n\n\n\n\n"); //this line is printed if the box is already filled
system("pause"); // system pasuse is the system function which is used for a pause and to continue it requires pressing of any key
system("cls"); //this is also a system function which is used for clearing screen
}
int condition1() //a funtion which return integer value if any of the given condition is true...THIS CONDITION IS FOR PLAYER 1
{
if ( a[0][0]=='X' && a[0][1]=='X' && a[0][2]=='X' || a[1][0]=='X' && a[1][1]=='X' && a[1][2]=='X' || a[2][0]=='X' && a[2][1]=='X' && a[2][2]=='X' ) //this condition checks all the horizontal rows values one by one and returns 1 if condition is true for any horizontal row
return 1;
else if ( a[0][0]=='X' && a[1][0]=='X' && a[2][0]=='X' || a[0][1]=='X' && a[1][1]=='X' && a[2][1]=='X' || a[0][2]=='X' && a[1][2]=='X' && a[2][2]=='X' ) //this condition checks all the vertical column values one by one and returns 1 if condition is true for any column row
return 1;
else if ( a[0][0]=='X' && a[1][1]=='X' && a[2][2]=='X') //this condition checks the principle diagonal values and returns 1 if condition is true. AND operator is used because all conditions in that statement should be true in order to return 1
return 1;
else if ( a[0][2]=='X' && a[1][1]=='X' && a[2][0]=='X') //this condition checks the values in diagonal boxes
return 1; //it the above statement is true it returns value 1
}
int condition2() //a funtion which return integer value if any of the given condition is true...THIS CONDITION IS FOR PLAYER 2
{
if ( a[0][0]=='O' && a[0][1]=='O' && a[0][2]=='O' || a[1][0]=='O' && a[1][1]=='O' && a[1][2]=='O' || a[2][0]=='O' && a[2][1]=='O' && a[2][2]=='O' ) //this condition checks all the horizontal rows values one by one and returns 2 if condition is true for any horizontal row
return 2;
else if ( a[0][0]=='O' && a[1][0]=='O' && a[2][0]=='O' || a[0][1]=='O' && a[1][1]=='O' && a[2][1]=='O' || a[0][2]=='O' && a[1][2]=='O' && a[2][2]=='O' ) //this condition checks all the vertical column values one by one and returns 2 if condition is true for any column row
return 2;
else if ( a[0][0]=='O' && a[1][1]=='O' && a[2][2]=='O') //this condition checks the principle diagonal values and returns 2 if condition is true. AND operator is used because all conditions in that statement should be true in order to return 2
return 2;
else if ( a[0][2]=='O' && a[1][1]=='O' && a[2][0]=='O') //this condition checks the values in diagonal boxes
return 2; //it the above statement is true it returns value 2
}
int win1() //a funtion which declares the winning of player 1 and returns 0 if any condition in condition1() is true.
{
if(condition1()==1) //selection statement used to tets the conidition1() function
printf("\n\n\nCongratulations...!!! Player 1 is the WINNER...!!!\n\n"); //winning declaration printing
return 0;
}
int win2() //a funtion which declares the winning of player 2 and returns 0 if any condition in condition2() is true.
{
if(condition2()==2) //selection statement used to tets the conidition2() function
printf("\n\n\nCongratulations...!!! Player 2 is the WINNER...!!!\n\n\n"); //winning declaration printing
return 0;
}
void toe() //void function used which returns nothing. It is just printed to tell the user about the box number for selection
{
printf("\n\t\t\t 1 | 2 | 3 \n\t\t\t___|___|___\n\t\t\t 4 | 5 | 6 \n\t\t\t___|___|___\n\t\t\t 7 | 8 | 9 \n\t\t\t | | \n\n\n");
}
void print1() //a funtion which when user enter the box number print the value in selected box according to the user selection
{
printf("\a\a\n\t\t\t %c | %c | %c \n\t\t\t___|___|___\n\t\t\t %c | %c | %c \n\t\t\t___|___|___\n\t\t\t %c | %c | %c \n\t\t\t | | \n\n\n\a\a",a[0][0],a[0][1],a[0][2],a[1][0],a[1][1],a[1][2],a[2][0],a[2][1],a[2][2]);
}
void design()
{
// char *pt1,*pt2; //pointers which gets the address of character and pass it to the s1 and s2
// pt1=st.s1; //assigning the pointer 1 to an array object of data structure
// pt2=st.s2; //assigning the pointer 2 to an array object of data structure
name(st.s1,st.s2); //name function declared above is called here
system("cls"); // screen clearing
for ( i=1 ; i<10 ; i++ ) //simple for loop from 1 to 9
{
phir: //reference name for goto statement
toe(); //toe funtion made above is called here
if(i%2==1) //selection statement which select the player's turn. if remainder of any value of i and 2 is 1 then it's player one's turn otherwise it's player 2's turn
{
printf(" %s's Turn : ",st.s1); //if i%2 gives remainder 1 then name of player 1 is used
}
else
{
printf(" %s's Turn : ",st.s2); //if i%2==1 is false then player 2's turn is announced
}
st.ch = ( getche()-48 ); //this gets an integer input from user in box and print it as character
if ( st.ch<0 || st.ch>9 ) //check condition which checks if value enetered by user is greater than 9 or lesser than zero then next block is printed otherwise next block is skipped
{
printf("\n\n\nYou have chosen wrong block...!!!\n\n\n"); //if selected box by user is less than 0 and greater than 9 then it prints that the user has choosen wrong box.
system("pause"); //to pause the program
system("cls"); //to clear the screen
goto phir; //goto statement which goes to phir declared above and check all the condition above again
}
switch(st.ch) //switch selection statement
{
case 1 : // in switch selection statement we use cases for checking conditions
if(a[0][0]=='X' || a[0][0]=='O') //checks value enetered in 1x1 by user
{
filled(); // checks weather the box is filled or not
break; //break statement used to terminate filled funtion
}
if(i%2==1) // player 1 selection...if i%2 gives remainder 1 then it is player 1's turn otherwise player 2's turn
{
a[0][0]='X'; //enter X character in place of integer
print1(); // print the game box which helps the user for selecting box number
win1(); //checks the condition that player 1 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 1
}
else //if above if condition is false this else condition is used
{
a[0][0]='O'; //enter O character in place of integer
print1(); //print the game box which helps the user for selecting box number
win2(); //checks the condition that player 2 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 2
}
break; //break statement for case 1
case 2 : //2nd possibility of value enetered by user
if(a[0][1]=='X' || a[0][1]=='O') //checks value enetered in 1x2 by user
{
filled(); // checks weather the box is filled or not
break; //break statement used to terminate filled funtion
}
if(i%2==1) // player 1 selection...if i%2 gives remainder 1 then it is player 1's turn otherwise player 2's turn
{
a[0][1]='X'; //enter X character in place of integer
print1(); // print the game box which helps the user for selecting box number
win1(); //checks the condition that player 1 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 1
}
else //if above if condition is false this else condition is used
{
a[0][1]='O'; //enter O character in place of integer
print1(); // print the game box which helps the user for selecting box number
win2(); //checks the condition that player 2 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 2
}
break; //break statement for case 2
case 3 : //3rd possibility of value enetered by user
if(a[0][2]=='X' || a[0][2]=='O') //checks value enetered in 1x3 by user
{
filled(); // checks weather the box is filled or not
break; //break statement used to terminate filled funtion
}
if(i%2==1) // player 1 selection...if i%2 gives remainder 1 then it is player 1's turn otherwise player 2's turn
{
a[0][2]='X'; //enter X character in place of integer
print1(); // print the game box which helps the user for selecting box number
win1(); //checks the condition that player 1 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 1
}
else //if above if condition is false this else condition is used
{
a[0][2]='O'; //enter O character in place of integer
print1(); // print the game box which helps the user for selecting box number
win2(); //checks the condition that player 2 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 2
}
break; //break statement for case 3
case 4 : //fourth possibility of value enetered by user
if( a[1][0]=='X' || a[1][0]=='O' ) //checks value enetered in 2x1 by user
{
filled(); // checks weather the box is filled or not
break; //break statement used to terminate filled funtion
}
if (i%2==1) // player 1 selection...if i%2 gives remainder 1 then it is player 1's turn otherwise player 2's turn
{
a[1][0]='X'; //enter X character in place of integer
print1(); // print the game box which helps the user for selecting box number
win1(); //checks the condition that player 1 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 1
}
else //if above if condition is false this else condition is used
{
a[1][0]='O'; //enter O character in place of integer
print1(); // print the game box which helps the user for selecting box number
win2(); //checks the condition that player 2 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 2
}
break; //break statement for case 4
case 5 : //fifth possibility of value enetered by user
if( a[1][1]=='X' || a[1][1]=='O' ) //checks value enetered in 2x2 by user
{
filled(); // checks weather the box is filled or not
break; //break statement used to terminate filled funtion
}
if (i%2==1) // player 1 selection...if i%2 gives remainder 1 then it is player 1's turn otherwise player 2's turn
{
a[1][1]='X'; //enter X character in place of integer
print1(); // print the game box which helps the user for selecting box number
win1(); //checks the condition that player 1 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 1
}
else //if above if condition is false this else condition is used
{
a[1][1]='O'; //enter O character in place of integer
print1(); // print the game box which helps the user for selecting box number
win2(); //checks the condition that player 2 wins or not
system("pause"); //system funtion used to pause
system("cls");
break; //break statement for player 2
}
break; //break statement for case 5
case 6 : //sixth possibility of value enetered by user
if ( a[1][2]=='X' || a[1][2]=='O' ) //checks value enetered in 2x3 by user
{
filled(); // checks weather the box is filled or not
break; //break statement used to terminate filled funtion
}
if (i%2==1) // player 1 selection...if i%2 gives remainder 1 then it is player 1's turn otherwise player 2's turn
{
a[1][2]='X'; //enter X character in place of integer
print1(); // print the game box which helps the user for selecting box number
win1(); //checks the condition that player 1 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 1
}
else //if above if condition is false this else condition is used
{
a[1][2]='O'; //enter O character in place of integer
print1(); // print the game box which helps the user for selecting box number
win2(); //checks the condition that player 2 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 2
}
break; //break statement for case 6
case 7 : //7th possibility of value enetered by user
if ( a[2][0]=='X' || a[2][0]=='O' ) //checks value enetered in 3x1 by user
{
filled(); // checks weather the box is filled or not
break; //break statement used to terminate filled funtion
}
if (i%2==1) // player 1 selection...if i%2 gives remainder 1 then it is player 1's turn otherwise player 2's turn
{
a[2][0]='X'; //enter X character in place of integer
print1(); // print the game box which helps the user for selecting box number
win1(); //checks the condition that player 1 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 1
}
else //if above if condition is false this else condition is used
{
a[2][0]='O'; //enter O character in place of integer
print1(); // print the game box which helps the user for selecting box number
win2(); //checks the condition that player 2 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 2
}
break; //break statement for case 7
case 8 : //8th possibility of value enetered by user
if ( a[2][1]=='X' || a[2][1]=='O' ) //checks value enetered in 3x2 by user
{
filled(); // checks weather the box is filled or not
break; //break statement used to terminate filled funtion
}
if (i%2==1) // player 1 selection...if i%2 gives remainder 1 then it is player 1's turn otherwise player 2's turn
{
a[2][1]='X'; //enter X character in place of integer
print1(); // print the game box which helps the user for selecting box number
win1(); //checks the condition that player 1 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 1
}
else //if above if condition is false this else condition is used
{
a[2][1]='O'; //enter O character in place of integer
print1(); // print the game box which helps the user for selecting box number
win2(); //checks the condition that player 2 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 2
}
break; //break statement for case 8
case 9 : //9th possibility of value enetered by user
if ( a[2][2]=='X' || a[2][2]=='O' ) //checks value enetered in 3x3 by user
{
filled(); // checks weather the box is filled or not
break; //break statement used to terminate filled funtion
}
if (i%2==1) // player 1 selection...if i%2 gives remainder 1 then it is player 1's turn otherwise player 2's turn
{
a[2][2]='X'; //enter X character in place of integer
print1(); // print the game box which helps the user for selecting box number
win1(); //checks the condition that player 1 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 1
}
else //if above if condition is false this else condition is used
{
a[2][2]='O'; //enter O character in place of integer
print1(); // print the game box which helps the user for selecting box number
win2(); //checks the condition that player 2 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 2
}
break; //break statement for case 9
}
}
}
void gameloading()
{
system("cls"); //for clearing screen
int length_of_loading_bar=0,timer=0; //initializing the length of bar for delay and timer for the speed of delaying
printf("\n\n\n\n\n\n\n\n\n\n\t\t\t GaMe iS LoAdInG..."); //Loading indication
printf("\n\t\t\t ");
for(length_of_loading_bar=0;length_of_loading_bar<20;length_of_loading_bar++) //loop for the horizontal length of bar
{
for(timer=0;timer<10000000;timer++); //semi colon is used bcoz we don't want to print anything vertically instead we want to count time...here we are using loop as counter
printf("%c",182); //special symbol askey value 182 is used for 19 times printing of special symbol
}
printf("\n"); //to print on new line
system("cls"); //for clearing screen
}
//Reg.No. DDP-SP14-BSE-005
//Section A
//Assignment # 3
//Submitted to Sir A.K.Shahid
//COMSATS Institute of Information Technology, Lahore, Pakistan
#include<stdio.h> //hearder file for I/O
char a[3][3]={0},i,j; //a 2D array of 3X3 is globally declared and initialized to zero so that it doesn't give any junk value, variable i and j are also globally declared. They are declared globally so that they can be used throughout the program.
void header(); //prototype of header funtion
void name(char *pt1,char *pt2); //prototype of name function
void filled(); //prototype of filled funtion
int condition1(); //prototype of condition1 funtion
int condition2(); //prototype of condition2 funtion
int win1(); //prototype of win1 funtion
int win2(); //prototype of win2 funtion
void toe(); //prototype of toe funtion
void print1(); //prototype of print1 funtion
void design(); //prototype of design funtion
void gameloading(); //prototype of gameloading funtion
struct std //data structure is declared as std
{
int ch; //object of data structure
char s1[100],s2[100]; //character type object declaration in data structure
}st;
int main() //main funtion
{
int menu; //declaring menu variable
header(); //calling header funtion
system("pause"); //to pause screen
again: //pointing for goto statement
gameloading(); //loading funtion is called
printf("\n\n\n\n\n\n\n\n\n\t\t\t1. Start Game\n\n\t\t\t2. Exit \n\t\t\t");
menu=getche()-48; //getting integer value from user
system("cls"); //clearing screen
switch(menu) //switch selection statement is applied on menu
{
case 1: //first case for selection statement
if(menu==1) //if user press 1 then game is started
design(); //design() funtion is called
break; //break statement for case 1
case 2:
if(menu==2) //if user select 2 then game is exited
return 0; //terminating condition
break;
default: //if user select any option except 1 and 2 then default condition is performed
printf("\n\n\nYou have Selected Wrong Option.");
printf("\n\n\n\n\n\n");
system("pause"); //pausing screen
system("cls"); //clearing screen
goto again; //if default condition is performed this statement goes to again pointing value.
break;
}
}
void header() //a function which does not return any value used to show header file in main function
{
printf("\n\t\t\t TIC TAC TOE"); //printf statement is used for printing & \n for next line and \t for tab or space
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
void name(char *pt1,char *pt2) //pointer funtion which gets the name of user and pass the address where it called
{
puts("\n\n\t\t\tEnter Name of Player 1 : "); //asking player 1 to enter name ...puts is used to print string
printf("\n\t\t\t");
gets(pt1); //gets is used to get the string from user
puts("\n\n\t\t\tEnter Name of Player 2 : "); //asking player 2 to enter name
printf("\n\t\t\t");
gets(pt2); //getting name of 2nd player
}
void filled() //a function which tell us about the already filled box
{
condition1(); //it checks the conditions given in funtion condition1() and decrement of i-- is used to identify is the box already filled
i--;
printf("\n\n\n\nOooOpppss This Block is already filled....!!!\n\n\n\n\n"); //this line is printed if the box is already filled
system("pause"); // system pasuse is the system function which is used for a pause and to continue it requires pressing of any key
system("cls"); //this is also a system function which is used for clearing screen
}
int condition1() //a funtion which return integer value if any of the given condition is true...THIS CONDITION IS FOR PLAYER 1
{
if ( a[0][0]=='X' && a[0][1]=='X' && a[0][2]=='X' || a[1][0]=='X' && a[1][1]=='X' && a[1][2]=='X' || a[2][0]=='X' && a[2][1]=='X' && a[2][2]=='X' ) //this condition checks all the horizontal rows values one by one and returns 1 if condition is true for any horizontal row
return 1;
else if ( a[0][0]=='X' && a[1][0]=='X' && a[2][0]=='X' || a[0][1]=='X' && a[1][1]=='X' && a[2][1]=='X' || a[0][2]=='X' && a[1][2]=='X' && a[2][2]=='X' ) //this condition checks all the vertical column values one by one and returns 1 if condition is true for any column row
return 1;
else if ( a[0][0]=='X' && a[1][1]=='X' && a[2][2]=='X') //this condition checks the principle diagonal values and returns 1 if condition is true. AND operator is used because all conditions in that statement should be true in order to return 1
return 1;
else if ( a[0][2]=='X' && a[1][1]=='X' && a[2][0]=='X') //this condition checks the values in diagonal boxes
return 1; //it the above statement is true it returns value 1
}
int condition2() //a funtion which return integer value if any of the given condition is true...THIS CONDITION IS FOR PLAYER 2
{
if ( a[0][0]=='O' && a[0][1]=='O' && a[0][2]=='O' || a[1][0]=='O' && a[1][1]=='O' && a[1][2]=='O' || a[2][0]=='O' && a[2][1]=='O' && a[2][2]=='O' ) //this condition checks all the horizontal rows values one by one and returns 2 if condition is true for any horizontal row
return 2;
else if ( a[0][0]=='O' && a[1][0]=='O' && a[2][0]=='O' || a[0][1]=='O' && a[1][1]=='O' && a[2][1]=='O' || a[0][2]=='O' && a[1][2]=='O' && a[2][2]=='O' ) //this condition checks all the vertical column values one by one and returns 2 if condition is true for any column row
return 2;
else if ( a[0][0]=='O' && a[1][1]=='O' && a[2][2]=='O') //this condition checks the principle diagonal values and returns 2 if condition is true. AND operator is used because all conditions in that statement should be true in order to return 2
return 2;
else if ( a[0][2]=='O' && a[1][1]=='O' && a[2][0]=='O') //this condition checks the values in diagonal boxes
return 2; //it the above statement is true it returns value 2
}
int win1() //a funtion which declares the winning of player 1 and returns 0 if any condition in condition1() is true.
{
if(condition1()==1) //selection statement used to tets the conidition1() function
printf("\n\n\nCongratulations...!!! Player 1 is the WINNER...!!!\n\n"); //winning declaration printing
return 0;
}
int win2() //a funtion which declares the winning of player 2 and returns 0 if any condition in condition2() is true.
{
if(condition2()==2) //selection statement used to tets the conidition2() function
printf("\n\n\nCongratulations...!!! Player 2 is the WINNER...!!!\n\n\n"); //winning declaration printing
return 0;
}
void toe() //void function used which returns nothing. It is just printed to tell the user about the box number for selection
{
printf("\n\t\t\t 1 | 2 | 3 \n\t\t\t___|___|___\n\t\t\t 4 | 5 | 6 \n\t\t\t___|___|___\n\t\t\t 7 | 8 | 9 \n\t\t\t | | \n\n\n");
}
void print1() //a funtion which when user enter the box number print the value in selected box according to the user selection
{
printf("\a\a\n\t\t\t %c | %c | %c \n\t\t\t___|___|___\n\t\t\t %c | %c | %c \n\t\t\t___|___|___\n\t\t\t %c | %c | %c \n\t\t\t | | \n\n\n\a\a",a[0][0],a[0][1],a[0][2],a[1][0],a[1][1],a[1][2],a[2][0],a[2][1],a[2][2]);
}
void design()
{
// char *pt1,*pt2; //pointers which gets the address of character and pass it to the s1 and s2
// pt1=st.s1; //assigning the pointer 1 to an array object of data structure
// pt2=st.s2; //assigning the pointer 2 to an array object of data structure
name(st.s1,st.s2); //name function declared above is called here
system("cls"); // screen clearing
for ( i=1 ; i<10 ; i++ ) //simple for loop from 1 to 9
{
phir: //reference name for goto statement
toe(); //toe funtion made above is called here
if(i%2==1) //selection statement which select the player's turn. if remainder of any value of i and 2 is 1 then it's player one's turn otherwise it's player 2's turn
{
printf(" %s's Turn : ",st.s1); //if i%2 gives remainder 1 then name of player 1 is used
}
else
{
printf(" %s's Turn : ",st.s2); //if i%2==1 is false then player 2's turn is announced
}
st.ch = ( getche()-48 ); //this gets an integer input from user in box and print it as character
if ( st.ch<0 || st.ch>9 ) //check condition which checks if value enetered by user is greater than 9 or lesser than zero then next block is printed otherwise next block is skipped
{
printf("\n\n\nYou have chosen wrong block...!!!\n\n\n"); //if selected box by user is less than 0 and greater than 9 then it prints that the user has choosen wrong box.
system("pause"); //to pause the program
system("cls"); //to clear the screen
goto phir; //goto statement which goes to phir declared above and check all the condition above again
}
switch(st.ch) //switch selection statement
{
case 1 : // in switch selection statement we use cases for checking conditions
if(a[0][0]=='X' || a[0][0]=='O') //checks value enetered in 1x1 by user
{
filled(); // checks weather the box is filled or not
break; //break statement used to terminate filled funtion
}
if(i%2==1) // player 1 selection...if i%2 gives remainder 1 then it is player 1's turn otherwise player 2's turn
{
a[0][0]='X'; //enter X character in place of integer
print1(); // print the game box which helps the user for selecting box number
win1(); //checks the condition that player 1 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 1
}
else //if above if condition is false this else condition is used
{
a[0][0]='O'; //enter O character in place of integer
print1(); //print the game box which helps the user for selecting box number
win2(); //checks the condition that player 2 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 2
}
break; //break statement for case 1
case 2 : //2nd possibility of value enetered by user
if(a[0][1]=='X' || a[0][1]=='O') //checks value enetered in 1x2 by user
{
filled(); // checks weather the box is filled or not
break; //break statement used to terminate filled funtion
}
if(i%2==1) // player 1 selection...if i%2 gives remainder 1 then it is player 1's turn otherwise player 2's turn
{
a[0][1]='X'; //enter X character in place of integer
print1(); // print the game box which helps the user for selecting box number
win1(); //checks the condition that player 1 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 1
}
else //if above if condition is false this else condition is used
{
a[0][1]='O'; //enter O character in place of integer
print1(); // print the game box which helps the user for selecting box number
win2(); //checks the condition that player 2 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 2
}
break; //break statement for case 2
case 3 : //3rd possibility of value enetered by user
if(a[0][2]=='X' || a[0][2]=='O') //checks value enetered in 1x3 by user
{
filled(); // checks weather the box is filled or not
break; //break statement used to terminate filled funtion
}
if(i%2==1) // player 1 selection...if i%2 gives remainder 1 then it is player 1's turn otherwise player 2's turn
{
a[0][2]='X'; //enter X character in place of integer
print1(); // print the game box which helps the user for selecting box number
win1(); //checks the condition that player 1 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 1
}
else //if above if condition is false this else condition is used
{
a[0][2]='O'; //enter O character in place of integer
print1(); // print the game box which helps the user for selecting box number
win2(); //checks the condition that player 2 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 2
}
break; //break statement for case 3
case 4 : //fourth possibility of value enetered by user
if( a[1][0]=='X' || a[1][0]=='O' ) //checks value enetered in 2x1 by user
{
filled(); // checks weather the box is filled or not
break; //break statement used to terminate filled funtion
}
if (i%2==1) // player 1 selection...if i%2 gives remainder 1 then it is player 1's turn otherwise player 2's turn
{
a[1][0]='X'; //enter X character in place of integer
print1(); // print the game box which helps the user for selecting box number
win1(); //checks the condition that player 1 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 1
}
else //if above if condition is false this else condition is used
{
a[1][0]='O'; //enter O character in place of integer
print1(); // print the game box which helps the user for selecting box number
win2(); //checks the condition that player 2 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 2
}
break; //break statement for case 4
case 5 : //fifth possibility of value enetered by user
if( a[1][1]=='X' || a[1][1]=='O' ) //checks value enetered in 2x2 by user
{
filled(); // checks weather the box is filled or not
break; //break statement used to terminate filled funtion
}
if (i%2==1) // player 1 selection...if i%2 gives remainder 1 then it is player 1's turn otherwise player 2's turn
{
a[1][1]='X'; //enter X character in place of integer
print1(); // print the game box which helps the user for selecting box number
win1(); //checks the condition that player 1 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 1
}
else //if above if condition is false this else condition is used
{
a[1][1]='O'; //enter O character in place of integer
print1(); // print the game box which helps the user for selecting box number
win2(); //checks the condition that player 2 wins or not
system("pause"); //system funtion used to pause
system("cls");
break; //break statement for player 2
}
break; //break statement for case 5
case 6 : //sixth possibility of value enetered by user
if ( a[1][2]=='X' || a[1][2]=='O' ) //checks value enetered in 2x3 by user
{
filled(); // checks weather the box is filled or not
break; //break statement used to terminate filled funtion
}
if (i%2==1) // player 1 selection...if i%2 gives remainder 1 then it is player 1's turn otherwise player 2's turn
{
a[1][2]='X'; //enter X character in place of integer
print1(); // print the game box which helps the user for selecting box number
win1(); //checks the condition that player 1 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 1
}
else //if above if condition is false this else condition is used
{
a[1][2]='O'; //enter O character in place of integer
print1(); // print the game box which helps the user for selecting box number
win2(); //checks the condition that player 2 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 2
}
break; //break statement for case 6
case 7 : //7th possibility of value enetered by user
if ( a[2][0]=='X' || a[2][0]=='O' ) //checks value enetered in 3x1 by user
{
filled(); // checks weather the box is filled or not
break; //break statement used to terminate filled funtion
}
if (i%2==1) // player 1 selection...if i%2 gives remainder 1 then it is player 1's turn otherwise player 2's turn
{
a[2][0]='X'; //enter X character in place of integer
print1(); // print the game box which helps the user for selecting box number
win1(); //checks the condition that player 1 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 1
}
else //if above if condition is false this else condition is used
{
a[2][0]='O'; //enter O character in place of integer
print1(); // print the game box which helps the user for selecting box number
win2(); //checks the condition that player 2 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 2
}
break; //break statement for case 7
case 8 : //8th possibility of value enetered by user
if ( a[2][1]=='X' || a[2][1]=='O' ) //checks value enetered in 3x2 by user
{
filled(); // checks weather the box is filled or not
break; //break statement used to terminate filled funtion
}
if (i%2==1) // player 1 selection...if i%2 gives remainder 1 then it is player 1's turn otherwise player 2's turn
{
a[2][1]='X'; //enter X character in place of integer
print1(); // print the game box which helps the user for selecting box number
win1(); //checks the condition that player 1 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 1
}
else //if above if condition is false this else condition is used
{
a[2][1]='O'; //enter O character in place of integer
print1(); // print the game box which helps the user for selecting box number
win2(); //checks the condition that player 2 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 2
}
break; //break statement for case 8
case 9 : //9th possibility of value enetered by user
if ( a[2][2]=='X' || a[2][2]=='O' ) //checks value enetered in 3x3 by user
{
filled(); // checks weather the box is filled or not
break; //break statement used to terminate filled funtion
}
if (i%2==1) // player 1 selection...if i%2 gives remainder 1 then it is player 1's turn otherwise player 2's turn
{
a[2][2]='X'; //enter X character in place of integer
print1(); // print the game box which helps the user for selecting box number
win1(); //checks the condition that player 1 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 1
}
else //if above if condition is false this else condition is used
{
a[2][2]='O'; //enter O character in place of integer
print1(); // print the game box which helps the user for selecting box number
win2(); //checks the condition that player 2 wins or not
system("pause"); //system funtion used to pause
system("cls"); //for clearing screen
break; //break statement for player 2
}
break; //break statement for case 9
}
}
}
void gameloading()
{
system("cls"); //for clearing screen
int length_of_loading_bar=0,timer=0; //initializing the length of bar for delay and timer for the speed of delaying
printf("\n\n\n\n\n\n\n\n\n\n\t\t\t GaMe iS LoAdInG..."); //Loading indication
printf("\n\t\t\t ");
for(length_of_loading_bar=0;length_of_loading_bar<20;length_of_loading_bar++) //loop for the horizontal length of bar
{
for(timer=0;timer<10000000;timer++); //semi colon is used bcoz we don't want to print anything vertically instead we want to count time...here we are using loop as counter
printf("%c",182); //special symbol askey value 182 is used for 19 times printing of special symbol
}
printf("\n"); //to print on new line
system("cls"); //for clearing screen
}
Subscribe to:
Posts (Atom)