Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

Tic-Tac Toe Game using C Programming

Posted by Tushar Bedekar

/* A simple Tic Tac Toe game. */

#include <stdio.h>
#include <stdlib.h>

char matrix[3][3];                   /* the tic tac toe matrix */       
char check(void);
void init_matrix(void);
void get_player_move(void);
void get_computer_move(void);
void disp_matrix(void);
int main(void)
{
char done;
printf("This is the game of Tic Tac Toe.\n");
printf("You will be playing  against the computer.\n");
done = ' ';
init_matrix();
do{
disp_matrix();
get_player_move();
done = check();                                                               /* see if winner */
if(done!= ' ') break;                                                       /* winner!*/
get_computer_move();
done = check();                                      /* see if winner */
} while(done== ' ');
if(done=='X') printf("You won!\n");
else printf("I won!!!!\n");
disp_matrix();                                          /* show final positions */
return 0;
}

/* Initialize the matrix. */

void init_matrix(void)
{
int i, j;
for(i=0; i<3; i++)
for(j=0; j<3; j++) matrix[i][j] = ' ';
}

/* Get a player's move. */

void get_player_move(void)
{
int x, y;
printf("Enter X,Y coordinates for your move: ");
scanf("%d%*c%d", &x, &y);
x--; y--;
if(matrix[x][y]!= ' '){
printf("Invalid move, try again.\n");
get_player_move();
}
else matrix[x][y] = 'X';
}

/* Get a move from the computer. */

void get_computer_move(void)
{
int i, j;
for(i=0; i<3; i++){
for(j=0; j<3; j++)
if(matrix[i][j]==' ') break;
if(matrix[i][j]==' ') break;
}
if(i*j==9) {
printf("draw\n");
exit(0);
}
else
matrix[i][j] = 'O';
}

/* Display the matrix on the screen. */

void disp_matrix(void)
{
int t;
for(t=0; t<3; t++) {
printf(" %c | %c | %c ",matrix[t][0],
matrix[t][1], matrix [t][2]);
if(t!=2) printf("\n---|---|---\n");
}
printf("\n");
}

/* See if there is a winner. */

char check(void)
{
int i;
for(i=0; i<3; i++)                                                    /* check rows */
if(matrix[i][0]==matrix[i][1] &&
matrix[i][0]==matrix[i][2]) return matrix[i][0];
for(i=0; i<3; i++)                                                   /* check columns */
if(matrix[0][i]==matrix[1][i] && matrix[0][i]==matrix[2][i])
    return matrix[0][i];
/* test diagonals */
if(matrix[0][0]==matrix[1][1] &&
matrix[1][1]==matrix[2][2])
return matrix[0][0];
if(matrix[0][2]==matrix[1][1] &&
matrix[1][1]==matrix[2][0])
return matrix[0][2];
return ' ';
}


Download Game:-

                                                           


Read More

INTERVIEW QUESTIONS IN "C PROGRAMMING "LANGUAGE

Posted by Tushar Bedekar


TABLE OF CONTENTS

CHAPTER 1: Variables & Control Flow

1. What is the difference between declaring a variable and defining a variable? 
2. What is a static variable? 
3. What is a register variable? 
4. Where is an auto variable stored? 
5. What is scope & storage allocation of extern and global variables? 
6. What is scope & storage allocation of register, static and local variables?
7. What are storage memory, default value, scope and life of Automatic and Register storage class? 
8. What are storage memory, default value, scope and life of Static and External storage class? 
9. What is the difference between 'break' and 'continue' statements?
10. What is the difference between 'for' and 'while' loops?

solution:-Download link


CHAPTER 2: Operators, Constants & Structures


1. Which bitwise operator is suitable for checking whether a particular bit is ON or OFF? 
2. Which bitwise operator is suitable for turning OFF a particular bit in a number? 
3. What is equivalent of multiplying an unsigned int by 2: left shift of number by 1 or right shift of number by
1? 
4. What is an Enumeration Constant?
5. What is a structure? 
6. What are the differences between a structure and a union? 
7. What are the advantages of unions? 
8. How can typedef be to define a type of structure? 
9. Write a program that returns 3 numbers from a function using a structure. 
10. In code snippet below: 
struct Date {
 int yr;
 int day;
 int month;
} date1,date2;
date1.yr = 2004;
date1.day = 4;
date1.month = 12;
Write a function that assigns values to date2. Arguments to the function must be pointers to the
structure, Date and integer variables date, month, year. 

solution:-Download link

CHAPTER 3: Functions


1. What is the purpose of main() function? 
2. Explain command line arguments of main function? 
3. What are header files? Are functions declared or defined in header files ? 
4. What are the differences between formal arguments and actual arguments of a function? 
5. What is pass by value in functions? 
6. What is pass by reference in functions? 
7. What are the differences between getchar() and scanf() functions for reading strings? 
8. Out of the functions fgets() and gets(), which one is safer to use and why? 
9. What is the difference between the functions strdup() and strcpy()? 

solution:-coming-soon


CHAPTER 4: Pointers


1. What is a pointer in C? 
2. What are the advantages of using pointers? 
3. What are the differences between malloc() and calloc()? 
4. How to use realloc() to dynamically increase size of an already allocated array? 
5. What is the equivalent pointer expression for referring an element a[i][j][k][l], in a four dimensional array?
6. Declare an array of three function pointers where each function receives two integers and returns float. 
7. Explain the variable assignment in the declaration
int *(*p[10])(char *, char *);
8. What is the value of 
sizeof(a) /sizeof(char *)
in a code snippet:
char *a[4]={"sridhar","raghava","shashi","srikanth"};
9. (i) What are the differences between the C statements below: 
char *str = "Hello";
char arr[] = "Hello";
(ii) Whether following statements get complied or not? Explain each statement. 
arr++;
*(arr + 1) = 's';
printf("%s",arr);

solution:-coming-soon


CHAPTER 5: Programs


1. Write a program to find factorial of the given number. 
2. Write a program to check whether the given number is even or odd. 
3. Write a program to swap two numbers using a temporary variable. 
4. Write a program to swap two numbers without using a temporary variable. 
5. Write a program to swap two numbers using bitwise operators. 
6. Write a program to find the greatest of three numbers. 
7. Write a program to find the greatest among ten numbers. 
8. Write a program to check whether the given number is a prime. 
9. Write a program to check whether the given number is a palindromic number. 
10.Write a program to check whether the given string is a palindrome. 
11.Write a program to generate the Fibonacci series. 
12.Write a program to print "Hello World" without using semicolon anywhere in the code. 
13.Write a program to print a semicolon without using a semicolon anywhere in the code. 
14.Write a program to compare two strings without using strcmp() function. 
15.Write a program to concatenate two strings without using strcat() function. 
16.Write a program to delete a specified line from a text file. 
17.Write a program to replace a specified line in a text file. 
18.Write a program to find the number of lines in a text file. 
19.Write a C program which asks the user for a number between 1 to 9 and shows the number. If the user inputs a number out of the specified range, the program should show an error and prompt the user for a valid input. 
20.Write a program to display the multiplication table of a given number.

solution:-coming-soon
Read More

Try If You Can

Posted by Tushar Bedekar




  • Note:- You Can Also Mail Your Responses at Contact us
  • The responses May be Generated either using GCC or Turbo C compiler.It does`t matter but the correctness of responses is required.
  • Download Turbo C HERE
  • Online Compiler HERE

Read More

C Program to Print 1-10 numbers without using Conditional Loops

Posted by Tushar Bedekar
Print 1-10 numbers without using Conditional Loop i.e without using
  • for Loop
  • while Loop
  • do-while Loop


#include<stdio.h>

void printNumber(int value) {
   int i;
   printf("%d\n", value);          //The Following programme is written using the concept of recursion
   i = value + 1;

   if (i > 10)
      return;
   printNumber(i);
}

void main() {
   printNumber(1);
}
Read More

Algorithms and Flowchart in C Programming

Posted by Tushar Bedekar
                              

                                           Algorithms:-
A sequential solution of any program that written in human language,called algorithm.
Algorithm is first step of the solution process, after the analysis of problem, programmer write the algorithm of that problem.
Example of Algorithms:
Q. Write a algorithms to find out number is odd or even?
Ans. 
step 1 : start
step 2 : input number
step 3 : rem=number mod 2
step 4 : if rem=0 then
               print "number even"
           else
               print "number odd"
           end if
step 5 : stop



                                            Flowchart:-

1. Graphical representation of any program is called flowchart.

2. There are some standard graphics that are used in flowchart as following:








Q. Make a flowchart to input temperature, if temperature is less than 32 then print "below freezing" otherwise print  "above freezing"?
Ans.










Read More

Fibonacci series by Recursion in c

Posted by Tushar Bedekar

Introduction:-

http://www.allaboutcomputing.net/
Recursion is the process of repeating items in a self-similar way. In other words recursion is a process in which a same set of a statement/expressions are executed large no of times in such a manner that after the execution of a certain set of a statement/expression, again the same set of a statement/expressions are being executed until the condition is satisfied. Generally this kind of a process takes place in the in functions in which a function calls itself. The following is the syntax of the recursive function:-
void recursion()
{
   recursion(); /* function calls itself */
}

int main()
{
   recursion();
}
http://www.allaboutcomputing.net/Actually most of the programming languages support recursion an C programming language is one of them i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go in infinite loop.
Recursive function are very useful to solve many mathematical problems like to calculate factorial of a number, generating Fibonacci series, etc.

Program me:-


/* Fibonacci by Recursion */
#include<stdio.h>
http://www.allaboutcomputing.net/
int fib(int); int main() { printf("Type any value : "); printf("\nNth value: %d",fib(getche()-'0')); return 0; } int fib(int n) { if(n<=1) return n; return(fib(n-1)+fib(n-2)); }
Read More

C Programming to Explain Type Casting

Posted by Tushar Bedekar

Introduction:-

Type casting is a way to convert a variable from one data type to another data type. For example, if you want to store a long value into a simple integer then you can type cast long to int. You can convert values from one type to another explicitly using the cast operator as follows:


Program me:-

#include< stdio.h>
#include< conio.h>
int main( )
{
int n,i,j,fact;
float sum=0;
clrscr();
for(i=1; i<=7; i++) { fact=1; for(j=i; j>=1; j--)                       // to find the factorial
fact=fact*j;

sum=sum+(float)i/fact;
}
printf("\nSum : %f",sum);
getch();
return 0;
}

Read More

Modifiers in C Programming Language

Posted by Tushar Bedekar
C Programming Modifier
As We Have discussed in the last article about C Programming   the data types have some modifiers preceding them. The use of these modifiers changes the meaning of the base type.

The memory in the computer is organized in terms of units called bytes. One  byte consists of 8 bits and bit is a smallest unit of memory.

Need of Data Modifiers:

The Need of modifiers can be understood by the help of following.Let us take an example of a Program where we need to input the “Salary” of “Employees” in a team. This program will accept the salary as an input from user and then calculate the Income Tax of that user. We use “int” to store the Salary of the employee as we are assuming that the salary will be in “Whole Numbers”.
C Programming Modifier

An integer data type takes 2 Bytes of Memory and we are aware that the Salary of any of the employee can not be “Negative”. We are using “2 Bytes” to store the memory of an Employee and we can easily save 1 Byte over there by removing the “Signed Part” in the integer. This positive value can easily be stored in “1 Bye Int” This leads us to the user of Data Type Modifiers.

Types of Data Modifiers in C:

Basically c programming language consist of 3 basic modifiers.The modifiers are listed below :


Signed Type Modifier:



All data types are “signed” by default. Signed Data Modifier implies that the data type variable can store positive values as well as negative values.


For example, if we need to declare a variable to store temperature, it can be negative as well as positive.

C Programming Modifier
signed int temperature;


Or


int temperature;



Unsigned Type Modifier:



If we need to change the data type so that it can only store only store positive values, “unsigned” data modifier is used.


For example, if we need to declare a variable to store the salary of an employee as explained above, we will use “Unsigned” Data Qualifier here.


unsigned int salary;


Long Type Modifier:


Sometimes while coding a program, we need to increase the Storage Capacity of a variable so that it can store values higher than its maximum limit which is there as default. In such situations or programs, we need to make use of the “long” data type qualifier. “long” type modifier doubles the “length” of the data type when used along with it.

C Programming Modifier
For example, if we need to store the “annual turnover” of a company in a variable, we will make us of this type qualifier.


long int turnover;


This variable will take 4 Bytes in memory.



Short Type Modifier:



A “short” type modifier does just the opposite of “long”. If one is not expecting to see high range values in a program and the values are both positive & negative.

C Programming Modifier
For example, if we need to store the “age” of a student in a variable, we will make use of this type qualifier as we are aware that this value is not going to be very high.


short int age;


This variable will consume only 1 Byte in memory.



Example

We can apply the above mentioned modifiers to integer (int) and character (char) base types.


1. Integer Type Modifiers:



* We can use all the above mentioned Type Modifiers for int data type in C Language

* Short int and long int are also termed as short and long respectively.
* Int is signed & unsigned by default.


2. Character Type Modifiers:



* Variables of type char consume 1 byte in memory.

* Char can be signed or unsigned only.
* They have a range of -128 to 127 and 0 to 255 for signed & unsigned respectively.


3. Float Type & Double Type Modifier:



There are 3 types of float type modifiers as given below:

C Programming Modifier


* float

* double
* long double

Double is same as long float. Float type occupies 4 bytes of memory. Type double occupies 8 bytes. Long double occupies 10 bytes. The exception to this is long double, which modifies the size of the double data type to 10 bytes. Please note that in some compilers this has no effect.



Note: We may use long modifier with double data type but it cannot be used with float, i.e. long double is allowed but long float is not allowed because long float is equal to double.


For More Info Download:-

                                                                                                                                                                       
Tushar
                                                           
Read More

C Programming Data Type

Posted by Tushar Bedekar
In C, variable(data) should be declared before it can be used in program. Data types are the keywords, which are used for assigning a type to a variable.In other words we can define the data type as a keyword which specifies the type of the data to be processed during the execution of the program me. 

Data types determine the following:

• Range of the data
• Type of the data stored
• Number of bytes it occupies in memory





C supports following data types:

• int – occupies 2 (16-bit compiler) or 4 bytes (32-bit compiler) of memory
• float – occupies 4 byes of memory
• double – occupies 8 bytes of memory
• char – occupies 1 byte of memory

Integer Data Type:-

int is used to define integer numbers. It occupies 2 bytes of memory (16-bit compiler).Keyword int is used for declaring the variable with integer type. For example:
The size of int is either 2 bytes(In older PC's) or 4 bytes. If you consider an integer having size of 4 byte( equal to 32 bits), it can take 232 distinct states as: -231,-231+1, ...,-2, -1, 0, 1, 2, ..., 231-2, 231-1
Similarly, int of 2 bytes, it can take 216 distinct states from -215 to 215-1. If you try to store larger number than 231-1, i.e,+2147483647 and smaller number than -231, i.e, -2147483648,  program will not run correctly.

Float Data Type:-

float is used to define floating type numbers. It occupies 4 bytes of memory
(16-bit compiler).Variables of floating types can hold real values(numbers) such as: 2.34, -9.382 etc. Keywords either float or double is used for declaring floating type variable. For example:

Double Data Type:-

double is used to store big floating point numbers. It reserves 8 bytes of memory (16-bit compiler)

Difference between float and double:-

Generally the size of float(Single precision float data type) is 4 bytes and that of double(Double precision float data type) is 8 bytes. Floating point variables has a precision of 6 digits whereas the the precision of double is 14 digits.
Note: Precision describes the number of significant decimal places that a floating values carries.

Char Data Type:-

char is used to store characters. It occupies 1 byte of memory (16-bit compiler).The size of char is 1 byte. The character data type consists of ASCII characters. Each character is given a specific value. For example:

Summary:

Following table is applicable for 16-bit compiler.

Read More

Literals in C Programming

Posted by Tushar Bedekar
The term literal constant or literal refers to a value that occurs in a program and cannot be changed. The C language uses the term constant in place of the noun literal. The adjective literal adds to the concept of a constant the notion that we can speak of it only in terms of its value. A literal constant is nonaddressable, which means that its value is stored somewhere in memory, but we have no means of accessing that address.

Every literal has a value and a data type. The value of any literal does not change while the program runs and must be in the range of representable values for its type. The following are the available types of literals:


Types:-


  • Boolean
  • Integer
  • Character
  • Floating-point
  • String
  • C Compound literal
  • Vector literal
According to Wikipedia  a literal is a notation for representing a fixed value in source code. Almost all programming languages have notations for atomic values such as integers,floating-point numbers, and strings, and usually for booleans and characters; some also have notations for elements of enumerated types and compound values such as arrays,records, and objects. An anonymous function is a literal for the function type.

In contrast to literals, variables or constants are symbols that can take on one of a class of fixed values, the constant being constrained not to change. Literals are often used to initialize variables, for example, in the following, 1 is an integer literal and the three letter string in "cat" is a string literal:
Read More
back to top