Course on C Programming

From WikiEducator
(Redirected from MyHome)
Jump to: navigation, search

MyHome | MyContent | Programing Exercise | Check Your Solution

Introduction

C Programming is one of the basic Programming Language for Computer Science. Learning this language well may lead you to develop your skills relating to problem solving using computers. It is to be mentioned here, that problem solving using computers is a three stage process - The first stage will require you to solve the problem itself by generic means. The second stage will be to develop an algorithm that are closer to computer languages and third is coding and debugging the program.

This course is an attempt to introduce you to all the three basic aspects.

Best wishes....

Objectives

The basic objectives of the course are:

  • Explain the precocess of problem solving using computers
  • aprreciate the process of development of program
  • create C programs for various problems

Let us Start

A Sample Program in C Language:

/* ----Documentation Area---- */ Why Documentation Area?
/*This Program calculates the Bigger of the two positive integers in the range 0 to 9999. The Program terminates when a user does not want to enter more input values. The program makes sure that user input values are in the given Range. The program uses a function big that returns the bigger value.*/
/* ----Documentation Area ENDS---- */
/* ----Preprocessor and Declarations Area---- */ Why Preprocessor and Declarations Area?


#include<stdio.h>
#define MAXINT 9999
#define MININT 0
#define TRUE 1
#define FALSE 0
/* We do not have any Global variable in this Program */ What are Global Variables?
int findbig(int, int); //This is a function prototype.
/* ----Preprocessor and Declarations Area ENDS---- */
/* -----The main Function. A program can have ONLY ONE main() function in C ---- */
int main ()
{
// variable declarations
int n1, n2, big; // n1 stores the first input and n2 stores the second input.
char ans='y';
signed char inrange = TRUE; //
// The Outer DO WHILE LOOP it allows to loop in the Program for more input values
do {
/* The following do - while loop asks the user an input in the Defined Range, prompts him/her to enter the correct input till the user enters the correct input */
do {
printf("\nPlease input the First Positive Number in the range %d and %d: ", MININT,MAXINT);
scanf("%d",&n1);
inrange = TRUE;
if ( n1 < MININT || n1 > MAXINT)
{
printf("Wrong Input ... Please try again..");
inrange = FALSE;
}
} while (inrange == FALSE);


/*The do -while loop for second input */
do {
printf("\nPlease input the Second Positive Number in the range %d and %d :”, MININT, MAXINT);
scanf("%d",&n2);
inrange = TRUE;
if ( n2 < MININT || n2 > MAXINT)
{
printf("Wrong Input ... Please try again..");
inrange = FALSE;
}
} while (inrange == FALSE);
/* Now you can be sure that both the input values (in n1 and n2) are in the defined range so we can continue with the rest of the program. Now we will use the function findbig to return the bigger value*/
big = findbig(n1,n2); // function call to findbig function
printf("\nThe bigger value of %d and %d is %d", n1, n2, big);
printf("\n\nDo you want to have more input values, y for yes ? ");
scanf("%s", &ans);
} while (ans == 'y' || ans == 'Y' );// Outer DO - WHILE loop ends
} // main() ends here.
// You can define the functions used by the program NOW
/* This function finds the bigger of the two values passed to this function. It returns the bigger value*/
int findbig(int first, int second)
{
if (first >= second) return first;
else return second;
}

Explanation of the Program as above

Before writing a Program it is a good idea to write about the program. You may also want to write the algorithm of the proposed program, its constraints etc. You may write all these information in the Documentation area of the program with the help of comment lines. (The information enclosed in /* and */ are called comments.) The comment lines are not compiled. Documentation is very useful as it helps you and other programmers to understand the logic of the program at any point of time.

Although you may not be using program linkages other than including the header files, yet you should know that you can establish linkages to other files in a C program. These linkage statements provide instructions to the compiler to link to executable functions from the system libraries.

In the preprocessor and declaration area you may define all symbolic constants and function prototypes. Symbolic constants help in program readability and maintenance. For example, in the example program above instead of using 9999 you are using MAXINT which indicates highest integer value allowed in the program. Thus, the use of MAXINT in comparison gives you better readability. Please also note that in case you want to change the MAXINT value to 1000, then you just need to do the change at the top of the program and recompile the program.

There are some variables that may be used by all the functions of a program. These are called global variables. Global variables are declared in the Global declaration are that is outside of all the functions. In general, a good program should NOT have many global variables.

You must have ONLY one main() function in every C program. The execution of a C program starts from this function. The main function contains the declarations and executable statements. C Program requires you to declare all the variable that you want to use in the program statements. As you know each declaration and statement in C is terminated by a semicolon (;).

A C Program in addition to main function may consist of many other functions. These functions can be used in the main function for performing some tasks. More about these functions will be discussed later in this Unit.

/*Documentation Area
This is the Documentation about the Program What it does etc. For example,
"This Program calculates the Bigger of the two positive integers in the range 0 to 9999. The Program terminates when a user does not want to enter more input values. The program makes sure that user input values are in the given Range. The program uses a function big that returns the bigger value.
The information enclosed in /* and */ are called comments. The comments are not compiled.
Documentation Ends */


/* Preprocessor and Declarations Area
This area is used to define preprocessor statements, such as including the header files and defining the symbolic names for values. You can also define function prototypes here */
The The header files containing declaration of standard input output functions used in C

Symbolic Constants – very useful for better documentation and in program maintenance Function Prototype – declares the requirement of a function and its type in the beginning itself

#include<stdio.h>
#define MAXINT 9999
#define MININT 0
#define TRUE 1
#define FALSE 0

int findbig(int, int); /* This is a function prototype, informing the compiler that the function accepts two integer values and returns an integer value. You will find details about this function later in the program where it is defined.*/

/* Preprocessor and Declarations Area ENDS*/

/* We do not have any Global variable in this Program */

/* The main function. A program can have ONLY ONE main() function in C */

int main () {

    // variable declarations
    int n1, n2, big; 	// n1 stores the first input and n2 stores the second input.
    char ans='y';
    signed char inrange = TRUE; //
  

// The Outer DO WHILE LOOP it allows to loop in the Program for more input values do {

/* The following do - while loop asks the user an input in the Defined Range, prompts him/her to enter the correct input till the user enters the correct input */

    do {

printf("\nPlease input the First Positive Number in the range %d and %d: ", MININT,MAXINT);

       	scanf("%d",&n1);
       	inrange = TRUE;
       	if ( n1 < MININT || n1 > MAXINT) 
          		{
              	printf("Wrong Input ... Please try again..");
              	inrange = FALSE;
         	}
     } 	while (inrange == FALSE);


/*The do -while loop for second input */

     do {

printf("\nPlease input the Second Positive Number in the range %d and %d :”, MININT, MAXINT);

       	scanf("%d",&n2);
       	inrange = TRUE;
       	if ( n2 < MININT || n2 > MAXINT) 
          	{
              printf("Wrong Input ... Please try again..");
              inrange = FALSE;
         	}
       } 	while (inrange == FALSE);    

/* Now you can be sure that both the input values (in n1 and n2) are in the defined range so we can continue with the rest of the program. Now we will use the function findbig to return the bigger value*/

       	big = findbig(n1,n2); // function call to findbig function

printf("\nThe bigger value of %d and %d is %d", n1, n2, big);

printf("\n\nDo you want to have more input values, y for yes ? ");

    	scanf("%s", &ans);
 

} while (ans == 'y' || ans == 'Y' );// Outer DO - WHILE loop ends

} // main() ends here.

// You can define the functions used by the program NOW /* This function finds the bigger of the two values passed to this function. It returns the bigger value*/

int findbig(int first, int second) { if (first >= second) return first;

else return second;

}

Before writing a Program it is a good idea to write about the program. You may also want to write the algorithm of the proposed program, its constraints etc. You may write all these information in the Documentation area of the program with the help of comment lines. Program documentation is very useful as it will help you and other programmers to understand the logic of the program at any point of time.

Although you may not be using program linkages other than including the header files, yet you should know that you can establish linkages to other files in a C program. These linkage statements provide instructions to the compiler to link to executable functions from the system libraries.

In the preprocessor and declaration area you may define all symbolic constants and function prototypes. Symbolic constants help in program readability and maintenance. For example, in the example program above instead of using 9999 you are using MAXINT which indicates highest integer value allowed in the program. Thus, the use of MAXINT in comparison gives you better readability. Please also note that in case you want to change the MAXINT value to 1000, then you just need to do the change at the top of the program and recompile the program.

There are some variables that may be used by all the functions of a program. These are called global variables. Global variables are declared in the Global declaration are that is outside of all the functions. In general, a good program should NOT have many global variables.

You must have ONLY one main() function in every C program. The execution of a C program starts from this function. The main function contains the declarations and executable statements. C Program requires you to declare all the variable that you want to use in the program statements. As you know each declaration and statement in C is terminated by a semicolon (;).

A C Program in addition to main function may consist of many other functions. These functions can be used in the main function for performing some tasks. More about these functions will be discussed later in this Unit.



Before you start doing anything let us do a very simple assignment:


Icon assess.gif

Assignment

List the steps that you perform to solve the following problems:
  1. To find if the given number is a Prime Number
  2. Assume that List contains numbers such as 58, 28, 20, 30, 45, 39, 30
    • Find the location for number 45 in the list
    • Find the location of number 100 in the list