Sunteți pe pagina 1din 13

Definition of Pseudocode

 Definition: Pseudocode is an informal way of programming description that does not


require any strict programming language syntax or underlying technology considerations.
It is used for creating an outline or a rough draft of a program. Pseudocode summarizes a
program’s flow, but excludes underlying details. System designers write pseudocode to
ensure that programmers understand a software project's requirements and align code
accordingly.

 Description: Pseudocode is not an actual programming language. So it cannot be


compiled into an executable program. It uses short terms or simple English language
syntaxes to write code for programs before it is actually converted into a specific
programming language. This is done to identify top level flow errors, and understand the
programming data flows that the final program is going to use. This definitely helps save
time during actual programming as conceptual errors have been already corrected.
Firstly, program description and functionality is gathered and then pseudocode is used to
create statements to achieve the required results for a program. Detailed pseudocode is
inspected and verified by the designer’s team or programmers to match design
specifications. Catching errors or wrong program flow at the pseudocode stage is
beneficial for development as it is less costly than catching them later. Once the
pseudocode is accepted by the team, it is rewritten using the vocabulary and syntax of a
programming language. The purpose of using pseudocode is an efficient key principle of
an algorithm. It is used in planning an algorithm with sketching out the structure of the
program before the actual coding takes place.

Advantages of pseudocode
 Pseudocode is understood by the programmers of all types.
 It enables the programmer to concentrate only on the algorithm part of the code
development.
 It cannot be compiled into an executable program. Example, Java code : if (i < 10) { i++; }
pseudocode :if i is less than 10, increment i by 1.

Variables
A variable is a named storage location, where data may be stored and later changed.

 An identifier is a more general term for a named location, which may contain either data or
code.
o Identifiers must begin with a letter or an underscore, preferable letters for user
programs.
o The remaining characters must be either alphanumeric or underscores.
o Identifiers may be of any length, but only the first 31 characters are examined in most
implementations.
o Identifiers are case sensitive, so "NUMBER", "number", and "Number" are three
different identifiers.
o By convention ordinary variables begin with a lower case letter, globals with a Single
Capital, and constants in ALL CAPS.
 Multi-word variables may use either underscores or "camel case", such as
"new_value" or "newValue".
o Integers are usually assigned variable names beginning with the letters I, J, K, L, M, or N,
and floating point variables are usually assigned names beginning with other letters.
o Identifiers may not be the same as reserved words. ( See text for a full list. )
 All variables must be declared before they can be used.
o In C, all variables must be declared before the first executable statement of the
program.
o C++ allows variables to be declared any time before they are used, but it is still normally
good practice to declare all variables at the beginning of the program, unless there is a
very good reason to do otherwise.
 ( Exceptions: Loop counter variables are often declared as part of the loop
structure. Occasionally it is beneficial to declare variables within a reduced
scope, to be discussed later. )
 Variables may be given an initial value at the time they are declared. This is called
"initialization", or "initializing the variables".
o Initialization in C should be done using an equals sign.
o Example: double x1 = 0.0, x2 = 0.0;
o UNINITIALIZED VARIABLES ARE DANGEROUS, AND SHOULD BE CONSIDERED TO HOLD
RANDOM VALUES.
 Variables may be declared "const", meaning that their values cannot be changed.
o const variables MUST be initialized at the time they are declared.
o By convention, const variables are named using ALL CAPS.
o Examples:
 const double PI = 3.14159;
 const int MAXROWS = 100;
o Note: K&R C did not have the const keyword, and so the #define pre-processor macro
was used to define constant values. The const qualifier is a better approach when it is
available, because it allows the compiler to perform type checking among other reasons.
For CS 107 we will defer the discussion of #define until we get to the chapter on the pre-
processor.

Fundamentals of Data Storage


 Variables are named storage locations where data is stored, which may be changed as a
program runs. E.g. "nStudents".
 Constants are values that are hard-coded into a program, and which do not chnage value. E.g.
"3.14159".
 Ultimately all data stored on a computer, both variables and constants, is stored as a sequence
of binary digits, e.g. strings of zeros and ones.
o These binary digits are referred to as "bits".
o
Physically these zeros and ones may be implemented using wires with two different
voltages, magnetic particles with two different alignments, spots on an optical disk
having two different optical properties, or by other means.
 The "type" of a particular variable or constant determines how many bits are used for that
particular data item, and how the bits are to be interpreted.
 Basic C recognizes four basic categories of data types: Integral, Floating Point, Character, and
Character String. Modern C adds a few special types to this list.
 Data may be converted from one type to another, ( possibly with loss of precision ), and new
types may be user defined.

Data Types
int
 The most basic and commonly used integral type is "int".
 The int data type is always the "best" size for the particular computer it is running
on, typically 32 bits
 Format specifiers for ints are either %d or %i, for either printf or scanf.

long int

 A long int typically uses twice as many bits as a regular int, allowing it to hold much
larger numbers.
o ( The C standard only specifies that a long cannot use a fewer number of
bits than a regular int )
 printf and scanf replace %d or %i with %ld or %li to indicate the use of a long int.
 long int may also be specified as just long.

long long int


 C99 introduces the long long int, typically having twice as many bits as a long int
and printed/scanned using %lld or %lli format specifiers

short int

 A short int may use fewer bits than a regular int, thereby saving storage space.
o ( The C standard only specifies that a short int cannot use more bits than a
regular int. On many machines short ints use the same number of bits as
regular ints. )
 printf and scanf replace %d or %i with %hd or%hi to indicate the use of a short int.
 short int may also be specified as just short.

unsigned ints
 Unless otherwise specified, all of the aforementioned int types are signed
numbers.
 Any of the above may be preceded by the keyword "unsigned" to indicate that
they are unsigned.
o e.g. "unsigned int", "unsigned long int", "unsigned long", etc.
o "unsigned" by itself implies "unsigned int"
 The format specifier for unsigned integers in decimal form is %u. The u may be
preceded by l, ll, or h for long, long long, and short unsigned types respectively.
 Unsigned integers can also be printed or scanned in octal or hexidecimal form
using the %o, %x, or %X format specifiers in place of %u.

char
 Normally chars are interpreted as characters ( see below )
 Technically the char data type is an integral type, always having exactly 8 bits, (
known as a byte. )
 Signed chars can have values from -128 to +127, and can be printed as integers
using %d or %i format specifiers
 chars can also be specified as unsigned, giving them a range from 0 to+255.
o Unsigned chars can be printed using %u, %o, %x, or %X format specifiers.

Integral Constants

 int constants in decimal form must begin with a digit from 1 to 9, and can be
followed by additional digits from 0 to 9.
o in octal form they must begin with a 0, and following digits must be in the
range from 0 to 7.
o in hexidecimal form they begin with 0x. Followng digits must be in the
range 0 to 9, or A to F, or a to f.
 Any of the above may be preceded by a minus sign.
 int constants may be followed by a u, l, or ll to indicate unsigned, long, or long long
constants respectively.

Allowable formats are as follows, where the [ square brackets ] denote optional characters:
Decimal: [±]1-9[0-9...][Ll][Uu]
Octal: [±]0[0-7...][Ll][Uu]
Hexadecimal: [±]0x[0-9a-fA-F...][Ll][Uu]

Integer Overflow
 When integer math yields a result that is too big ( or too negative ) to be
represented by the corresponding integer type, then overflow ( or underflow ) is
said to have occurred.
 With unsigned numbers the result is defined to "wrap around" to the other end of
the integer's range, so if you add 1 to an integer that is at the maximum value for
it's type, the result is zero.
 With signed numbers the result of overflow is undefined. In some cases adding 1
to the maximum positive value will wrap around to the most negative value, but in
other cases erratic behaviour or even a program crash may occur.

float
 The most basic type of floating point number is the float type.
 According to the IEEE standard, a single precision floating point number is exactly
32 bits long, comprised of:
o one bit for indicating the sign
o 23 bits ( plus one implied ) for recording the digits as a 24-bit integer. This
works out to about 6 or 7 decimal digits of precision.
o 8 bits for a binary exponent, to shift the digits left or right. This works out
to an absolute range from about 10^-38 to 10^38
 Note that because a float only has 24 bits available for storing the digits of the
number, it can actually be less precise than a 32-bit int for certain large integers.

double
 The double precision data type uses twice as many bits as a float, yielding
approximately twice the number of digits of precision.
 According to the IEEE standard, a double precision floating point number is 64 bits
long, comprised of:
o one sign bit.
o 52 bits ( plus one implied ) for recording digits, which works out to about
15 decimal digits of precision.
o 11 bits for the exponent, which works out to a range of about 10^-308 to
10^308
 The double data type is the preferred floating point type for most scientific and
engineering calculations.

long double
 The long double type is guaranteed to have more bits than a double, but the exact
number my vary from one hardware platform to another. The most typical
implementations are either 80 or 128 bits.
 The IEEE standard for quadruple precision floating point numbers is 128 bits
consisting of:
o one sign bit
o 112 bits ( plus one implied ) for digits, working out to about 34 decimal
digits of precision
o 15 bits for the exponent, giving a range from about 10^-4932 to 10^4932

Floating point constants


 Floating-point constants are normally indicated by the presence of a decimal point,
and are normally doubles.
o Floating point constants may be followed by either an "F" to indicate an
ordinary float, or an "L" to indicate a long double.
o Floating point constants can also be expressed in scientific notation

Allowable formats are as follows:


[±]1-9[0-9...].[0-9...][Ee[±]0-9...][FfLl]
[±][0].[0-9...][Ee[±]0-9...][FfLl]
[±]1-9[0-9...]Ee[±]0-9...[FfLl]
[±]1-9[0-9...]Ff[Ll]

Characters
 Although the char data type is technically a small integer ( see above ), its most common use is
to hold numeric codes representing ( ASCII ) characters.
 For example, the ASCII code for the capital letter 'A' is 65.
 Note that the character '9' is not the same as the integer value 9.
o In this case the ASCII code for the character '9' is 57.
o The numerical value 9 in the ASCII code set happens to represent a horizontal tab
character.
 The full ASCII code table can be found in the back of most programming textbooks, or online at
http://www.asciitable.com/ and many other sites.

Character Constants
 Character constants are enclosed in single quotes, such as 'A'.
 Character constants may also include escape characters such as '\n' or '\t', as
shown here:

Escape Sequence
Meaning

\a alarm ( bell )

\b Backspace

\f Form feed ( clears screen )

\n New line

\r Carriage return
\t Horizontal tab

\v Vertical tab

\\ Backslash

\? Question mark

\' Single quote

\" Double quote

\0 Numerical zero ( null byte )

 The \ can also be used to escape a 3-digit octal numerical constant, or a


hexidecimal constant beginning with \x
o So '\112' and '\x4A' are both a capitol 'J'. See the ASCII table to confirm.

Character Strings
 C stores character strings as arrays of type char, terminated by a null byte.
 Constant character strings are enclosed in double quotes, and may include escape characters,
such as "\n\n\t Please enter X > "
 These notes will postpone further discussion of character strings until after arrays have been
covered.

C program to find area of circle


C program to find area of circle: c programming code to calculate the area of a
circle.

1. #include<stdio.h>
2. #include<math.h>
3.
4. main()
5. {
6. float radius, area;
7.
8. printf("Enter the radius of circle\n");
9.
10. scanf("%f",&radius);
11.
12. /* M_PI (pi) is a constant in math.h header file */
13.
14. area = M_PI*radius*radius;
15.
16. printf("Area of circle = %.2f\n", area);
17.
18. return 0;
19.}

Odd or even program in C using modulus


operator
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n;
6.
7. printf("Enter an integer\n");
8. scanf("%d", &n);
9.
10. if (n%2 == 0)
11. printf("Even\n");
12. else
13. printf("Odd\n");
14.
15. return 0;
16.}
Do the following pattern in C program
*

***

*****

*******

*********

1. #include <stdio.h>
2.
3. int main()
4. {
5. int row, c, n, s;
6.
7. printf("Enter the number of rows in pyramid of stars you wish to see\n");
8. scanf("%d", &n);
9.
10. s = n;
11.
12. for (row = 1; row <= n; row++) // Loop to print rows
13. {
14. for (c = 1; c < s; c++) // Loop to print spaces in a row
15. printf(" ");
16.
17. s--;
18.
19. for (c = 1; c <= 2*row - 1; c++) // Loop to print stars in a row
20. printf("*");
21.
22. printf("\n");
23. }
24.
25. return 0;
26.}
Factorial program in C using a for loop
1. #include <stdio.h>
2.
3. int main()
4. {
5. int c, n, fact = 1;
6.
7. printf("Enter a number to calculate its factorial\n");
8. scanf("%d", &n);
9.
10. for (c = 1; c <= n; c++)
11. fact = fact * c;
12.
13. printf("Factorial of %d = %d\n", n, fact);
14.
15. return 0;
16.}

C program to sum n numbers using a for loop


1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, sum = 0, c, value;
6.
7. printf("How many numbers you want to add?\n");
8. scanf("%d", &n);
9.
10. printf("Enter %d integers\n", n);
11.
12. for (c = 1; c <= n; c++)
13. {
14. scanf("%d", &value);
15. sum = sum + value;
16. }
17.
18. printf("Sum of the integers = %d\n", sum);
19.
20. return 0;
21.}

Fibonacci series C program using a for loop


1. /* Fibonacci series program in C language */
2. #include <stdio.h>
3.
4. int main()
5. {
6. int n, first = 0, second = 1, next, c;
7.
8. printf("Enter the number of terms\n");
9. scanf("%d", &n);
10.
11. printf("First %d terms of Fibonacci series are:\n", n);
12.
13. for (c = 0; c < n; c++)
14. {
15. if (c <= 1)
16. next = c;
17. else
18. {
19. next = first + second;
20. first = second;
21. second = next;
22. }
23. printf("%d\n", next);
24. }
25.
26. return 0;
27.}

C programming code
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, t, sum = 0, remainder;
6.
7. printf("Enter an integer\n");
8. scanf("%d", &n);
9.
10. t = n;
11.
12. while (t != 0)
13. {
14. remainder = t % 10;
15. sum = sum + remainder;
16. t = t / 10;
17. }
18.
19. printf("Sum of digits of %d = %d\n", n, sum);
20.
21. return 0;
22.}

C program to check leap year


1. #include <stdio.h>
2.
3. int main()
4. {
5. int year;
6.
7. printf("Enter a year to check if it is a leap year\n");
8. scanf("%d", &year);
9.
10. if (year%400 == 0) // Exactly divisible by 400 e.g. 1600, 2000
11. printf("%d is a leap year.\n", year);
12. else if (year%100 == 0) // Exactly divisible by 100 and not by 400 e.g. 1900,
2100
13. printf("%d isn't a leap year.\n", year);
14. else if (year%4 == 0) // Exactly divisible by 4 and neither by 100 nor 400
e.g. 2020
15. printf("%d is a leap year.\n", year);
16. else // Not divisible by 4 or 100 or 400 e.g. 2017, 2018, 2019
17. printf("%d isn't a leap year.\n", year);
18.
19. return 0;

20.}

S-ar putea să vă placă și