Thursday, March 8, 2018

Writing an algorithm

Writing an algorithm

An algorithm can be written in English, like sentences and using mathematical formulas. Sometimes algorithm written in English like language is Pseudo code.

Examples

1)   Finding the average of three numbers

1.   Let a,b,c are three integers

2.   Let d is float

3.   Display the message “Enter any three integers:”

4.   Read three integers and stores in a,b,c

5.   Compute the d = (a+b+c)/3.0

6.   Display “The avg is:” , d

7.   End.

Wednesday, March 7, 2018

Finding The Maximum And Minimum Of Given Set Of Numbers




/* C program to find maximum and minimum element in array */

#include <stdio.h>

int main()

{

int arr[100];

int i, max, min, size;

/* Input size of the array */

printf("Enter size of the array: ");

scanf("%d", &size);

/* Input array elements */

printf("Enter elements in the array: ");

for(i=0; i<size; i++)

{

scanf("%d", &arr[i]);

}

/* Assume first element as maximum and minimum */

max = arr[0];

min = arr[0];

/* Finds maximum and minimum in all array elements. */

for(i=1; i<size; i++)

{

/* If current element of array is greater than max */

if(arr[i]>max)

{

max = arr[i];

}

/* If current element of array is smaller than min */

if(arr[i]<min)

{

min = arr[i];

}

}

/* Print maximum and minimum element */

printf("Maximum element = %d\n", max);

printf("Minimum element = %d", min);

return 0;

}
OUTPUT

Output
Finding The Maximum And Minimum Of Given Set Of Numbers
















Algorithm / Pseudo Code

Algorithm / Pseudo Code

Algorithm is a method of representing the step by step logical procedure for solving a problem. It is a tool for finding the logic of a problem.

Algorithm Properties:

1.   Finiteness: an algorithm must terminate in a finite number of steps.

2.   Definiteness: Each step of an algorithm must be clear and easy to understand (unambiguous).

3.   Effectiveness: Each step must be effective, in the sense that should be primitive (easily convertible to program).

4.   Generality: The algorithm must be complete in itself so that it can be used to solve all problems of a specific type for any input data.

5.   Input / Output: Each algorithm must take zero, one or more quantities as input data and produce one or more output values.

Tuesday, March 6, 2018

Program To Print Half Pyramid Using  *.






/* C Program to print half pyramid using  */

#include <stdio.h>

int main()

{

int p, k, number;

printf("Enter number of rows :");

scanf("%d",&number);

for(p=0; p<=number;p++)

{

for(k=0; k<p; k++)

{

printf("  *");

}

printf("\n");

}

return 0;

}

Output
Program To Print Half Pyramid Using  *.

Program Development Method

Program Development Method

1.   Specifying and analyzing the problem statement.

2.   Designing an Algorithm

3.   Coding and Implementation

4.   Debugging

5.   Testing and validating

6.   Documentation and maintenance

Specifying and analyzing the problem statement: the problem which has to be implemented into a program must be thoroughly understood before the program is written. Problem must be analyzed to determine the input and output requirements of the program. A problem statement is created with these specifications.

Designing an Algorithm: with the problem statement obtained in the previous step, various methods available for obtaining the required solution are analyzed and the best method is designed into algorithm.

Coding and implementation: the actual problem is written in the required programming language with the help of information depicted in flow charts and algorithms.

Debugging: there is a possibility of occurrence of errors in programs. These errors must be removed to ensure proper working of programs. Hence solving the program without errors is known as debugging.

Types of errors that may occur in the program are:

a)   Syntactic Errors(Compilation Errors): These errors occur due to the usage of wrong syntax for the statements.

b)   Runtime Errors: These errors are determined at the execution time of the program.


c)   Logical Errors: These errors occur due to incorrect usage of the instructions in the program.

Testing and Validating: Testing and Validation is performed to check whether the program is producing correct results or not for different values according to user requirement.

Documentation and Maintenance: Documentation is the process of collecting, organizing and maintaining, in written the complete information of the program for future references. Maintenance is the process of upgrading the program according to the changing requirements.

Monday, March 5, 2018

Program To Print Pascal Triangle Up To N Rows.


 

/* C program to print Pascal triangle up to n rows */

#include <stdio.h>

/* Function definition */

long long fact(int n);

int main()

{

int n, k, num, i;

long long term;

/* Input number of rows */

printf("Enter number of rows : ");

scanf("%d", &num);

for(n=0; n<num; n++)

{

/* Prints 3 spaces */

for(i=n; i<=num; i++)

printf("%3c", ' ');

/* Generate term for current row */

for(k=0; k<=n; k++)

{

term = fact(n) / (fact(k) * fact(n-k));

printf("%6lld", term);

}

printf("\n");

}

return 0;

}

/* Function to calculate factorial */

long long fact(int n)

{

long long factorial = 1ll;

while(n>=1)

{

factorial *= n;

n--;

}

return factorial;

}



Output
Program To Print Pascal Triangle Up To N Rows.

Creating and Running Programs

Creating and Running Programs

There are four steps in this process.

1.   Writing and editing the program using Text editor (source code).

2.   Compile the program using any C compiler.(.bak file)

3.   Linking the program with the required library modules(object file)

4.   Executing the program. (.Exe file)

Creating and Editing a C Program in C Programming Language compiler:

Writing or creating and editing source program is a first step in c language. Source code is written in c programming language according to the type of problem or requirement, in any text editor.

Saving C Program in C Programming Language: Source code is saved on the secondary storage. Source code is saved as text file. The extension of file must be ".c". Example the file name is "learn c programming language.c"

Compiling C program in C Programming Language: Computer does not understand c programming language. It understands only 0 and 1 means machine language. So c programming language code is converted into machine language. The process of converting source code in to machine code is called compiling. Compiler is a program that compiles source code. Compiler also detects errors in


source program. If compiling is successful source program is converted into object program. Object program is saved on disk. The extension of file is ".obj"

Linking in C programming Language: There are many built in functions available in c programming language. These functions are also called library functions. These functions are stored in different header files.

Loading program: The process of transferring a program from secondary storage to main memory for execution is called loading a program. A program called loader does loading.

Executing program: Execution is the last step. In this step program starts execution. Its instructions start working and output of the program display on the screen.

Life Articles And News:

Life Articles And News: